Powershell:导入csv并获取aduser
我正在尝试查找用户,存储在广告中的“users.csv”中:Powershell:导入csv并获取aduser,powershell,active-directory,Powershell,Active Directory,我正在尝试查找用户,存储在广告中的“users.csv”中: Import-Module ActiveDirectory import-csv "\users.csv" | ForEach-Object { Get-ADUser -Filter {displayname -eq $_.id}} 但是PS说找不到“id”属性 users.csv内容: id MacAskill Danny Cedric Gracia e、 t.c编辑CSV文件,并用双引号将显示名称括起来,如下所示: id "M
Import-Module ActiveDirectory
import-csv "\users.csv" | ForEach-Object {
Get-ADUser -Filter {displayname -eq $_.id}}
但是PS说找不到“id”属性
users.csv内容:
id
MacAskill Danny
Cedric Gracia
e、 t.c编辑CSV文件,并用双引号将显示名称括起来,如下所示:
id
"MacAskill Danny"
"Cedric Gracia"
然后,像这样使用itermediate变量
Import-Csv .\users.csv | % {
$f = $_.id; # Set the displayname into a temp variable
get-aduser -filter { displayname -eq $f } # Use the temp variable with filter
}
我不知道为什么需要中间变量。使用
-filter
参数传递和解析变量时出现了一些奇怪的情况。编辑CSV文件并用双引号将显示名括起来,如下所示
id
"MacAskill Danny"
"Cedric Gracia"
# Use Import-csv and Get-ADUser together
# Import csv that contains "sn" column and get all AD users where
# sn matches any of the values imported from csv
Import-Csv C:\temp\1.csv | select sn -ExpandProperty sn | foreach { Get-ADUser -Filter 'sn -eq $_' }
然后,像这样使用itermediate变量
Import-Csv .\users.csv | % {
$f = $_.id; # Set the displayname into a temp variable
get-aduser -filter { displayname -eq $f } # Use the temp variable with filter
}
我不知道为什么需要中间变量。使用
-filter
参数传递和解析变量时出现了一些奇怪的情况。我知道这是一篇老文章,但它确实为我省去了很多麻烦。无法找到具有导入值的用户,但只需创建一个中间变量,我就可以让脚本正常工作。我甚至不需要用双引号
# Use Import-csv and Get-ADUser together
# Import csv that contains "sn" column and get all AD users where
# sn matches any of the values imported from csv
Import-Csv C:\temp\1.csv | select sn -ExpandProperty sn | foreach { Get-ADUser -Filter 'sn -eq $_' }
克里斯我知道这是一篇老文章,但它确实让我省去了很多麻烦。无法找到具有导入值的用户,但只需创建一个中间变量,我就可以让脚本正常工作。我甚至不需要用双引号
Kris“%”的位置是什么?使用PS的第一天,它炸了我的大脑…@nuT707,
%
是每个对象的别名。也就是说,它只是一个较短的形式。就像?
是Where Object
的快捷方式。更多信息:获取有关别名的帮助。
。“%”的位置是什么?使用PS的第一天,它炸了我的大脑…@nuT707,%
是每个对象的别名。也就是说,它只是一个较短的形式。就像?
是Where Object
的快捷方式。更多信息:获取有关\u别名的帮助
。