Snippet for running scripts manually / automatically
If the script was started with the parameter “Automatic”, then it runs automatically, otherwise in “Manual” mode:
param(
[Parameter](Mandatory = $false, Position = 0)]
[string]$RunningMode
)
if( ($RunningMode -eq $null) -or ($RunningMode -ne "Automatic") )
{
$RunningMode = "Manual"
}
# extra tip: if you want to run the script on specific computers automatically,
# then you can do so with this:
if( $env:COMPUTERNAME.ToUpper().StartsWith("MYSRV") )
{
# it will run automatically on MYSRV1, MYSRV2 etc.
$RunningMode = "Automatic"
}
# getting the correct path:
# (for correct results you have to save your script first!)
if( $RunningMode -eq "Manual" )
{
$myPath = $psISE.CurrentFile.FullPath
#$BaseDir = up one level (= parent dir)
$BaseDir = (Get-Item $myPath).Directory.FullName
}
if( $RunningMode -eq "Automatic" )
{
$myPath = $PSScriptRoot + "\" + $MyInvocation.MyCommand.Name
#$BaseDir = up one level (= parent dir)
$BaseDir = $PSScriptRoot
}
PowerShellIt can be used at the beginning of a script. For me it is an easy way to determine if a script runs automatically or manually. Check this post.

