CMD的等同声明是什么:
dir && cd ..
<\ n>在Powershell中?
我试过了:
dir -and cd ..
但它会引发错误:
Get-ChildItem:找不到与参数名称匹配的参数 &#39;和&#39;
在行:1字符:5
+
dir -and(cd ..)
+
CategoryInfo:InvalidArgument:(:) [Get-ChildItem],ParameterBindingException
+
FullyQualifiedErrorId:NamedParameterNotFound,Microsoft.PowerShell .Commands.GetChildItemCommand
答案 0 :(得分:4)
在PowerShell中没有cmd.exe &amp;&amp; 中的直接等效词,这意味着“只有在左侧成功时才执行右侧”。但是你可以写一个简短的函数来做同等的事情:
function IfTrue([ScriptBlock] $testExpression, [ScriptBlock] $runExpression) {
if ( & $testExpression ) { & $runExpression }
}
例如:
IfTrue { get-childitem "fileThatExists.txt" -ea SilentlyContinue } { "File exists..." }
如果你想让$ testExpression产生输出,那么IfTrue函数可以写成如下:
function IfTrue([ScriptBlock] $testExpression, [ScriptBlock] $runExpression) {
& $testExpression
if ( $? ) { & $runExpression }
}
比尔
答案 1 :(得分:2)