PoshCode Archive  Artifact [6565a8abb7]

Artifact 6565a8abb7470d0d81aafbaa3ca6484176aa9ef956f8cef6abb8242f74a11162:

  • File Console-Function-Run.ps1 — part of check-in [83f3fb3918] at 2018-06-10 13:20:13 on branch trunk — This is the Console version of the ISE Run-Script function posted earlier. Typing ‘Run example’ from the command line will run and log the start and end times of ‘example.ps1’ (user: Archdeacon size: 2558)

# encoding: ascii
# api: powershell
# title: Console Function Run
# description: This is the Console version of the ISE Run-Script function posted earlier. Typing ‘Run example’ from the command line will run and log the start and end times of ‘example.ps1’
# version: 0.1
# type: function
# author: Archdeacon
# license: CC0
# x-poshcode-id: 3087
# x-archived: 2012-01-14T07:04:35
# x-published: 2012-12-09T23:02:00
#
#
function Run ([String]$scriptName = '-BLANK-') {
<# The next function records any running scripts started in the console
   session (from $pwd) in the Scripts Event Log.
   It should be placed in the Console $profile. Scripts should be started
   by typing 'Run example' to capture example.ps1, for example. 
   The default logfile is that used by the Windows Script Monitor Service, 
   available from www.SeaStarDevelopment.Bravehost.com
#>   
    if ($host.Name -ne 'ConsoleHost') {
       return
    }
    $logfile = "$env:programfiles\Sea Star Development\" + 
        "Script Monitor Service\ScriptMon.txt"
    $parms  = $myInvocation.Line -replace "run(\s+)$scriptName(\s*)"
    $script = $scriptName -replace "\.ps1\b"       #Replace from word end only.          
    $script = $script + ".ps1"
    if (Test-Path $pwd\$script) {
        if(!(Test-Path Variable:\Session.Script.Job)) {
            Set-Variable Session.Script.Job -value 1 -scope global `
                -description "Script counter"
        }
        $Job    = Get-Variable -name Session.Script.Job
        $number = $job.Value.ToString().PadLeft(4,'0')
        $startTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
        $tag  = "$startTime [$script] start. --> $($myInvocation.Line)"
        if (Test-Path $logfile) {
            $tag | Out-File $logfile -encoding 'Default' -Append
        }
        Write-EventLog -Logname Scripts -Source Monitor -EntryType Information -EventID 2 -Category 002 -Message "Script Job: $script (PS$number) started."
        Invoke-Expression -command "$pwd\$script $parms"
        $endTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
        $tag  = "$endTime [$script] ended. --> $($myInvocation.Line)"
        if (Test-Path $logfile) {
            $tag | Out-File $logfile -encoding 'Default' -Append
        }
        Write-Eventlog -Logname Scripts -Source Monitor -EntryType Information -EventID 1 -Category 001 -Message "Script Job: $script (PS$number) ended."
        $job.Value += 1 
    }
    else {
        Write-Error "$pwd\$script does not exist."
    }
}