## p4start-scriptinglog ## Aaron Bockelie function p4start-scriptinglog { #uniquify path has a bug when run on root of a drive. if (!$env:depotsxml) { write-error "Could not locate depots.xml configuration file." break } ## make this a switch case instead of fallthrough if ($MyInvocation.MyCommand.name -match "checkpoint") #if this is a checkpoint invocation { $static = ([xml](gc $env:depotsxml)).perforce.checkpointService $p4static = (([xml](gc $env:depotsxml)).perforce.depot | ?{$_.name -eq $depot}).checkpointPrefs } if ($MyInvocation.MyCommand.name -match "sync")#if this is a sync invocation { $static = ([xml](gc $env:depotsxml)).perforce.syncService $p4static = (([xml](gc $env:depotsxml)).perforce.depot | ?{$_.name -eq $depot}).syncPrefs } if ($MyInvocation.MyCommand.name -match "watchdog")#if this is a watchdog invocation { $static = ([xml](gc $env:depotsxml)).perforce.watchdogService $p4static = (([xml](gc $env:depotsxml)).perforce.depot | ?{$_.name -eq $depot}).watchdogPrefs } $global:transcript = (Uniquify-Path("{0}\{1}-{2:yyyy-MM-dd}.log" -f (get-location).path,$static.FilenamePrefix.tostring(),(get-date))) ("Starting transcript to " + $transcript.tostring()) Trap {Continue} Stop-Transcript > $null # in case anything already running via previous command in shell try { Start-Transcript $transcript -append > $null} catch { $transcript = $null } # ISE doesn't support transcripts, ok to ignore } ## p4stop-scriptinglog ## Aaron Bockelie function p4stop-scriptinglog {param($subjectText,[switch]$failed) if (!$env:depotsxml) { write-error "Could not locate depots.xml configuration file." break } ## make this a switch case statement if ($MyInvocation.MyCommand.name -match "checkpoint") #if this is a checkpoint invocation { $static = ([xml](gc $env:depotsxml)).perforce.checkpointService $p4static = (([xml](gc $env:depotsxml)).perforce.depot | ?{$_.name -eq $depot}).checkpointPrefs } if ($MyInvocation.MyCommand.name -match "sync")#if this is a sync invocation { $static = ([xml](gc $env:depotsxml)).perforce.syncService $p4static = (([xml](gc $env:depotsxml)).perforce.depot | ?{$_.name -eq $depot}).syncPrefs } if ($MyInvocation.MyCommand.name -match "watchdog")#if this is a watchdog invocation { $static = ([xml](gc $env:depotsxml)).perforce.watchdogService $p4static = (([xml](gc $env:depotsxml)).perforce.depot | ?{$_.name -eq $depot}).watchdogPrefs } Trap {Continue} Stop-Transcript > $null #stop the transcript $transcriptLines = type $transcript #get the transcript into lines $shortTranscriptLines = $transcriptLines[0..($static.MaxLogSnippetLines-1)] #drop transcript lines after $maxlogsnippetlines if ($transcriptLines.count -ge $static.MaxLogSnippetLines) #if we dropped extra lines, add that to the transcript so we know more happened. { $shortTranscriptLines += "" $shortTranscriptLines += "...Remainder truncated..." } $shortTranscriptText = [string]::join("`r`n", $shortTranscriptLines) #add a carrige return to transcript if (!$subjectText -and !$failed) #if there is no subject text, and failed is false, assume the function correctly happened. Set subjecttext to success, and configure the event for the eventlog. { $subjectText = 'SUCCESS' $eventType = 'Information' $emailpriority = 'Low' $eventId = 38000 } if ($subjectText -and !$failed)#if a subject was defined, and things didn't go badly. { $subjectTest = $subjectTest #this is just here so it's easy to compare against the other states below. $eventType = 'Information' $emailpriority = 'Low' $eventId = 38050 } if (!$subjectText -and $failed) #if there is no subjecttext, and failed flagged as true, set configure for a failure event. { $subjectText = 'FAILURE' $eventType = 'Error' $emailPriority = 'High' $eventId = 38100 } if ($subjectText -and $failed)#if a subject was defined, and things went badly. (it must be really important!) { $subjectTest = $subjectTest #this is just here so it's easy to compare against the other states below. $eventType = 'Error' $emailpriority = 'High' #wish there was an OMG priority. $eventId = 38100 } Send-EventMessage -eventType $eventType -eventID $eventID -EventLogSource $static.EventLogSource -eventMessage $shortTranscriptText #write to event log Send-MailMessage -to $static.P4AdminEmail -subject "Perforce scripting logger: $subjectText" -from $static.P4EmailFrom -body $shortTranscriptText -smtpserver $static.SmtpServer -priority $emailPriority #send out a status email }