Silently handle errors in powershell -


i writing troubleshooting script determine ip addresses in our domain accessible wmi, , not. script read list of input parameters (about 18,000 lines), , output file ip address , username

ip address, username 

problem is, when wmi error thrown, writes file

ip address, get-wmiobject : rpc server unavailable. .....numerous lines of error 

i make such when wmi error thrown, writes following

ip address, "wmi error" 

and here modified code reference

#script_modified.ps1  $abc = $args $startinfo = $null $process = $null $standardout = $null   <#previously created password file in c:\script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file c:\script\cred.txt#> $password = get-content c:\script\cred.txt | convertto-securestring  $a = get-content "c:\script\test_input.txt"  foreach ($b in $a){      $startinfo = new-object system.diagnostics.processstartinfo     $startinfo.filename = "powershell.exe"     $startinfo.arguments = "c:\script\script2.ps1 " + $b      $startinfo.redirectstandardoutput = $true     $startinfo.useshellexecute = $false     $startinfo.createnowindow = $false     $startinfo.username = "service.infosec"     $startinfo.domain = "central"     $startinfo.password = $password       $process = new-object system.diagnostics.process     $process.startinfo = $startinfo     $process.start() | out-null     $standardout = $process.standardoutput.readtoend()     $process.waitforexit()      # $standardout should contain results of "c:\script\script2.ps1"     add-content c:\script\list_of_computers_in_domain.log $b","$standardout } 

edit

@hyper anthony

i updated code following

try{     $process.startinfo = $startinfo } catch{     $message = "wmi error" } finally{     $process.waitforexit()     add-content c:\script\list_of_computers_in_domain.log $b","$message } 

and following errors:

exception calling "waitforexit" "0" argument(s): "no process associated object." @ c:\script\script_modified.ps1:36 char:29 +         $process.waitforexit <<<< ()     + categoryinfo          : notspecified: (:) [], methodinvocationexception     + fullyqualifiederrorid : dotnetmethodexception 

how fix?

edit:

below updated code:

foreach ($b in $a){      $startinfo = new-object system.diagnostics.processstartinfo     $startinfo.filename = "powershell.exe"      ...more code...      $process = new-object system.diagnostics.process     $process.startinfo = $startinfo      try{         $process.start() | out-null         $standardout = $process.standardoutput.readtoend()     }     catch{         $standardout = "wmi error"     }     finally{         $process.waitforexit()         add-content c:\script\list_of_computers_in_domain.log $b","$standardout     }  } 

there no longer errors output console, but, output file not wish.

when there wmi error, following line written

'sender-ip=10.10.10.10', wmi error

but instead, following written

'sender-ip=10.10.10.10',get-wmiobject : rpc server unavailable. (exception hresult: 0x800706ba) ...many lines of error

or other error may printed instead of wmi error.

thanks once again!


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -