New to PoSh - else statement failing

This topic contains 4 replies, has 3 voices, and was last updated by  Matthew 4 weeks, 1 day ago.

  • Author
    Posts
  • #99733

    Matthew
    Participant

    Hello all,

    I am packaging an application with the PowerShell Application Deployment Toolkit. During Post-Installation tasks, I would like to look and see if our AT&T VPN exists, and if so, remove it. I got this to work with a WMI query, but that is too slow, can cause issues, and forced an immediate reboot. My solution is to run AT&T's .exe with some silent switches. Here is my script:
    Get-ChildItem -Path "C:\Program Files (x86)" | Where-Object {$_.FullName -Like "*at&t Global*"}
    if ($_.FullName -Like "*AT&T Global*") {
    Execute-Process -Path "$dirSupportFiles\agnc.exe" -Parameters "/S /v/qn /uninstall /norestart"
    }
    Else {
    Out-Null
    }

    There are probably better ways to do this but I did not want it to throw errors. The deployment tool I am using is junk and there are no detection methods, just exit codes. Here is what I get when I run the script by itself:
    Mode LastWriteTime Length Name
    —- ————- —— —-
    d—– 4/25/2018 9:28 AM AT&T Global Network Client

    When I execute PSADT, I get this error:
    Message : The term 'Else' is not recognized as the name of a cmdlet, function, script file, or operable
    program. Check the spelling of the name, or if a path was included, verify that the path is correct
    and try again.

  • #99756

    postanote
    Participant

    Remember, please use the code formmater tools when you post to make things easier to deal with for us on the forum.

    As for your query...

    You are not sending / passing the result of this...

    Get-ChildItem -Path "C:\Program Files (x86)" | Where-Object {$_.FullName -Like "*at&t Global*"}
    

    ... to this (or in your post you ae showing that you are)...
    and you are not using the recurse switch so, if this is a subdir, it won't be found

     
    if ($_.FullName -Like "*AT&T Global*") {
     Execute-Process -Path "$dirSupportFiles\agnc.exe" -Parameters "/S /v/qn /uninstall /norestart"
     }
     Else {
     Out-Null
     }
    

    Why do these separately. Try it this way...

    if ($FileToExecute = (Get-ChildItem -Path 'C:\Program Files (x86)' -filter "AcroRd32.exe"  -Recurse -ErrorAction SilentlyContinue ).FullName) 
    { "File found. Execute process started for $FileToExecute" }
    Else { 'File not found. Execution process skipped' }
    
    # File found. Execute process started for C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe
    
    • #99765

      Matthew
      Participant

      I am not calling an uninstaller in the folder. I have need to call an executable I have in a cache locally on the computer. I'm looking for the existence of that folder because that means the software exists and can now run my executable.

      Thank you for helping

  • #99757

    Richard Siddaway
    Moderator

    running this

    $x = 2
    
    if ($x -eq 2){
    'yay'
    }
    else {
    'no'
    }
    

    In the PowerShell ISE works. Copying and pasting into the console I got this

    PS> $x = 2
    PS>
    PS> if ($x -eq 2){
    >> 'yay'
    >> }
    yay
    PS> else {
    >> 'no'
    >> }
    else : The term 'else' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
    the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + else {
    + ~~~~
        + CategoryInfo          : ObjectNotFound: (else:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException
    

    saving it as a script also works

    PS> Get-Content .\test.ps1
    $x = 2
    
    if ($x -eq 2){
    'yay'
    }
    else {
    'no'
    }
    PS> .\test.ps1
    yay
    

    To be safe put the else on the same line as the } immediately before it

    $x = 2
    
    if ($x -eq 2)
    {
    'yay'
    }else {
    'no'
    }
    
    Also there's no connect between you if statement and the proceeding line and the out-null does nothing
    Your code probably wants to be something like
    
    Get-ChildItem -Path "C:\Program Files (x86)" -directory | 
    foreach {
    if ($_.FullName -Like "*AT&T Global*") {
    Execute-Process -Path "$dirSupportFiles\agnc.exe" -Parameters "/S /v/qn /uninstall /norestart"
    } Else {
    Out-Null
    }
    
    }
    

    I'm presuming your looking for the directory and uninstalling the software if its present

    • #99840

      Matthew
      Participant

      Richard,

      I tried your script and it worked perfectly for me! Thank you!

      I also learned a little more about PowerShell.

      Thanks again, great forum!

You must be logged in to reply to this topic.