[英]check if DSQuery was successful in PowerShell?
I'm trying to convert a batch script to PowerShell. 我正在尝试将批处理脚本转换为PowerShell。 I encountered this piece of code, can anyone tell me how to convert this to PowerShell?
我遇到了这段代码,谁能告诉我如何将其转换为PowerShell?
dsquery ou -domain "$SysDomain" -name "$FuncOU,$DestOU" || (echo OU was not found & Goto :eof)
I want to do somnething like: 我想像这样做某事:
$dsq = dsquery ou -domain "$SysDomain" -name "$FuncOU,$DestOU"
if ($dsq.HasSucceeded -eq $true) {
echo "OU was not found"
exit
}
dsquery
doesn't return an exit code, so I doubt that your batch command could ever have worked. dsquery
不会返回退出代码,因此我怀疑您的批处理命令是否可以正常工作。 Also, the command produces string output, not a process or job object, so there's no HasSucceeded
property to check. 另外,该命令产生字符串输出,而不是进程或作业对象,因此没有要检查的
HasSucceeded
属性。
What you can do is check the output of the command that is collected in the variable $dsq
. 您可以做的是检查变量
$dsq
收集的命令输出。 If the variable is empty, the command did not find a OU. 如果变量为空,则该命令找不到OU。 PowerShell automatically interprets
$null
values as $false
, so something like this should work: PowerShell会自动将
$null
值解释为$false
,因此这样的方法应该起作用:
$dsq = & dsquery ou ...
if (-not $dsq) {
echo 'OU was not found.'
exit 1
}
Side note: I recommend using the call operator ( &
) for running external commands, and returning an actual exit code when using the exit
statement. 旁注:我建议使用调用运算符(
&
)来运行外部命令,并在使用exit
语句时返回实际的退出代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.