我是使用 PowerShell 的新手,我正在尝试从 Active Directory 中选择一些类似于字符串的字段,让我解释一下:
到目前为止,这是我的代码:
$csv = Import-Csv C:\Users\barreola\Desktop\test_AD.csv
$IS = @(
'*601*',
'*609*',
'*602*',
'*608*',
'*610*',
'*611*',
'*1212*',
'*603*',
'*604*'
)
$csv.Username | ForEach-Object {
Get-ADUser $_ -Properties Department | Select-Object SamAccountName,Department
}
至此,这检索到了预期的数据,如下图
SamAccountName Department
-------------- ----------
user1 First floor department (0601)
user2 Second floor department (0609)
user3 Second floor department (0609)
user4 Second floor department (0609)
user5 Third floor department (0604)
user6 Fourth floor department (0707)
user7 Fifth floor department (0500)
user8 Sixth floor department (1306)
user9 First floor department (0601)
但我希望 PowerShell 只显示包含在 中的内容$IS
,例如
Get-ADUser $_ -Properties Department | Select-Object SamAccountName,Department Where-Object {Department -like $IS}
预期的输出将是
SamAccountName Department
-------------- ----------
user1 First floor department (0601)
user2 Second floor department (0609)
user3 Second floor department (0609)
user4 Second floor department (0609)
user5 Third floor department (0604)
user9 First floor department (0601)
注意:.csv
文件包含 700,000+ 行。