将 powershell 结果左对齐

将 powershell 结果左对齐

我有以下脚本:

Get-Acl 'c:\folder' | Select -ExpandProperty Access | Select IdentityReference,FileSystemRights,AccessControlType,IsInherited | Format-Table -AutoSize

我应该添加什么才能使文本左对齐?

我尝试过谷歌搜索,但没有结果。

答案1

原始输出

Get-Acl -Path  'C:\folder' |
    Select-Object -ExpandProperty Access |
    Select-Object -Property IdentityReference,
        FileSystemRights,
        AccessControlType, IsInherited
IdentityReference                           FileSystemRights AccessControlType IsInherited
-----------------                           ---------------- ----------------- -----------
BUILTIN\Administrators                           FullControl             Allow        True
NT AUTHORITY\SYSTEM                              FullControl             Allow        True
BUILTIN\Users                    ReadAndExecute, Synchronize             Allow        True
NT AUTHORITY\Authenticated Users         Modify, Synchronize             Allow        True
NT AUTHORITY\Authenticated Users                  -536805376             Allow        True

我应该添加什么才能使文本左对齐?尝试概念计算属性(仅适用于FileSystemRights以下示例中的属性):

Get-Acl -Path 'C:\folder' |
    Select-Object -ExpandProperty Access |
    Select-Object -Property IdentityReference,
        @{Name='FileSystemRights';Expression={"$($_.FileSystemRights)"}},
        AccessControlType, IsInherited
IdentityReference                FileSystemRights            AccessControlType IsInherited
-----------------                ----------------            ----------------- -----------
BUILTIN\Administrators           FullControl                             Allow        True
NT AUTHORITY\SYSTEM              FullControl                             Allow        True
BUILTIN\Users                    ReadAndExecute, Synchronize             Allow        True
NT AUTHORITY\Authenticated Users Modify, Synchronize                     Allow        True
NT AUTHORITY\Authenticated Users -536805376                              Allow        True

答案2

这是 PowerShell 中的默认格式化程序,如果您想要不同的东西,您需要更改格式化程序或使用 format-* cmdlet、字符串格式化,或创建结果的自定义对象(如计算属性、哈希表等)并按您希望的方式格式化。

‘格式化 powershell cmdlet 输出’

'format powershell 格式运算符'

-f 格式运算符

对齐。正数将右对齐 n 个字符。负数将左对齐 n 个字符。

左对齐和右对齐文本:

“|{0,-10}| |{1,10}|” -f“你好”,“世界”|你好||世界|

因此,例如,组合计算属性和 -F 运算符将得到以下结果...(顺便说一句,您可以在下面看到我使用点引用与 -ExpandProperty 来获取数据。)

(Get-Acl -Path  'D:\temp').Access | 
Select-Object -Property @{Name = 'IdentityReference ';Expression = {$PSItem.IdentityReference}},
@{Name = 'FileSystemRights ';Expression = {"{0,6}" -f $PSItem.FileSystemRights}},
@{Name = 'AccessControlType ';Expression = {"{0,5}" -f $PSItem.AccessControlType}},
@{Name = 'IsInherited';Expression = {"{0,6}" -f $PSItem.IsInherited}}
# Results
<#
IdentityReference                             FileSystemRights  AccessControlType  IsInherited
------------------                            ----------------- ------------------ -----------
NT AUTHORITY\SYSTEM                           FullControl       Allow               False     
BUILTIN\Administrators                        FullControl       Allow               False     
...
#>

或者 PSCustomObject,像这样......

ForEach ($AclObject in (Get-Acl -Path  'D:\temp').Access)
{
    [PSCustomObject]@{
        IdentityReference = $AclObject.IdentityReference
        FileSystemRights  = ($AclObject.FileSystemRights).ToString()
        AccessControlType = ($AclObject.AccessControlType).ToString()
        IsInherited       = ($AclObject.IsInherited).ToString()
    }
}
# Results
<#
IdentityReference                             FileSystemRights AccessControlType IsInherited
-----------------                             ---------------- ----------------- -----------
NT AUTHORITY\SYSTEM                           FullControl      Allow             False      
BUILTIN\Administrators                        FullControl      Allow             False
...
#>

或这个...

ForEach ($AclObject in (Get-Acl -Path  'D:\temp').Access)
{
    [PSCustomObject]@{
        IdentityReference = $AclObject.IdentityReference
        FileSystemRights  = "$($AclObject.FileSystemRights)"
        AccessControlType = "$($AclObject.AccessControlType)"
        IsInherited       = "$($AclObject.IsInherited)"
    }
}

# Results
<#
IdentityReference                             FileSystemRights AccessControlType IsInherited
-----------------                             ---------------- ----------------- -----------
NT AUTHORITY\SYSTEM                           FullControl      Allow             False      
BUILTIN\Administrators                        FullControl      Allow             False
...
#>

相关内容