How can I format the output of the proxy function in separate lines?

I created a proxy function for Get-ADUser that adds some AD attributes to the output. I am trying to format them so that they are easier to read. I would like both additional attributes to be formatted in a vertical list, something like this:

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 

Currently, proxyAddresses is formatted as ' j.bloggs@blah.com user.admin@blah.com mr.big@blah.commember ' , and memberOf is formatted as '{ABCGroup1, ABCGroup2, ABCGroup3}'

Recreate. Create a module:

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

Create a proxy function for Get-ADUser using C:\Users\user-id\Documents\WindowsPowerShell\Modules\TestModule1\Get-ADUserEx.ps1 (code at the end) and the format file: C:\Users\user-id\Documents\WindowsPowerShell\Modules\TestModule1\TestModule1.format.ps1xml , containing:

I added this proxy function to the new module, which also has a TestModule1.format.ps1xml file containing a view defined as:

 <?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> 

And the function looks like this:

 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 #> } 
+4
source share
1 answer

What you see is the default behavior of how Out-Host formats your objects. Whenever you see {}, it is because this element is a collection (you may have already known this by simply pointing out thoroughness).

That says ... if you want it to print "beautifully" in the console, you will have to combine one large line instead of using a collection. The key to this is using "n" in your lines to create a new line return.

I briefly tested this with your XML, and it worked, but I assume that you will need to tweak it a bit to make sure it breaks your string correctly and formats it the way you want.

For proxy addresses, I was able to get away from this. In the worst case, you will have to use $ _. Split ("") if the addresses are not returned as an array.

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

I used a similar approach for MemberOf groups.

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

Hope this helps ...

+1
source

All Articles