Powershell 中的 Get-ADComputer 失败并出现 "Identity property on the argument is null or empty" 错误

Get-ADComputer in powershell fails with "Identity property on the argument is null or empty" error

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

Get-ADComputer : Cannot validate argument on parameter 'Identity'. The Identity property on the argument is null or empty.

这是脚本:

$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.

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

文件中似乎有空行。

您可以检查 NullOrEmpty 以及修剪空格。

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