Automation Example

This is an automation example. This HelloWorldLogs.bat script is not maintained and is not guaranteed to work. Use at your own risk.

Copy
@echo off
PowerShell -ExecutionPolicy Bypass -File "HelloWorldLogs.ps1"
EXIT /B %ERRORLEVEL%

HelloWorldLogs.ps1
<#
.SYNOPSIS
    HelloWorld.PS1

.DESCRIPTION
    This script will pop up a Hello World Window

.INPUTS
    None

.OUTPUTS
    When enabled, a log file will be created in the same location from which the script is run.

.NOTES
    Version:            1.0
    Author:             Lakeside Software
    Requirements:       This script is assumed to be run by the SysTrack agent (local SYSTEM account).
                        This script is NOT maintained and is not guaranteed to work and is meant as an example
                        
                        USE AT YOUR OWN RISK

                        Copyright 2023 LAKESIDE SOFTWARE, INC.

.LINK
    http://www.lakesidesoftware.com
#>

#####
# Begin Functions
#####

    #####
    # Function: LogWrite (Logging)
    #####
    function LogWrite([string]$info)
    {       
        if($script:log -eq $true)
        {   
            # Write to logfile date - message
            "$(get-date -format "yyyy-MM-dd HH:mm:ss") -  $info" >> $logfile
            # Any logged Start or Exit statements also write to host
            If ( ($info.contains("Starting Script")) -or ($info.contains("Exiting Script")) ) {
                Write-Host $info
            }
            # Any logged Warning or Error statements also write to host
            If ( ($info.contains("Error:")) -or ($info.contains("Warning"))) {
                Write-Host "  " $info  "(See Log For Details)"
            }
        }
        else  {
            Write-Host $info
        }
    }              

    #####
    # Function: NewFile (Logging, Location)
    #####
    function NewFile($data, $loc)
    {  
        If (test-path $loc) {Remove-Item $loc}
        "$data" >> $loc  
        LogWrite "Data saved to $loc"
    }   
    
    #####
    # Function: ExitScript
    #####
    function ExitScript($msg, $ExitCode)
    {
        ### Cleanup
        If ($Silent) {
            Write-Progress -Activity "Working..." `
            -Completed -Status $msg
        }
        
        ### Log Exit
        LogWrite "$msg ($ExitCode)"
        LogWrite "Exiting Script..."
        Logwrite ""
        #Exit
        Exit($ExitCode)
    }   

#####
# End Functions
#####

#####
# Advanced Variables
#####   
        #logging
    If (!$Logging) {$Logging = $true}
    $ErrorActionPreference = "Stop"
    $debug = $false
    $retain = $false # new log on each run
    
    $ScriptName = & { $myInvocation.ScriptName }
    $ScriptPath = Split-Path -parent $ScriptName
    $ScriptName = Split-Path $ScriptName -Leaf
    $ScriptName = $ScriptName.Replace(".PS1","")
    
    $logfile = "$ScriptPath\$ScriptName" + "Log.txt"
    $script:log = $Logging
    $Dev = "False"
    
#####
# Start Script
#####
    cls
    
    ### Delete existing log files > 1mb
    If (test-path $logfile) {
        If ($retain -eq $true) {
            If ((Get-Item $logfile).length -gt 1mb) {
                Remove-Item $logfile
            }
        }
        Else {
            Remove-Item $logfile
        }
    }
    
#####
# Start Script
#####

    ### Start Script
    LogWrite ""
    LogWrite "Starting Script..."
    
    $WhoAmI = [Environment]::UserName
    $Is64Bit = [Environment]::Is64BitProcess
    
    LogWrite "Running As:  $WhoAmI"
    LogWrite "64bit Process:  $Is64Bit"
    
    #LogWrite "About to try writing hello world to a file..."
    try{
        LogWrite "Start Hello World! Invoking Forms."
        Add-Type -AssemblyName System.Windows.Forms
        LogWrite "Hello World!!"
    }
    catch{
        LogWrite "Unable to popup Hello World prompt due"
        LogWrite $error[0].FullyQualifiedErrorId
        LogWrite $error[0].Exception.ToString()
        LogWrite $error[0].ScriptStackTrace
        ExitScript -msg "Script Failed" -code 1
    }

#####
# Exit Script
#####
    
    ExitScript -msg "Script Completed" -code 0