PowerShell相当于cmd“IF%ERRORLEVEL%NEQ 0”

时间:2016-06-13 14:04:26

标签: powershell powershell-v3.0

查找与此cmd错误检查相对应的PowerShell:

IF %ERRORLEVEL% NEQ 0

以下是我要编写的PowerShell代码:

Write-Information "Installing .NET 3 from DVD:"
$NetFX3_Source = "D:\Sources\SxS"
dism /online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:$NetFX3_Source /NoRestart
IF (****TheCommandYouTellMe****) {
Write-Information "DVD not found, installing from online sources, the Win default method"
DISM.EXE /Online /Add-Capability /CapabilityName:NetFx3~~~~
Add-WindowsCapability –Online -Name NetFx3~~~~
}

1 个答案:

答案 0 :(得分:4)

由于dism.exe是外部程序,因此您需要检查$LASTEXITCODE自动变量:

dism /online /andsoon
if($LASTEXITCODE -ne 0)
{
    # Add your capability
}