首页 > 解决方案 > Powershell 中的 Get-ADComputer 失败并出现“参数上的标识属性为空或为空”错误

问题描述

我有一个用于调用的 powershell 脚本,Get-ADComputer此时我收到此错误:

Get-ADComputer:无法验证参数“身份”上的参数。参数的 Identity 属性为 null 或空。

这是脚本:

$computers = Get-Content -Path C:\Users\computersforscript.txt

    foreach($line in $computers)

 {
    Get-ADComputer -Identity $line -Properties * | FT Name, LastLogonDate, MemberOf -Autosize

}

 $computers |
 Select-Object Name,LastLogonDate,MemberOf |
 Export-CSV -Path C:\Users\output.txt -NoTypeInformation

The script does iterate through each workstation on "computersforscript.txt
" but it does not export it and it errors out.

如果您能帮我解决这个问题,我将不胜感激。

标签: powershell-3.0

解决方案


文件中似乎有空行。

您可以检查NullOrEmpty和修剪空白。

foreach($line in $computers){
  if(-not [String]::IsNullOrEmpty($Line)){
      Get-ADComputer -Identity $line.Trim() -Properties * | FT Name, LastLogonDate, MemberOf -Autosize
   }
}

推荐阅读