powershell中的Get-ADComputer失败,并且“参数上的标识属性为空或空”错误

问题描述 投票:0回答:1

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

Get-ADComputer:无法验证参数'Identity'的参数。参数上的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-v3.0
1个回答
0
投票

好像文件中有空行。

您可以检查NullOrEmpty以及修剪白色空间。

foreach($line in $computers){
  if(-not [String]::IsNullOrEmpty($Line)){
      Get-ADComputer -Identity $line.Trim() -Properties * | FT Name, LastLogonDate, MemberOf -Autosize
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.