1

How can I capture errors when using DISM tool from PowerShell script?

& cmd /c 'DISM /online /disable-feature /NoRestart /featurename:[feature_name] >NUL 2>&1'

if ($LASTEXITCODE -ne 0) {
    Write-Host "ERROR"
} else {
    Write-Host "SUCCESS"
}

Whether [feature_name] exists or not, I always get ERROR.

However it works if I replace the cmd command with another, for example

& cmd /c 'dir [some_file] >NUL 2>&1'

If [some_file] exists I get SUCCESS, otherwise ERROR.

AndroidX
  • 558
  • 4
  • 15
  • Consider the `DISM` cmdlets. https://technet.microsoft.com/itpro/powershell/windows/dism/inde – Matthew Wetmore Feb 16 '17 at 23:42
  • Link to DISM cmdlets did not work for me. https://technet.microsoft.com/en-us/library/hh852126.aspx?f=255&MSPPError=-2147217396 – lit Feb 17 '17 at 15:04
  • `DISM` cmdlets are not supported on Windows 7 (which is where I need to run the script). Anyway I've found the way to handle the errors – AndroidX Feb 18 '17 at 00:49

1 Answers1

0

It seems that the exit code for success returned by DISM is 3010 instead of 0.

This works for DISM commands within PowerShell:

if ($LASTEXITCODE -ne 3010) {
    ...
} else {
    ...
}
AndroidX
  • 558
  • 4
  • 15