PoshCode Logo PowerShell Code Repository

Function Run-Script by Archdeacon 3 months ago (modification of post by Archdeacon view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6733"></script>download | new post

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.

  1. #################################################################################
  2. # This function should be included in the PowerShell ISE profile.ps1 and it will
  3. # display the start and end times of any scripts started by clicking 'Run Script'
  4. # in the Add-ons Menu, or F2; additionally they will be logged to the Scripts
  5. # Event Log (which needs creating first) and also to a text log file. This
  6. # defaults to that created by the Windows Script Monitor Service (available from
  7. # www.SeaStar.co.nf) which normally indicates the full command line used to start
  8. # each script.
  9. # The source directory of any script must always be the current '$pwd'.
  10. # V2.0 Use Try/Catch to trap (child) script errors & change Hotkey to F2.
  11. # v3.1 Arguments entered on the command line will now be passed to the script.
  12. # v3.2 Change Event Log 'category' to 2 for ISE run scripts.
  13. #################################################################################
  14.  
  15. function Run-Script {
  16.    if ($host.Name -ne 'Windows PowerShell ISE Host' )) {
  17.       return
  18.    }
  19.    $script = $psISE.CurrentFile.DisplayName
  20.    if ($script.StartsWith("Untitled") -or $script.Contains("profile.") {
  21.       return
  22.    }
  23.    $psISE.CurrentFile.Save()
  24.    $logfile = "$env:programfiles\Sea Star Development\" +
  25.         "Script Monitor Service\ScriptMon.txt"                   #Change to suit.        
  26.    if (!(Test-Path env:\JobCount)) {
  27.       $env:JobCount = 1                #This will work across multi Tab sessions.
  28.    }
  29.    $number = $env:JobCount.PadLeft(4,'0')
  30.    $startTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
  31.    #Now grab any parameters entered from the command line...
  32.    $parms = $psISE.CurrentPowerShellTab.CommandPane.Text
  33.    $tag  = "$startTime [$script] start. --> PSE $($myInvocation.Line) $pwd\$script $parms"
  34.    if (Test-Path $logfile) {
  35.        $tag | Out-File $logfile -encoding 'Default' -Append
  36.    }
  37.    "$startTime [$script] started."
  38.    Write-EventLog -Logname Scripts -Source Monitor -EntryType Information -EventID 2 -Category 002 -Message "Script Job: $script (PSE$number) started."
  39.    try {
  40.       Invoke-Expression -Command "$pwd\$script $parms"
  41.    }
  42.    catch {
  43.       Write-Host -ForegroundColor Red ">>> ERROR: $_"
  44.    }
  45.    $endTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
  46.    $tag  = "$endTime [$script] ended. --> PSE $($myInvocation.Line) $pwd\$script $parms"
  47.    if (Test-Path $logfile) {
  48.       $tag | Out-File $logfile -encoding 'Default' -Append
  49.    }
  50.    "$endTime [$script] ended."
  51.    Write-Eventlog -Logname Scripts -Source Monitor -EntryType Information -EventID 1 -Category 002 -Message "Script Job: $script (PSE$number) ended."
  52.    $env:JobCount = [int]$env:JobCount+1
  53. }
  54.  
  55. $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Run Script",{Run-Script}, "F2") | Out-Null

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me