在Get-ADComputer过滤器中使用变量时出错

时间:2015-10-26 21:27:16

标签: powershell active-directory

我正在尝试从CSV导入计算机列表并检查AD中的哪些计算机。我使用以下代码:

$CSV = Import-Csv -Path $pathtoCSV
foreach ($c in $CSV) { 
  $PC = Get-ADComputer -Filter {dnshostname -eq $c.Name}
  # do more stuff with the results here
}

但是我收到以下错误:

Get-ADComputer : Property: 'name' not found in object of type:
'System.Management.Automation.PSCustomObject'.
At line:1 char:30
+ foreach ($c in $csv) { $pc = Get-ADComputer -Filter {dnshostname -eq $c.name}
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADComputer], ArgumentException
    + FullyQualifiedErrorId : Property: 'name' not found in object of type: 'System.Management.Automation.PSCustomObject'.,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

我的CSV文件如下所示:

IP,Name,Mask,MAC,Expires,Type,
1.1.1.1,pcname.domain.com,255.255.255.0,ff-ff-ff-ff-ff-ff,10/27/2015 11:55:19 AM,DHCP

我做错了什么?

1 个答案:

答案 0 :(得分:1)

试试这个:

$CSV = Import-csv -path $pathtoCSV
foreach ($c in $CSV.Name) { 
  $PC = Get-ADComputer -filter {dnshostname -eq $c}
  # do more stuff with the results here
}

或者这个

$CSV = Import-csv -path $pathtoCSV
foreach ($c in $CSV.Name) { 
  $PC = Get-ADComputer -Identity $c
  # do more stuff with the results here
}