Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查DSQuery在PowerShell中是否成功?_Powershell_Batch File_Active Directory_Windows Scripting - Fatal编程技术网

检查DSQuery在PowerShell中是否成功?

检查DSQuery在PowerShell中是否成功?,powershell,batch-file,active-directory,windows-scripting,Powershell,Batch File,Active Directory,Windows Scripting,我正在尝试将批处理脚本转换为PowerShell。我遇到了这段代码,谁能告诉我如何将其转换为PowerShell dsquery ou -domain "$SysDomain" -name "$FuncOU,$DestOU" || (echo OU was not found & Goto :eof) 我想做一些梦游,比如: $dsq = dsquery ou -domain "$SysDomain" -name "$FuncOU,$DestOU" if ($dsq.HasSuccee

我正在尝试将批处理脚本转换为PowerShell。我遇到了这段代码,谁能告诉我如何将其转换为PowerShell

dsquery ou -domain "$SysDomain" -name "$FuncOU,$DestOU" || (echo OU was not found & Goto :eof)
我想做一些梦游,比如:

$dsq = dsquery ou -domain "$SysDomain" -name "$FuncOU,$DestOU"
if ($dsq.HasSucceeded -eq $true) {
    echo "OU was not found"
    exit
}
dsquery不返回退出代码,所以我怀疑您的批处理命令是否有效。此外,该命令生成字符串输出,而不是进程或作业对象,因此没有要检查的HasSucceeded属性

您可以做的是检查在变量$dsq中收集的命令的输出。如果变量为空,则命令未找到OU。PowerShell,所以类似这样的东西应该可以工作:

$dsq = & dsquery ou ...
if (-not $dsq) {
    echo 'OU was not found.'
    exit 1
}

旁注:我建议使用call操作符&来运行外部命令,并在使用exit语句时返回实际的退出代码。

使用正确的格式。