繁体   English   中英

Powershell写错误结果

[英]Powershell Write-Error Results

感谢您的帮助-输入文本文件包含要针对AD查询的upz属性值列表,以发现它们是否存在-我在考虑使用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

仅在用户不存在时才需要运行一些代码? 在这里,我们在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对象中,该对象转储到工具末尾的列表中。

从现在开始,有很多选择,希望这个例子对您有所帮助。 如果您需要在此处进行任何说明,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM