# 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.SeaStar.co.nf) which normally indicates the full command line used to start each script.
# version: 2.0
# type: function
# author: Archdeacon
# license: CC0
# function: Run-Script
# x-poshcode-id: 6740
# x-archived: 2017-02-23T01:14:34
# x-published: 2017-02-19T03:07:00
#
#
#################################################################################
# This function should be included in the PowerShell ISE profile.ps1 and it will 
# display the start and end times of any scripts started by clicking 'Run Script'
# in the Add-ons Menu, or F2; 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.SeaStar.co.nf) which normally indicates the full command line used to start 
# each script. 
# The source directory of any script must always be the current '$pwd'.
# V2.0 Use Try/Catch to trap (child) script errors & change Hotkey to F2.
# v3.1 Arguments entered on the command line will now be passed to the script.
# v3.2 Change Event Log 'category' to 2 for ISE run scripts.
#################################################################################

function Run-Script {
 if ($host.Name -ne 'Windows PowerShell ISE Host' ) {
      return
   }
   $script = $psISE.CurrentFile.DisplayName
   if ($script.StartsWith("Untitled") -or $script.Contains("profile.") {
      return
   }
   $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"
   #Now grab any parameters entered from the command line...
   $parms = $psISE.CurrentPowerShellTab.CommandPane.Text
   $tag  = "$startTime [$script] start. --> PSE $($myInvocation.Line) $pwd\$script $parms"
   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."
   try {
      Invoke-Expression -Command "$pwd\$script $parms"
   }
   catch {
      Write-Host -ForegroundColor Red ">>> ERROR: $_"
   }
   $endTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
   $tag  = "$endTime [$script] ended. --> PSE $($myInvocation.Line) $pwd\$script $parms"
   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 002 -Message "Script Job: $script (PSE$number) ended."
   $env:JobCount = [int]$env:JobCount+1
}

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