Powershell AD:类似过滤器的描述 $variable => 包含$variable



我的任务包括在AD中筛选组和子组中的所有用户名。继续筛选计算机并只显示这些计算机,其中包含已筛选的名称。问题是,该描述还包括其他字符,如空格或"空格";新";。

我的代码:

foreach ($file in Get-ADGroupMember -Identity GroupName -Recursive) {Get-ADComputer -Filter 'Description -like $file.name' -Property Name,Description | Select -Property Name,Description}

如果只添加*或更改-喜欢-包括:D,那就太好了。但是。。。

我的问题是:如何编写代码来查看所有结果,而不仅仅是与$file.name完全匹配的结果?

谢谢你抽出时间!

您最初的问题是使用的Filter。通过正确的引用并使用子表达式运算符$()修复它。

然而,正如我在评论中所承诺的那样,以下是我关于如何创建组成员(包括用户、计算机,如果你喜欢的话,还包括子组(的报告的意思。由于从Get-ADGroupMembercmdlet返回的所有对象都具有.objectClass属性,因此可以使用该属性来确定下一个可以使用的Get-AD*cmdlet。

在这里,我将在foreach((循环中捕获收集的对象输出,该变量可以显示在屏幕上,也可以保存为Csv文件,例如可以在Excel中打开。

$groupName = 'GroupName'
$result = foreach($adObject in (Get-ADGroupMember -Identity $groupName -Recursive)) {
# use the proper Get-AD* cmdlet depending on the type of object you have
switch ($adObject.objectClass) {
'user' {
$adObject | Get-ADUser -Properties Description | Select-Object Name, Description, @{Name = 'Type'; Expression = {'User'}}
}
'computer' {
$computer = $adObject | Get-ADComputer -Properties Description
# you want to output only the computers where the Description property holds the computer name
if ($computer.Description -like '*$($computer.Name)*') {
$computer | Select-Object Name, Description, @{Name = 'Type'; Expression = {'Computer'}}
}
}
# perhaps you don't want subgroups in your report, in that case just remove or comment out the next part
'group' { 
$adObject | Get-ADGroup -Properties Description | Select-Object Name, Description, @{Name = 'Type'; Expression = {'Group'}}
}
}
}
# show the result on screen
$result | Format-Table -AutoSize
# save the result as Csv file
$outFile = Join-Path -Path 'X:Somewhere' -ChildPath ('{0}_members.csv' -f $groupName)
$result | Export-Csv -Path $outFile -NoTypeInformation -UseCulture

-UseCulture开关确保Csv文件使用本地Excel期望的分隔符。如果没有,则使用逗号

有趣的阅读:

  • about_Operators
  • 亚当
  • 学习Powershell |获得更多

当然还有StackOverflow

最新更新