lundi 23 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()

}


UPDATE:


When I print $_ I see this in the editor:



Exception calling "SelfServiceCreateSite" with "11" argument(s): "Another site already exists at http://ift.tt/1MZU55q. Delete th
is site before attempting to create a new site with the same URL, choose a new URL, or create a new inclusion at the path you originally specified."
At C:\Users\myusername\Desktop\CreateProjectSiteOriginal.ps1:174 char:54
+ $newSite = $webApp.Sites[0].SelfServiceCreateSite <<<< ($ProjectSiteUrl, $ProjectSiteName, $item["srProjectDescription"], 1033, "{F90E6CBE-49BE-4F78-A937
-191DD04BB5B3}#WTProjectSite", $Owner, "PortalSupport", "PortalSupport@mycompany.nl", $Owner, "PortalSupport", "PortalSupport@mycompany.nl")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException


Like you see here, is the message and a little bit of the stackerror inclusive the line number.


When I print $_.Exception I see this:



Exception calling "SelfServiceCreateSite" with "11" argument(s): "Another site already exists at http://ift.tt/1MZU55q. Delete th
is site before attempting to create a new site with the same URL, choose a new URL, or create a new inclusion at the path you originally specified."


Here I missing the stackerror and the line number.


When I print $_.Exception.Message I see:



Exception calling "SelfServiceCreateSite" with "11" argument(s): "Another site already exists at http://ift.tt/1MZU55q. Delete th
is site before attempting to create a new site with the same URL, choose a new URL, or create a new inclusion at the path you originally specified."


Here I missing the stackerror and the line number.


When I print $_.Exception.Stacktrace I see:



at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[] origina
lArguments)
at System.Management.Automation.ParserOps.CallMethod(Token token, Object target, String methodName, Object[] paramArray, Boolean callStatic, Object valueToS
et)
at System.Management.Automation.MethodCallNode.InvokeMethod(Object target, Object[] arguments, Object value)
at System.Management.Automation.MethodCallNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
at System.Management.Automation.AssignmentStatementNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)
at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionCo
ntext context)


This is another stackerror than I see in my fist print. I would like to have the stackerror like my first print. Also here I am missing the linenumber.


How can I get the message, stacktrace and linenumber from the exception?








0 commentaires:

Enregistrer un commentaire