下面的这个工作正常,没有问题:
$properties = 'Name,msExchRemoteRecipientType,msExchRecipientDisplayType,msExchRecipientTypeDetails,proxyAddresses' -split ','
Get-ADUser -identity 'Lee.Xia' -Properties $properties |
Select-Object $properties
但是下面的这个,它仅使用SMTP格式化电子邮件地址不起作用? 添加此行:@{Label = 'Email Address'; Expression = {($_.proxyAddresses | Where-Object {($_ -like 'SMTP*') -and ($_ -notlike '*onmicrosoft.com') } | Sort-Object -CaseSensitive -Descending | ForEach-Object {$_.Split(':')[1]}) -join ', ' }}
所以它会像:
$properties = 'Name,msExchRemoteRecipientType,msExchRecipientDisplayType,msExchRecipientTypeDetails,proxyAddresses' -split ','
Get-ADUser -identity 'Lee.Xia' -Properties $properties |
Select-Object $properties, @{Label = 'Email Address'; Expression = {($_.proxyAddresses | Where-Object {($_ -like 'SMTP*') -and ($_ -notlike '*onmicrosoft.com') } | Sort-Object -CaseSensitive -Descending | ForEach-Object {$_.Split(':')[1]}) -join ', ' }}
这是错误代码:
Select-Object : Cannot convert System.String[] to one of the following types {System.String, System.Management.Automation.ScriptBlock}.
At line:3 char:1
+ Select-Object $properties, @{Label = 'Email Address'; Expression = {( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException
+ FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand
Select-Object
的-Property
参数需要单个属性或属性数组。由于$properties
已经是一个数组,因此您需要使用+
而不是,
将计算属性添加到其中。
$properties = 'Name,msExchRemoteRecipientType,msExchRecipientDisplayType,msExchRecipientTypeDetails,proxyAddresses' -split ','
Get-ADUser -identity 'Lee.Xia' -Properties $properties |
Select-Object ($properties + @{Label = 'Email Address'; Expression = {($_.proxyAddresses | Where-Object {($_ -like 'SMTP*') -and ($_ -notlike '*onmicrosoft.com') } | Sort-Object -CaseSensitive -Descending | ForEach-Object {$_.Split(':')[1]}) -join ', ' }})
使用,
而不是+
将创建一个包含两个元素的数组。第一个元素是你的$properties
数组。第二个元素是计算属性。Select-Object
不会将其作为单个数组展开。