Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell equivalent of cmd "IF %ERRORLEVEL% NEQ 0"

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~~~~
}
like image 444
gregg Avatar asked Jan 26 '26 06:01

gregg


1 Answers

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
}
like image 66
Mathias R. Jessen Avatar answered Jan 28 '26 21:01

Mathias R. Jessen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!