powershell - Script to run an exe with parameters and catch exit code -


i have import tool has support running in unattended mode. accepts arguments this:

importer.exe -organization "dev" -datafile "e:\importdata.txt" -rightsfile "e:\importrights.txt" -logfile "c:\logfile.log"

now above how tool accepts arguments.

i'm writing ps-script launch tool above parameters.

<#     .synopsis     executes pwr bulk data importer tool in unattended mode.      .description     script executes pwr bulk data importer tool in unattended mode. import files: data , rights must supplied , log file must provided.      .parameter importtoolexe     full path , exe of tool.      .parameter organization     identifier of organization.      .parameter datafile     full path , filename of data file.      .parameter rightsfile     full path , filename of rights file.      .parameter logfile     full path , filename of log file (will created , if exists, it'll over-writtien).      .parameter isforced     if true, tool run in override mode omitting deletion warnings.      .example     executer -importtoolexe "d:\tool\pwr bulk data importer.exe" -organization "vtdev" -datafile "e:\importdata.txt" -rightsfile "e:\importrights.txt" -logfile "c:\logfile.log"     executer -importtoolexe "d:\tool\pwr bulk data importer.exe" -organization "vtdev" -datafile "e:\importdata.txt" -rightsfile "e:\importrights.txt" -logfile "c:\logfile.log" -isforced true #>  param(     [parameter(mandatory = $true, position = 0, valuefrompipelinebypropertyname = $true)]     [string]$importtoolexe,      [parameter(mandatory = $true, position = 1, valuefrompipelinebypropertyname = $true)]     [string]$organization,      [parameter(mandatory = $true, position = 2, valuefrompipelinebypropertyname = $true)]     [string]$datafile,      [parameter(mandatory = $true, position = 3, valuefrompipelinebypropertyname = $true)]     [string]$rightsfile,      [parameter(mandatory = $true, position = 4, valuefrompipelinebypropertyname = $true)]     [string]$logfile,      [parameter(mandatory = $false)]     [bool]$isforced )  write-output "" write-output "script execute bulk data importer" write-output ""  $params = "-organization " + $organization + " -datafile " + $datafile + " -rightsfile " + $rightsfile + " -logfile " + $logfile  write-output "debuging" write-output ($importtoolexe + " " + $params)  try {     write-output " "     write-output "executing..."     invoke-expression ($importtoolexe + " " + $params)      write-output "finished."     write-output "checking exit code."      } catch [system.exception] {     " "     "exception while trying execute"     write-output $_.exception.gettype().fullname;      write-output $_.exception.message;     return } {     write-output " " }  $isimportsuccess = $false  if ($lastexitcode -eq 0) {     write-output "import successful."     $isimportsuccess = $true } else {     write-output "import failed."     $isimportsuccess = $false }  if ($isimportsuccess -eq $true) {     try     {         $smtpserver = "smtp.gmail.com"          $smtpclient = new-object net.mail.smtpclient( $smtpserver, 587 )           $smtpclient.enablessl = $true          $smtpclient.credentials = new-object system.net.networkcredential( "gmail_username", "gmail_password" );            $emailmessage = new-object system.net.mail.mailmessage         $emailmessage.from = $emailfrom         foreach ( $recipient in $arry_emailto )         {             $emailmessage.to.add( $recipient )         }         $emailmessage.subject = $emailsubj         $emailmessage.body = $emailbody          # have attachments?         # if yes, add them, if not, nothing         # if ( $arry_emailattachments.count -ne $null )          # {         #   $emailmessage.attachments.add()         # }          $emailmessage.attachments.add($logfile)          $smtpclient.send( $emailmessage )     }     catch [system.exception]     {         " "         "exception while emailing"         write-host $_.exception.gettype().fullname;          write-host $_.exception.message;         return     } } 

no i'm getting output errors:

script execute bulk data importer  debuging d:\tool\pwr bulk data importer.exe -organization vtdev -datafile e:\importdata.txt -rightsfile e:\importrights.txt -logfile c:\logfile.log  executing...  exception while trying execute system.management.automation.commandnotfoundexception term 'd:\tool\pwr' not recognized name of cmdlet, function, script file, or operable program. check spelling of name, or if path included, verify path correct , try again. 

i'm facing bit of learning curve ps , had script reading so far.

i see main issue print debug line: quotes gone already. , manager told me invoke-expression not idea. recommends me use start-something

now i'm stuck. pointer appreciated , there upvotes well.

add -erroraction stop inside try/catch, should trigger desired action:

try{     ...     invoke-expression ($importtoolexe + " " + $params) -erroraction stop     ... } catch{     ## catch code } 

you can try invoke-command start exe in scenario replace invoke-expression following:

invoke-command -scriptblock {&$importtoolexe $args[0]} -argumentlist $params 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -