Functions/Add-WITErrorLog.ps1
<# .Synopsis Loggar Error med tid och datum .DESCRIPTION Dessa måste vara definierade och activerade $global:dbConn $global:adOpendynamic $global:adLockOptimistic eller $recordset med errorrecordset .EXAMPLE Add-WitTEDErrorLog -Errorplace $vad -Errorobject $Error[0] -ErrorWorkdataxml $Object.InnerXml -ErrorDescription $Description #> function Add-WitErrorLog { [CmdletBinding()] [Alias()] Param ( # Param1 help description [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] $Errorplace, $Errorobject, $ErrorDescription, $ErrorWorkdataXML, $recordset ) Begin { } Process { write-host "Caught an exception in $Errorplace :" -ForegroundColor Red if ($Errorobject.length -ge 1) { write-host "Exception Message: $($Errorobject.Exception.Message)" -ForegroundColor Red write-host "Exception Message: $($Errorobject.invocationinfo.Positionmessage)" -ForegroundColor Red #write-host "På PersonID: $($global:Personid)" -ForegroundColor Red } $query="Select top(0) * FROM [dbo].[Log_Errors]" $rs_Errorfile = new-object -comobject ADODB.Recordset; if ($recordset -eq $null) { $rs_Errorfile.open($query, $global:dbConn,$global:adOpendynamic,$global:adLockOptimistic); } else { $rs_Errorfile=$recordset } $rs_Errorfile.addnew() $rs_Errorfile.Fields.Item("ErrorDescription").Value ="$ErrorDescription" $rs_Errorfile.Fields.Item("ErrordataXML").Value ="$ErrorWorkdataXML" if ($Errorobject.length -ge 1) { $rs_Errorfile.Fields.Item("Errorline").Value ="$($Errorobject.invocationinfo.Positionmessage)" $rs_Errorfile.Fields.Item("Errortxt").Value ="$($Errorobject.Exception.Message)" $rs_Errorfile.Fields.Item("ErrorType").Value ="$Errorplace" } $rs_Errorfile.Fields.Item("TimeStamp").Value =$(Get-Date -Format g) $rs_Errorfile.update() if ($recordset -eq $null) { $rs_Errorfile.close() } # } #Process End { } } |