感谢您的帮助 - 输入文本文件包含upz属性值的列表,如果它们存在,我想要查询AD到发现 - 我想通过使用Write-Error,当查询失败时,Write -Error" $ line"将输出到文件,我将拥有不在目录中的所有用户的列表。
如果有更好的方法 - 让我知道 - 这是我到目前为止(还没有工作) - 应该循环文本文件并输出每个成功的查找。不确定如何使用或不使用Where-Object构建IF语句,以及如何说“#”在活动目录中不存在'。
使用这些行创建文件users_upz.ps1,并使用upz值列表(userid属性值的单值列表)创建C:\ users_upz.txt文件:
$UPZ = Get-Content "c:\users_upz.txt"
Write-Output $UPZ.count total lines read from file
ForEach ($line in $upz)
{
Write-Output "$line"
Get-QADObject -Service mydirectory.com:1234 -LdapFilter "(upz=$line)" -properties *|select *
Write-Output "_____________________________________________"
} | Out-File 'c:\icomplyusers_upz.log'
运行:powershell.exe users_upz.ps1
答案 0 :(得分:0)
如果用户不存在,您只需要运行一些代码吗?在这里,我们在Try / Catch / Finally构造中有一个很棒的工具,它允许我们尝试命令,如果遇到错误,请转到Catch块。修改您的代码示例,我们得到以下内容。
ForEach ($line in $upz)
{
Write-Output "$line"
#Try says 'Run this, and if fails, hide the message and continue to the Catch block
try {Get-QADObject -Service mydirectory.com:1234 -LdapFilter "(upz=$line)" `
-properties * -ErrorAction Stop |select * }
catch{Write-Warning "Displaying an error message if user not found"
$errorusers += $line}
现在,将会发生的事情是我们可以在catch块中放入我们想要的任何代码。正如您所看到的,我还添加了一个片段,用于将所有错误收集到$ errorusers对象中,并将其转储到工具末尾的列表中。
从这一点开始有很多选择,我希望这个例子可以帮到你。如果您需要澄清任何内容,请告知我们。