最佳实践 - 格式编号列表输出 - PowerShell

时间:2018-06-01 12:25:27

标签: powershell

使用案例

以下代码列出了localhost上具有SID值的当前用户帐户

将格式输出到输出编号列表不会将表中的值分开

编码可读格式化输出的最佳做法是什么

代码

$sid = [System.Security.Principal.WindowsIdentity]::GetCurrent().groups
$accounts = $sid.Translate([System.Security.Principal.NTAccount]).value

$hash = @{"sid"=$sid.value;"Accounts"=$accounts}

$obj = New-Object -TypeName psobject -Property $hash

for ($i=0; $i -lt $obj.sid.Length; $i++) 
{"{0:D2}. {1}  -   {2}" -f ($i),$obj.Accounts[$i],$obj.sid[$i]}

输出

00. Everyone  -   S-1-1-0
01. NT AUTHORITY\Local account and member of Administrators group  -   S-1-5-114
02. BUILTIN\Administrators  -   S-1-5-32-544
03. BUILTIN\Remote Desktop Users  -   S-1-5-32-555
04. BUILTIN\Remote Management Users  -   S-1-5-32-580
05. BUILTIN\Performance Log Users  -   S-1-5-32-559
06. BUILTIN\Users  -   S-1-5-32-545
07. NT AUTHORITY\INTERACTIVE  -   S-1-5-4
08. CONSOLE LOGON  -   S-1-2-1
09. NT AUTHORITY\Authenticated Users  -   S-1-5-11
10. NT AUTHORITY\This Organization  -   S-1-5-15
11. MicrosoftAccount\sumeet.singhji@outlook.com  -   S-1-11-96-3623454863-58364-18864-2661722203-1597581903-1914428568-11352006
56-1295981414-3729554605-567089576
12. NT AUTHORITY\Local account  -   S-1-5-113
13. LOCAL  -   S-1-2-0
14. NT AUTHORITY\Cloud Account Authentication  -   S-1-5-64-36

2 个答案:

答案 0 :(得分:4)

我会创建一个包含两个属性(SID和Account)的对象列表:

$list = for ($i=0; $i -lt $obj.sid.Length; $i++) 
{
    [PsCustomObject]@{
        SID = $obj.sid[$i]
        Accounts = $obj.Accounts[$i]
    }   
}

然后你只需输入$list

即可得到一个不错的输出
SID                                           Accounts                        
---                                           --------                                
S-1-1-0                                       Everyone                        
S-1-5-32-544                                  BUILTIN\Administrators          
S-1-5-32-559                                  BUILTIN\Performance Log Users   
S-1-5-32-545                                  BUILTIN\Users                   
S-1-5-4                                       NT AUTHORITY\INTERACTIVE        
S-1-2-1                                       CONSOLE LOGON                   
S-1-5-11                                      NT AUTHORITY\Authenticated Users
S-1-5-15                                      NT AUTHORITY\This Organization  
S-1-2-0                                       LOCAL                           

答案 1 :(得分:4)

马丁的回答没有任何问题。但是,如果目标是允许运行脚本的人选择用户,那么我就是Out-Gridview的粉丝:

template<class... U>
movable_il( U&&... in): t{std::forward<U>(in)...} {}