如何将代理函数的输出格式化为单独的行?

How can I format the output of a proxy function to be on separate lines?

我已经为 Get-ADUser 创建了一个代理函数,它向输出添加了一些 AD 属性。我正在尝试对它们进行格式化,以便更容易阅读。我希望将这两个附加属性格式化为垂直列表,如下所示:

Name              : Joe Bloggs
DistinguishedName : CN=Joe Blogs,OU=User,etc.
ProxyAddresses    : j.bloggs@blah.com
                    user.admin@blah.com
                    mr.big@blah.com
Member of         : ABCGroup1
                    ABCGroup2
                    ABCGroup3

目前,proxyAddresses 的格式为 'j.bloggs@blah.com user.admin@blah.com mr.big@blah.commember',memberOf 的格式为 '{ABCGroup1, ABCGroup2, ABCGroup3}'

重新创建。创建模块:

New-ModuleManifest 'C:\Users\user-id\Documents\WindowsPowerShell\Modules\TestModule1\TestModule1.psd1' -FormatsToProcess 'TestModule1.format.ps1xml' -ScriptsToProcess 'Get-ADUserEx.ps1'

使用 C:\Users\user-id\Documents\WindowsPowerShell\Modules\TestModule1\Get-ADUserEx.ps1(代码在末尾)创建 Get-ADUser 的代理函数和格式文件:C:\Users\user-id\Documents\WindowsPowerShell\Modules\TestModule1\TestModule1.format.ps1xml 包含:

我已将该代理功能添加到一个新模块,该模块还有一个 TestModule1.format.ps1xml 文件,其中包含定义为的视图:

<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
  <SelectionSets>
  </SelectionSets>
  <Controls>
  </Controls>
  <ViewDefinitions>
    <View>
      <Name>ADABCUserList</Name>
      <ViewSelectedBy>
        <TypeName>ABC.ADUserEx</TypeName>
      </ViewSelectedBy>
      <ListControl>
        <ListEntries>
          <ListEntry>
            <ListItems>
              <ListItem>
                <PropertyName>Name</PropertyName>
              </ListItem>
              <ListItem>
                <PropertyName>DistinguishedName</PropertyName>
              </ListItem>
              <ListItem>
                <PropertyName>ProxyAddresses</PropertyName>
                <FormatString>{0}</FormatString>
              </ListItem>
              <ListItem>
                <Label>ABC Group(s):</Label>
                <ScriptBlock>
                    foreach ($item in $_.MemberOf) {
                        if ($item -match 'ABC') {
                            $commaIndex = $item.IndexOf(',OU=')
                            $cn = $item.Substring(3, $commaIndex - 3)
                            "{0}" -f $cn
                        }
                    }
                </ScriptBlock>
              </ListItem>
            </ListItems>
          </ListEntry>
        </ListEntries>
      </ListControl>
    </View>
  </ViewDefinitions>
</Configuration>

函数如下所示:

function Get-ADUserEx {
    [CmdletBinding(DefaultParameterSetName='Filter')]
    param(
        [Parameter(ParameterSetName='Filter', Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]
        ${Filter},

        [Parameter(ParameterSetName='LdapFilter', Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]
        ${LDAPFilter},

        [Alias('Property')]
        [ValidateNotNullOrEmpty()]
        [string[]]
        ${Properties},

        [Parameter(ParameterSetName='Filter')]
        [Parameter(ParameterSetName='LdapFilter')]
        [ValidateRange(0, 2147483647)]
        [ValidateNotNullOrEmpty()]
        [int]
        ${ResultPageSize},

        [Parameter(ParameterSetName='LdapFilter')]
        [Parameter(ParameterSetName='Filter')]
        [System.Nullable[int]]
        ${ResultSetSize},

        [Parameter(ParameterSetName='LdapFilter')]
        [Parameter(ParameterSetName='Filter')]
        [ValidateNotNull()]
        [string]
        ${SearchBase},

        [Parameter(ParameterSetName='Filter')]
        [Parameter(ParameterSetName='LdapFilter')]
        [ValidateNotNullOrEmpty()]
        [Microsoft.ActiveDirectory.Management.ADSearchScope]
        ${SearchScope},

        [Parameter(ParameterSetName='Identity', Mandatory=$true, Position=0, ValueFromPipeline=$true)]
        [ValidateNotNull()]
        [Microsoft.ActiveDirectory.Management.ADUser]
        ${Identity},

        [Parameter(ParameterSetName='Identity')]
        [ValidateNotNullOrEmpty()]
        [string]
        ${Partition},

        [ValidateNotNullOrEmpty()]
        [string]
        ${Server},

        [ValidateNotNullOrEmpty()]
        [pscredential]
        [System.Management.Automation.CredentialAttribute()]
        ${Credential},

        [Microsoft.ActiveDirectory.Management.ADAuthType]
        ${AuthType})

    begin
    {
        try {
            $outBuffer = $null
            if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
            {
                $PSBoundParameters['OutBuffer'] = 1
            }

            $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Get-ADUser', [System.Management.Automation.CommandTypes]::Cmdlet)

            $props = @(
                'ProxyAddresses',
                'MemberOf'
            )

            if ($PSBoundParameters.ContainsKey('Properties')) {
                foreach ($prop in $PSBoundParameters['Properties']) {
                    if (-not $props.Contains($prop)) {
                        $props += $prop
                    }
                }
            }

            $PSBoundParameters['Properties'] = $props

            $scriptCmd = { & $wrappedCmd @PSBoundParameters | 
                    ForEach-Object { 
                        $_.PSTypeNames.Insert(0,'ABC.ADUserEx'); 
                        $_ 
                    }
            }

            $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
            $steppablePipeline.Begin($PSCmdlet)
        } catch {
            throw
        }
    }

    process
    {
        try {
            $steppablePipeline.Process($_)
        } catch {
            throw
        }
    }

    end
    {
        try {
            $steppablePipeline.End()
        } catch {
            throw
        }
    }
    <#

    .ForwardHelpTargetName Get-ADUser
    .ForwardHelpCategory Cmdlet

    #>
}

您看到的是 Out-Host 如何格式化您的对象成员的默认行为。每当您看到 { } 时,那是因为该成员是一个集合(您可能已经知道这一点,只是为了彻底指出)。

就是说...如果您希望它在控制台中打印 "pretty",您将不得不连接一个大字符串而不是使用集合。这样做的关键是在字符串中使用“`n”来创建一个新行 return.

我用您的 XML 对此进行了简短测试,它确实有效,但我想您需要进行一些调整以确保它正确地拆分您的字符串并按照您想要的方式格式化。

对于代理地址,我能够侥幸逃脱。最坏的情况是,如果地址不是 return 数组,则您必须使用 $_.split(" ")。

    <ListItem>
        <Label>ProxyAddressesPretty</Label>
        <ScriptBlock>
            $_.ProxyAddresses | %{
                $return += $_ + "`n"
            }
            return $return
        </ScriptBlock>
    </ListItem>

我对 MemberOf 组使用了类似的方法

    <ScriptBlock>
        $groups = ""
        foreach ($item in $_.MemberOf) {
            $commaIndex = $item.IndexOf(',OU=')
            $cn = $item.Substring(3, $commaIndex - 3)
            $groups += "$cn`n"
        }
        return $groups
    </ScriptBlock>

希望对您有所帮助...