lots of improvements to Run-Script and a few new params
This commit is contained in:
@@ -120,17 +120,42 @@ param(
|
|||||||
## Parameters for local scripts
|
## Parameters for local scripts
|
||||||
[Parameter(Mandatory=$true,ParameterSetName='local')]
|
[Parameter(Mandatory=$true,ParameterSetName='local')]
|
||||||
[string]$Path,
|
[string]$Path,
|
||||||
|
#### $NoWait will prevent waiting for the specified script to finish running
|
||||||
|
[Parameter(Mandatory=$false,ParameterSetName='local')]
|
||||||
|
[switch]$NoWait=$false,
|
||||||
## Parameters for all scripts
|
## Parameters for all scripts
|
||||||
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
|
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
|
||||||
[Parameter(Mandatory=$false,ParameterSetName='local')]
|
[Parameter(Mandatory=$false,ParameterSetName='local')]
|
||||||
[string]$Arguments='null',
|
[string]$Arguments='null',
|
||||||
|
#### $HaltIfRunning is a collection of app names which, if running, will prevent the script from running
|
||||||
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
|
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
|
||||||
[Parameter(Mandatory=$false,ParameterSetName='local')]
|
[Parameter(Mandatory=$false,ParameterSetName='local')]
|
||||||
[string[]]$HaltIfRunning
|
[string[]]$HaltIfRunning,
|
||||||
|
#### $ForceClose will forcefully close all apps specified in $HaltIfRunning
|
||||||
|
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
|
||||||
|
[Parameter(Mandatory=$false,ParameterSetName='local')]
|
||||||
|
[switch]$ForceClose=$false
|
||||||
)
|
)
|
||||||
$LogName = $MyInvocation.MyCommand
|
$LogName = $MyInvocation.MyCommand
|
||||||
If ( $Arguments -eq "null" ) { $Arguments = $null }
|
If ( $Arguments -eq "null" ) { $Arguments = $null }
|
||||||
#If (! ($HaltIfRunning) ) { $HaltIfRunning = $null }
|
If ( $Path ) { If (! (Test-Path $Path) ) { Log "The specified script does not exist: ${Path}" -Name $LogName ; Exit } }
|
||||||
|
If ( ($HaltIfRunning) -and ($ForceClose) -and (! (IsAdmin) ) ) { Log "Cannot close dependent apps because this task does not have administrative privileges" -Name $LogName ; Exit }
|
||||||
|
If ( $HaltIfRunning )
|
||||||
|
{
|
||||||
|
$Halt = $false
|
||||||
|
Log "Checking for running instances of the following dependent apps:" -Name $LogName
|
||||||
|
ForEach ( $name in $HaltIfRunning)
|
||||||
|
{
|
||||||
|
$RunningInstances = Get-Process | Where { $_.Name -like "${name}" }
|
||||||
|
If ( $RunningInstances )
|
||||||
|
{
|
||||||
|
If ( $ForceClose -eq $true ) { $name | Stop-Process -Force ; Log " ""${name}"" (force closed)" -Name $LogName }
|
||||||
|
Else { $Halt = $true ; Log " ""${name}"" (running)" -Name $LogName }
|
||||||
|
}
|
||||||
|
Else { Log " ""${name}"" (not running)" -Name $LogName }
|
||||||
|
}
|
||||||
|
If ( $Halt -eq $true ) { Log "Dependent apps are currently in use and are preventing the specified script from running" -Name $LogName ; Exit }
|
||||||
|
}
|
||||||
switch ($PSCmdlet.ParameterSetName)
|
switch ($PSCmdlet.ParameterSetName)
|
||||||
{
|
{
|
||||||
'live-ps'
|
'live-ps'
|
||||||
@@ -148,34 +173,18 @@ switch ($PSCmdlet.ParameterSetName)
|
|||||||
|
|
||||||
'local'
|
'local'
|
||||||
{
|
{
|
||||||
If ( Test-Path $Path )
|
Log "Executing: ${Path} ${Arguments}" -Name $LogName
|
||||||
|
Try
|
||||||
{
|
{
|
||||||
$Halt = $false
|
If ( $NoWait ) { Start-Process "${Path}" -ArgumentList "{$Arguments}" }
|
||||||
If ( $HaltIfRunning )
|
Else
|
||||||
{
|
{
|
||||||
Log "Checking for running instances of the following dependent apps:" -Name $LogName
|
Log " Waiting for script to finish..." -Name $LogName
|
||||||
ForEach ( $name in $HaltIfRunning)
|
Start-Process "${Path}" -ArgumentList "{$Arguments}" -Wait
|
||||||
{
|
Log " Finished" -Name $LogName
|
||||||
$RunningInstances = Get-Process | Where { $_.Name -like "${name}" }
|
|
||||||
If ( $RunningInstances )
|
|
||||||
{
|
|
||||||
$Halt = $true
|
|
||||||
Log " ""${name}"" (running)" -Name $LogName
|
|
||||||
}
|
|
||||||
Else { Log " ""${name}""" -Name $LogName }
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
If ( $Halt -eq $false )
|
|
||||||
{
|
|
||||||
Log "Executing: ${Path} ${Arguments}" -Name $LogName
|
|
||||||
Try { Start-Process "${Path}" -ArgumentList "{$Arguments}" -Wait }
|
|
||||||
Catch { $_.Exception.Message | Log -Name $LogName }
|
|
||||||
}
|
|
||||||
Else { Log "Dependent apps are currently in use and are preventing the specified script from running" -Name $LogName }
|
|
||||||
}
|
}
|
||||||
Else { Log "The specified script does not exist: ${Path}" -Name $LogName }
|
Catch { $_.Exception.Message | Log -Name $LogName }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user