PowerShell equivalent of cmd “IF %ERRORLEVEL% NEQ 0”

坚强是说给别人听的谎言 提交于 2019-12-18 09:31:21

问题


Looking for the PowerShell equivalent of this cmd error-check:

IF %ERRORLEVEL% NEQ 0

Here is the PowerShell code I am trying to write:

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:


Since dism.exe is an external program, you'd want to check the $LASTEXITCODE automatic variable:

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


来源:https://stackoverflow.com/questions/37791797/powershell-equivalent-of-cmd-if-errorlevel-neq-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!