Powershell errorlevel within a batch

Discussion in 'Windows 7' started by IAmTheDude, Oct 10, 2015.

  1. IAmTheDude

    IAmTheDude MDL Member

    Oct 12, 2011
    112
    19
    10
    #1 IAmTheDude, Oct 10, 2015
    Last edited by a moderator: Apr 20, 2017
    Hey all, (Me again...)


    Been playing around with batch files recently, learnt a lot so far :)


    Stuck on this little thing though.


    Im playing with a script to search for and remove the telemertry updates from W7.


    Code:
    ECHO Searching...
    powershell get-hotfix -id kb2952664,kb2977759
    ...{code for a chice here}...
    

    Works lovely if there are updates present.


    However if there isnt any or they have already been removed you get:


    Code:
    Get-HotFix : This command cannot find hot-fix on the machine 'localhost'. Verify the
     input and Run your command again.
    At line:1 char:11
    + get-hotfix <<<<  -id kb2952664,kb2977759
        + CategoryInfo          : ObjectNotFound: (:) [Get-HotFix], ArgumentException
        + FullyQualifiedErrorId : GetHotFixNoEntriesFound,Microsoft.PowerShell.Commands
       .GetHotFixCommand
    

    I have tried something this:


    Code:
    ECHO Searching...
    set ERROR=%ERRORLEVEL%
    powershell get-hotfix -id kb2952664,kb2977759 || %ERRORLEVEL%
    IF %ERROR% EQU 0 GOTO :NOUPDATES
    
    
    ...
    
    
    :NOUPDATES
    {blah blah}
    

    But Powershell always seems to return 0 no matter what.


    Still quite new to the whole errorlevel thing, first time Ive used it but where should I be looking and whats wrong?


    OT:
    Also, thank you all. Learning a lot from this forum, reading other peoples scripts etc and am doing things I never thought Id be able to do :)
     
  2. netlords

    netlords MDL Novice

    Jun 20, 2012
    35
    6
    0
    When you run native EXE console commands inside your scripts, these commands typically return a numeric return value. This value is known as "ErrorLevel", and in batch files, you would refer to the return value as %ERRORLEVEL%.


    Let's see how PowerShell gets a hold of this numeric return value, and how a PowerShell script can in turn emit its own "ErrorLevel" - that then could be received by the caller of the PowerShell script:


    ping 1.2.3.4 -n 1 -w 500
    $result1 = $LASTEXITCODE


    ping 127.0.0.1 -n 1 -w 500
    $result2 = $LASTEXITCODE


    $result1
    $result2


    if ($result1 -eq 0 -and $result2 -eq 0)
    {
    exit 0
    }
    else
    {
    exit 1
    }
    In this example, the code pings two IP addresses. The first call fails, the second succeeds. The script saves the return code through $LASTEXITCODE and saves it in two variables.


    It then figures out what the impact of these return values are. In the example, the PowerShell script emits an ErrorLevel code of 0 if both calls returned 0, else it emits 1.


    Of course, this is just a simple example. You can use it with your own native commands. Just make sure you save the value of $LASTEXITCODE immediately after the call of the native application, as it is overwritten by subsequent calls.
     
  3. IAmTheDude

    IAmTheDude MDL Member

    Oct 12, 2011
    112
    19
    10
    #3 IAmTheDude, Oct 14, 2015
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Thats starting to make a little sense regarding the ps error codes ut I seem to be running into a problem of 'merging' it in with batch.

    I have tried a few ways but dont seem to be getting the hang of it.

    Code:
    set $result1=%ERRORLEVEL%
    powershell get-hotfix -id kb2952664,kb2977759
    $result1 = $LASTEXITCODE
    
    
    if ($result1 -eq 0)
    {
    exit 0
    }
    else
    {
    exit 1
    } 
    
    
    IF %ERRORLEVEL% EQU 1 GOTO :NOUPDATES

    Code:
    set $result1=%ERRORLEVEL%
    powershell get-hotfix -id kb2952664,kb2977759 || $result1 = $LASTEXITCODE
    
    
    if ($result1 -eq 0)
    {
    exit 0
    }
    else
    {
    exit 1
    } 
    
    
    IF %ERRORLEVEL% EQU 1 GOTO :NOUPDATES
    Code:
    powershell get-hotfix -id kb2952664,kb2977759
    $result1 = $LASTEXITCODE
    
    
    if (&result1 -eq 0)
    {
    GOTO :REMOVEUPDATES
    }
    else
    {
    GOTO :NOUPDATES
    } 
    
    etc etc

    It always fails on the $result1:

    Code:
    '$result1' is not recognized as an internal or external command,
    operable program or batch file.
    -eq was unexpected at this time.
    
     
  4. glego

    glego MDL Novice

    Jan 23, 2016
    3
    1
    0
    #4 glego, Jan 23, 2016
    Last edited by a moderator: Apr 20, 2017
    Hi IAmTheDude,

    I'm actually extremely confused why you are doing batch and Powershell together. It makes little sense as powershell is able to handle errors much more efficiently.

    My simple advise is to:

    1. Focus on Powershell and move away from batch.
    2. If you need more information, check out Don Jones training videos, he is MVP for Powershell and users powershell since the beginning of time. (I cant post links so check it on youtube "Learn Windows PowerShell in a Month of Lunches")

    Anyway, below you kind find the immediate solution for your problem. Although I strongly recommend you follow the courses and start scripting in Powershell.

    The problem is you are mixing batch and Powershell syntaxes, see below example how to catch the "exitcode" from batch.

    Code:
    @echo off
    powershell Get-Item -Path "C:\Error"
    SET result1=%ERRORLEVEL%
    
    powershell Get-Item -Path "C:\Temp"
    SET result2=%ERRORLEVEL%
    
    echo Result 1: %RESULT1%
    echo Result 2: %RESULT2%
    
    Output:
    Code:
    C:\temp\>RunPowershell.bat
    Get-Item : Cannot find path 'C:\Error' because it does not exist.
    At line:1 char:1
    + Get-Item -Path C:\Error
    + ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (C:\Error:String) [Get-Item], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemCommand
    
    
    
        Directory: C:\
    
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----       23/01/2016     23:33                Temp
    
    
    Result 1: 1
    Result 2: 0