vendredi 20 mars 2015

How to write powershell catch error stacktrace to a listitem?



I am using a powershell script where I do some logic. It is a big powershell and is scheduled. I would like to triggered when there occurred some error. I would like to have the whole error stacktrace with the line number and all other detail info. How to do that?



try
{
//some logic
}
catch
{
"error occurred, write the error stacktrace to the loglist where I got an email alert for each new list item"

$Error[0].Exception

$webURL = "http://ift.tt/1AQ0gBG"
$listName = "Logging"

$web = Get-SPWeb $webURL

#Get the SPList object
$list = $web.Lists[$listName]

#Create a new item
$newItem = $list.Items.Add()

#Add properties to this list item
$newItem["Title"] = $item["Title"]
$newItem["Exception"] = $Error[0].Exception
$newItem["srPortalUrlExtranet"] = $item["srPortalUrlExtranet"]
$newItem["srPortalUrl"] = $item["srPortalUrl"]
$newItem["srProjectName"] = $item["srProjectName"]

#Update the object so it gets saved to the list
$newItem.Update()
$web.Dispose()

}







0 commentaires:

Enregistrer un commentaire