从csv列表中获取广告中停用的计算机



我想弄清楚哪些计算机被停用了。为此,我在csv列表中提供了计算机名称。我只想输出已停用的计算机。这就是我所拥有的。不幸的是,我的电脑都停用了。但我只想在csv 中提供这些名称

Import-CSV -Path "C:pc_names" | Select -expand Name | Get-ADComputer -searchbase 'XXX' -Filter {(Enabled -eq $False)} -Properties Name, OperatingSystem | Export-CSV “C:TempDisabledComps.CSV” -NoTypeInformation

问题可能出现在Get-ADComputer命令中,您指定了一个SearchBase(假定为OU),并为所有禁用的计算机指定了筛选器,但从未实际包括从CSV传入的计算机的名称,因此它只返回该搜索库下的每台禁用的计算机。

不如试试这样的东西;

Import-CSV -Path "C:pc_names" | Select -Expand Name | Get-ADComputer -SearchBase 'XXX' -Filter {(Enabled -eq $False) -and ($_.Name)} -Properties Name, OperatingSystem | Export-CSV "C:TempDisabledComps.CSV" -NoTypeInformation

注意过滤器中的$_.Name

我可能有过滤器语法错误,但这应该是原因。

使用-Filter参数无法测试计算机名是否在名称数组中找到。。

您需要首先在SearchBase OU中收集计算机对象,并仅筛选禁用的对象
然后,使用Where-Object子句过滤掉$pcNames数组中的那些:

$pcNames = (Import-Csv -Path "C:pc_names.csv").Name 
Get-ADComputer -SearchBase 'XXX' -Filter "Enabled -eq 'False'" -Properties OperatingSystem | 
Where-Object { $pcNames -contains $_.Name } |    # or: Where-Object { $_.Name -in $pcNames }
Export-Csv -Path "C:TempDisabledComps.csv" -NoTypeInformation

注意:默认情况下,Get-ADComputer已经返回这些属性:DistinguishedName, DNSHostName, Enabled, Name, ObjectClass, ObjectGUID, SamAccountName, SID, UserPrincipalName。这意味着在这种情况下,您只需要要求额外的属性OperatingSystem

很明显,这样的操作会忽略管道输入的内容并返回许多计算机。

'comp001' | get-adcomputer -filter 'Enabled -eq $False'

如果你一直等到最后,会出现一条错误消息:

get-adcomputer : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its
properties do not match any of the parameters that take pipeline input.
At line:1 char:13
+ 'comp001' | get-adcomputer -filter 'Enabled -eq $false'
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (comp001:String) [Get-ADComputer], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

你可以让adcomputer进入foreach循环并测试名称:

$list = echo comp001 comp002 comp003
$list | % { get-adcomputer -filter 'Enabled -eq $False -and Name -eq $_' }

相关内容

最新更新