【问题标题】:Powershell Get-ADUser calculated property Select-Object : Cannot convert System.String[]?Powershell Get-ADUser 计算属性 Select-Object:无法转换 System.String []?
【发布时间】:2020-10-21 22:17:48
【问题描述】:

下面这个可以正常工作,没有问题:

$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

【问题讨论】:

    标签: powershell active-directory powershell-4.0


    【解决方案1】:

    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 不会将其展开为单个数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多