locked
Try not catching Invoke-Expression error RRS feed

  • Question

  • Hi

    I have a script which runs (Invoke-expression "svn.exe info svn://xxx.xxx.xxx/branch/x") to check fo the presence of a directory. This is placed within a Try/Catch structure.

    It works fine when the dir exists, but fails with an SVN error when I indicate an erronous dir name

    The trouble is that Try doesn't catch this error, even though $ErrorActionPreference is set to STOP

    In fact when I output the value of $Error[0] just after the previous command I realise that Powershell doesn't actually detect any error.

    I sense a lack of consistency somewhere in Powershell's functionality.

    I previously had problems, well actually I still do, with zipping an entire folder (rather than it's individual contents) when executed from a script. What I mean here is that the same code works fine when executed within POSH ISE.

    The Try/Catch codee works perfectly when executed from within POSH ISE, but not from when in a script that is executed with (Powershell.exe -file c:\script.ps1)

    any Ideas ?

    thanks

     

    yann

     

     

     

    Wednesday, December 14, 2011 9:51 AM

Answers

  • Try this:
    Try{
        $Exp = "svn.exe info $URL_SVN/$Version --username xxxxx --password xxxxx --no-auth-cache"
        $er = (invoke-expression $Exp) 2>&1
    	if ($lastexitcode) {throw $er}
        }
    Catch{
            Add-Content $log "$TimeStamp - $_"
            Break
         }
    

    Wednesday, December 14, 2011 10:22 AM

All replies

  • Please post your troublesome code so we can see what the problem might be.
    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    Wednesday, December 14, 2011 9:57 AM
  • Try this:
    Try{
        $Exp = "svn.exe info $URL_SVN/$Version --username xxxxx --password xxxxx --no-auth-cache"
        $er = (invoke-expression $Exp) 2>&1
    	if ($lastexitcode) {throw $er}
        }
    Catch{
            Add-Content $log "$TimeStamp - $_"
            Break
         }
    

    Wednesday, December 14, 2011 10:22 AM
  • Cmd calling script:

    powershell.exe -NoProfile -inputFormat none -File "C:\Temp\svntest.ps1"

    Script:

    PARAM(   
        [String]$Version      = "x.x.x",
        [string]$URL_SVN      = "SVN://xxx.xxx.xxx.xxx/branch",        
        [string]$ZipFolder    = "c:\test"
        )
       
    $TimeStamp = Get-Date -format yyyyMMddHHmmss

      $Error.Clear()  
      $ErrTimeStamp = Get-Date -format yyyyMMdd
      $log = $ZipFolder + "\log\" + $ErrTimeStamp + ".log"
      $ErrorActionPreference = "Stop"
     
        Try{
            $Exp = "svn.exe info $URL_SVN/$Version --username xxxxx --password xxxxx --no-auth-cache"
            invoke-expression $Exp
        }Catch{
            Add-Content $log "$TimeStamp - $Error[0]"
            Break
        }

    Wednesday, December 14, 2011 10:24 AM
  • This demo code may help to explain the behaviour your are getting:

     

    Try{
      $exp = "7z afh"
      Invoke-expression $Exp
    
     if (!($lastExitCode) -eq 0) { Write-Host "Application generated error" -ForegroundColor Red }
      Invoke-Expression ajfklj
      }
    Catch{
      Write-Host "Invoke Expression failed" -ForegroundColor Red
      }
    

     


    ([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')
    • Edited by Bigteddy Wednesday, December 14, 2011 10:30 AM
    Wednesday, December 14, 2011 10:24 AM
  • Spot on ! It worked

     

    cheers Kazun

    Wednesday, December 14, 2011 11:06 AM
  • I guess it boils down to the standard way by which POSH detects errors

    I read somewhere that POSH detects errors from a stream rather than traditional error codes

    thanks BigTeddy

    Wednesday, December 14, 2011 11:09 AM