AR Powershell Script log

Hi Team.

When running your PS via ARS what are you using to log that the script has run and doing what you expect?

I have been using Start-Transcript and End-Transscript but the issue with that is that it seems to if there is a problem and it cant stop the transscript it  locks the log file up and it cant be deleted until the AR Service is restarted

Just looking for a better solution 

Thank in advance 

Parents
  • I just have my own logging function that writes timestamped entries to a folder on AR server executing the script (see code below).

    I call it thus:

    Logit "Here's the message I want to log"

    I also have a more elaborate version that marks the entries as "[INFO]", "[ERROR]" or "[WARN]" if you supply a "type" parameter when you call the function.

    If you want to turn off some of the logging, it's easy enough to search and replace in the code e.g. replace Logit with #Logit.

    function Logit ($Text,$ScriptName) # Simple function for creating timestamped log entries
    {

    if ($ScriptName -eq ""){$ScriptName = "MyScript"}

    $LogHost = $(([System.Net.Dns]::GetHostByName(($env:computerName))).Hostname)

    # Establish a sub folder and under an existing parent path for script logs on the AR server

    $LogsParentPath = "\\$LogHost\D$\Active_Roles_Work\Script_Logs"

    $LogPath = "$LogsParentPath\$ScriptName\$ScriptName" + "_Log.txt"

    if (!(Test-Path $LogPath))
    {
    $Result = New-Item -Name $ScriptName -ItemType Directory -Path $LogsParentPath
    }

    $Timestamp = $(Get-Date -Format "MM/dd/yyyy hh:mm:ss").ToString()

    $Text = $Timestamp + " " + $Text

    Add-Content -Path $LogPath -Value $Text

    } # End of Logit function declaration

Reply
  • I just have my own logging function that writes timestamped entries to a folder on AR server executing the script (see code below).

    I call it thus:

    Logit "Here's the message I want to log"

    I also have a more elaborate version that marks the entries as "[INFO]", "[ERROR]" or "[WARN]" if you supply a "type" parameter when you call the function.

    If you want to turn off some of the logging, it's easy enough to search and replace in the code e.g. replace Logit with #Logit.

    function Logit ($Text,$ScriptName) # Simple function for creating timestamped log entries
    {

    if ($ScriptName -eq ""){$ScriptName = "MyScript"}

    $LogHost = $(([System.Net.Dns]::GetHostByName(($env:computerName))).Hostname)

    # Establish a sub folder and under an existing parent path for script logs on the AR server

    $LogsParentPath = "\\$LogHost\D$\Active_Roles_Work\Script_Logs"

    $LogPath = "$LogsParentPath\$ScriptName\$ScriptName" + "_Log.txt"

    if (!(Test-Path $LogPath))
    {
    $Result = New-Item -Name $ScriptName -ItemType Directory -Path $LogsParentPath
    }

    $Timestamp = $(Get-Date -Format "MM/dd/yyyy hh:mm:ss").ToString()

    $Text = $Timestamp + " " + $Text

    Add-Content -Path $LogPath -Value $Text

    } # End of Logit function declaration

Children
No Data