Use PowerShell To Log Custom Events from XenDesktop, XenApp, or Hyper-V
I have been rewriting some of the scripts I had on www.tokeshi.com into PowerShell.
I recently discovered that I am using a old not recommended function and variable naming convention. When I started doing vbscript I tried to use a very scrict Hungarian Notation and Pascal Case. That is, I would name a string $strStringname. Bad form, old scripters do that. Scripters that read pulp-based newspaper and use X-jack 14.4 modems for dialup. (Back when dinosaurs ruled the earth, hunters would kill a mastodon and return to their cave and post on a BBS to record their accomplishment. They did not have broadband, Facebook, or a public internet.)

Anyway, my apologies for my bad form, I am in the process of re-writing my scripts and will repost this once I correct my Pascal Case with Camel Case, use proper verb-noun function names etc.

Anyway. This script creates a function that will allow you to 1. Get the current line number. This is cool if you want to do some debugging. This function is called GLN for Get Line Number should be GetLineNumber but I was trying to make is less keystrokes to get to a error message.

The second part of the script allows you to set a global variable called $GlobalLogLevel (See Pascal case, should be $globalLogLevel) $GlobalLogLevel can be equal to E, T, and or H or any combination.

This is an easy way to add logging to your scipts.
Add the Functions below to your scripts and you can activate a variety of logging options.
useage is as follows:
fnLog
or
fnLog 88 # Example is 88 but can include any integer up to 2^8
fnLog 88 “Custom Log Message Second Parameter”
fnLog 88 “Custom Log Message Second Parameter” “ETH”

All 5 of the fnLog parameters are: LogEventID, CustomLogMessage, LogLevel, LogSource, LogName
The LogLevel directs log to (E)ventLog or (T)extLog or (h)ost(S)creen or combination
or
$L = GLN; fnLog

  1. This can include the above parameters but it also inserts the Script Line number into the log as well.

Anyway, if you insert this function in, you can have some debug logging that only occurs where and when you need it when you write the script, but allows you to set a global ON OFF when you need more detailed debugging later.

Here it is. FunctionLogs.ps1

  1. ==============================================================================================
    #
  2. Microsoft PowerShell Source File – Created with SAPIEN Technologies PrimalScript 2009
    #
  3. NAME: FunctionLog
    #
  4. AUTHOR: royt ,
  5. DATE : 10/18/2010
    #
  6. COMMENT: This is an easy way to add logging to your scipts.
  7. Add the Functions below to your scripts and you can activate a variety of logging options.
  8. useage is as follows:
  9. fnLog
  10. or
  11. fnLog 88 # Example is 88 but can include any integer up to 2^8
  12. fnLog 88 “Custom Log Message Second Parameter”
  13. fnLog 88 “Custom Log Message Second Parameter” “ETH”
    #
  14. All 5 of the fnLog parameters are: LogEventID, CustomLogMessage, LogLevel, LogSource, LogName,
  15. The LogLevel directs log to (E)ventLog or (T)extLog or (h)ost(S)creen or combination
  16. or
  17. $L = GLN; fnLog
  18. # This can include the above parameters but it also inserts the Script Line number into the log as well.
    #
  19. ==============================================================================================
  1. $GlobalLogLevel = “eth”

function fnLog
{param ($strLogEventID = $L, $strLogMessage = “Status message from:”, $LogLevel= “e”, $strLogSource = “Tokeshi.com”, $strLogName = “Application”)

  1. Uncomment and run the following line once as admin
  2. New-EventLog -source $strLogSource -logname Application
  3. Un-comment one of the log levels below or set in third param of fnLog
  4. $LogLevel = “E” # Log to Event Log
  5. $LogLevel = “T” # Log to Text Event Log
  6. $LogLevel = “H” # Log to Host or Screen
  7. $LogLevel = “ETH” # Log to All, Combos work: ET Log to Text and Event, EH Log to Screen and Event
  1. IF $GlobalLogLevel set (to turn on logging in all parts of the script) then set $LogLevel
    if (!($GlobalLogLevel -eq $null))
    {$LogLevel = $GlobalLogLevel; Write-Host $LogLevel}
    else
    Unknown macro: {# No Global override for $LogLevel in place
    }

    if ($L -eq $null)
    {$L = “Undefined”}

  1. If $numLine is NULL then replace with $L
    if ($numLine -eq $null)
    {$numLine = $L}
    else
    Unknown macro: {# $numLine is set
    }

$strScriptName = GetFileName
$arrLogLevel = $LogLevel.ToUpper().ToCharArray()
if ($arrLogLevel -contains “E”)

Unknown macro: {Write-EventLog -LogName "$strLogName" -Source $strLogSource -EventID $strLogEventID -Message "$strLogMessage`n$strScriptName `nLine Number}

if ($arrLogLevel -contains “T”)
{
$strLogPath = “$env:temp\Tokeshi”
$Date = Get-Date
if (!(Test-Path -path “$strLogPath”))

Unknown macro: {
[IO.Directory]}

if (!(Test-Path -Path “$strLogPath\Script.log”))

Unknown macro: {
New-Item -ItemType file "$strLogPathScript.log"
Add-Content -Path $strLogPathScript.log ".LOG"
}

Add-Content Path $strLogPath\Script.log “$Date EventID: $strLogEventID- Message: $strLogMessage- Script at path: $strScriptName- Line Number: $numLine- ProcessID: $PID- Running on $ShellID`n”
}
if ($arrLogLevel -contains “H” -or $arrLogLevel -contains “S”)

Unknown macro: {Write-Host EventID}

}
function GLN

Unknown macro: {
$MyInvocation.ScriptLineNumber
$numLine = $MyInvocation.ScriptLineNumber
}

New-Alias -Name _LINE_ -Value Get-CurrentLineNumber -Description ‘Returns the current line number in a PowerShell script file.’
function GetFileName

Unknown macro: {
$MyInvocation.ScriptName
}

New-Alias -Name _FILE_ -Value GetFileName -Description ‘Returns the name of the current PowerShell script file.’

  1. $L = GLN; fnLog
    $L = GLN; fnLog[Use PowerShell To Log Custom Events from XenDesktop, XenApp, or Hyper-V

Thanks again and happy scripting!