Thursday, 15 April 2010

How would I go about error handling with a Powershell object in c#? -



How would I go about error handling with a Powershell object in c#? -

i have short powershell script:

$spname = $args[0] $filterlist = $args[1] $destinationfile = $args[2] $server = $args[3] $user = $args[4] $password = $args[5] bcp "exec $spname @filterlist = $filterlist" queryout "$destinationfile" -c -t"\0" -s $server -u $user -p $password

i using powershell object in .net add together arguments , phone call invoke method. had problem, though, when password wrong. locked db down. much embarrassment. @ no point, however, exception thrown. know .net pipeline object has error method returns collection of errors, don't know how implement pipeline script. there command bcp? add together script command? otherwise, there way implement error handling powershell object? help guys , gals can offer appreciated.

for error handling in powershell, there try/catch mechanism similar c#'s can implement.

however, works of powershell exception thrown along way. in case uncertainty bcp thrown exceptions, why script finished without error. instead you'll need rely on whatever error trapping mechanism bcp supports. in case there couple of parameters useful:

-m max_errors

specifies maximum number of syntax errors , compilation errors can occur before mass re-create operation canceled.

-e err_file

specifies total path of error file used store rows bcp unable transfer file database.

in addition, if bcp sets error code on exit, many old-style command line utilities do, there pair of pre-defined powershell variables hold result of lastly command executed:

$lastexitcode # holds actual error code $? # holds $true if $lastexitcode 0, $false otherwise.

c# powershell

No comments:

Post a Comment