如果在 Get-ADUser 脚本中没有找到用户,则向我找到的用户的 CSV 输出一条消息
If no user found in Get-ADUser script, output a message to my CSV of users found
我有一个简单的脚本可以运行,接收用户 ID 列表并输出一个 CSV 文件,其中包含他们的 ID、Enabled True/False 和他们的名字。我试图弄清楚如何将测试包装到其中,如果根据输入文件中的 ID 找不到用户,我的输出将包含“用户 [Id] 已清除”。或类似的东西。
我一直在尝试,但我不确定是否需要更改我的输出计划以合并类似的东西。这是我的工作,减去检查是否没有找到,写一条消息。
Get-Content $ENV:USERPROFILE\desktop\testusers.txt | ForEach {
Get-ADUser -Filter "SamAccountName -eq '$_'" -Properties SamAccountName,Enabled,Name |
Select SamAccountName,Enabled,Name
} | Export-CSV -path $ENV:USERPROFILE\desktop\output.csv -NoTypeInformation
这将为找不到的用户显示警告。
注意,在这种情况下不需要 -Properties SamAccountName,Enabled,Name
,这 3 个属性是 cmdlet 的默认属性。
$dom = (Get-ADRootDSE).defaultNamingContext
Get-Content $ENV:USERPROFILE\desktop\testusers.txt | ForEach-Object {
if([string]::IsNullOrWhiteSpace($_)){
continue # go to next iteration if this element is just white space
}
$usr = Get-ADUser -Filter "SamAccountName -eq '$_'" | Select SamAccountName,Enabled,Name
if(-not $usr){
Write-Warning "$_ could not be found on $dom"
continue
}
$usr
} | Export-CSV -path $ENV:USERPROFILE\desktop\output.csv -NoTypeInformation
如果我没理解错的话,你想存储用户 Get-ADUser
在输出 csv 中也找不到错误。
在那种情况下,只需尝试获取用户但吞下错误消息并使用 if{..} else {..}
构造,在这两种情况下您输出具有相同属性的对象:
Get-Content "$env:USERPROFILE\desktop\testusers.txt" | Where-Object { $_ -match '\S' } | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$_'" -ErrorAction SilentlyContinue
if ($user) {
# output the properties you need in the csv
$user | Select-Object SamAccountName, Enabled, Name
}
else {
# user does not exist; output a similar object with the error
[PsCustomObject]@{
SamAccountName = "User '$_' does not exist"
Enabled = $null
Name = $null
}
}
} | Export-CSV -path "$env:USERPROFILE\desktop\output.csv" -NoTypeInformation
我有一个简单的脚本可以运行,接收用户 ID 列表并输出一个 CSV 文件,其中包含他们的 ID、Enabled True/False 和他们的名字。我试图弄清楚如何将测试包装到其中,如果根据输入文件中的 ID 找不到用户,我的输出将包含“用户 [Id] 已清除”。或类似的东西。
我一直在尝试,但我不确定是否需要更改我的输出计划以合并类似的东西。这是我的工作,减去检查是否没有找到,写一条消息。
Get-Content $ENV:USERPROFILE\desktop\testusers.txt | ForEach {
Get-ADUser -Filter "SamAccountName -eq '$_'" -Properties SamAccountName,Enabled,Name |
Select SamAccountName,Enabled,Name
} | Export-CSV -path $ENV:USERPROFILE\desktop\output.csv -NoTypeInformation
这将为找不到的用户显示警告。
注意,在这种情况下不需要 -Properties SamAccountName,Enabled,Name
,这 3 个属性是 cmdlet 的默认属性。
$dom = (Get-ADRootDSE).defaultNamingContext
Get-Content $ENV:USERPROFILE\desktop\testusers.txt | ForEach-Object {
if([string]::IsNullOrWhiteSpace($_)){
continue # go to next iteration if this element is just white space
}
$usr = Get-ADUser -Filter "SamAccountName -eq '$_'" | Select SamAccountName,Enabled,Name
if(-not $usr){
Write-Warning "$_ could not be found on $dom"
continue
}
$usr
} | Export-CSV -path $ENV:USERPROFILE\desktop\output.csv -NoTypeInformation
如果我没理解错的话,你想存储用户 Get-ADUser
在输出 csv 中也找不到错误。
在那种情况下,只需尝试获取用户但吞下错误消息并使用 if{..} else {..}
构造,在这两种情况下您输出具有相同属性的对象:
Get-Content "$env:USERPROFILE\desktop\testusers.txt" | Where-Object { $_ -match '\S' } | ForEach-Object {
$user = Get-ADUser -Filter "SamAccountName -eq '$_'" -ErrorAction SilentlyContinue
if ($user) {
# output the properties you need in the csv
$user | Select-Object SamAccountName, Enabled, Name
}
else {
# user does not exist; output a similar object with the error
[PsCustomObject]@{
SamAccountName = "User '$_' does not exist"
Enabled = $null
Name = $null
}
}
} | Export-CSV -path "$env:USERPROFILE\desktop\output.csv" -NoTypeInformation