PoshCode Archive  Artifact [d29b4bc4c5]

Artifact d29b4bc4c5478f915aee82796a4ed309d41b86d3a18d46a5b127f1857fc90a4c:

  • File Function-Run-Script.ps1 — part of check-in [234b120e49] at 2018-06-10 13:20:15 on branch trunk — This function should be included in the PowerShell ISE profile.ps1 and it will display the start and end times of any scripts started clicking ‘Run Script’ (or ALT+R) in the Add-ons Menu; additionally they will be logged to the Scripts Event Log (which needs creating first) and also to a text log file. This defaults to that created by the Windows Script Monitor Service (available from www.SeaStarDevelopment.Bravehost.com) which normally indicates the full command line used to start each script. (user: Archdeacon size: 2331)

# encoding: ascii
# api: powershell
# title: Function Run-Script
# description: This function should be included in the PowerShell ISE profile.ps1 and it will display the start and end times of any scripts started clicking ‘Run Script’ (or ALT+R) in the Add-ons Menu; additionally they will be logged to the Scripts Event Log (which needs creating first) and also to a text log file. This defaults to that created by the Windows Script Monitor Service (available from www.SeaStarDevelopment.Bravehost.com) which normally indicates the full command line used to start each script.
# version: 0.1
# type: function
# author: Archdeacon
# license: CC0
# function: Run-Script
# x-poshcode-id: 3093
# x-derived-from-id: 3094
# x-archived: 2015-04-27T06:53:23
# x-published: 2012-12-13T20:30:00
#
#
function Run-Script {
   if ($psISE.CurrentFile.DisplayName.StartsWith("Untitled")) {
      return
   }
   $script = $psISE.CurrentFile.DisplayName
   $psISE.CurrentFile.Save()
   $logfile = "$env:programfiles\Sea Star Development\" + 
        "Script Monitor Service\ScriptMon.txt"                   #Change to suit.        
   if (!(Test-Path env:\JobCount)) {
      $env:JobCount = 1                #This will work across multi Tab sessions.
   }
   $number = $env:JobCount.PadLeft(4,'0')
   $startTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
   $tag  = "$startTime [$script] start. --> PSE $($myInvocation.Line)"
   if (Test-Path $logfile) {
       $tag | Out-File $logfile -encoding 'Default' -Append
   }
   "$startTime [$script] started."
   Write-EventLog -Logname Scripts -Source Monitor -EntryType Information -EventID 2 -Category 002 -Message "Script Job: $script (PSE$number) started."
   Invoke-Command -Scriptblock { & "$pwd\$script" }
   $endTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
   $tag  = "$endTime [$script] ended. --> PSE $($myInvocation.Line)"
   if (Test-Path $logfile) {
      $tag | Out-File $logfile -encoding 'Default' -Append
   }
   "$endTime [$script] ended."
   Write-Eventlog -Logname Scripts -Source Monitor -EntryType Information -EventID 1 -Category 001 -Message "Script Job: $script (PSE$number) ended."
   $env:JobCount = [int]$env:JobCount+1
}

$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Run Script",{Run-Script}, "Alt+R") | Out-Null