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