new function Run-Script

This commit is contained in:
2020-09-23 15:24:26 -04:00
parent 0e69340331
commit 4b28dadd24
+72
View File
@@ -108,6 +108,78 @@ If ( Test-Path $Script )
Else { Log "The specified script does not exist: ${Script}" -Name $LogName }
}
## Run a script from the live repo or from a local path
Run-Script
{
param(
## Parameters for live-ps scripts
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
[string]$Type='management-scripts',
[Parameter(Mandatory=$true,ParameterSetName='live-ps')]
[string]$LivePSScript,
## Parameters for local scripts
[Parameter(Mandatory=$true,ParameterSetName='local')]
[string]$Path,
## Parameters for all scripts
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
[Parameter(Mandatory=$false,ParameterSetName='local')]
[string]$Arguments='null',
[Parameter(Mandatory=$false,ParameterSetName='live-ps')]
[Parameter(Mandatory=$false,ParameterSetName='local')]
[System.Collections.ArrayList]$HaltIfRunning='null'
)
$LogName = $MyInvocation.MyCommand
If ( $Arguments -eq "null" ) { $Arguments = $null }
If ( $HaltIfRunning -eq "null" ) { $HaltIfRunning = $null }
switch ($PSCmdlet.ParameterSetName)
{
'live-ps'
{
$URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${LivePSScript}.ps1"
Log "Retrieving : ${LivePSScript}.ps1" -Name $LogName
Try { $Script = (New-Object Net.WebClient).DownloadString($URL) }
Catch { $_.Exception.Message | Log -Name $LogName }
Try { $ScriptBlock = [Scriptblock]::Create($Script) }
Catch { $_.Exception.Message | Log -Name $LogName }
Log "Executing: ${LivePSScript}.ps1 ${Arguments}"
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
Catch { $_.Exception.Message | Log -Name $LogName }
}
'local'
{
If ( Test-Path $Path )
{
$Halt = $false
If ( $HaltIfRunning -ne $null )
{
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 )
{
$Halt = $true
Log " ""${name}"" (running)" -Name $LogName
}
Else { Log " ""${name}""" -Name $LogName }
}
}
If ( $Halt -eq $false )
{
Log "Executing: ${Script} ${Arguments}" -Name $LogName
Try { Start-Process "${Script}" -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 }
}
}
}
## Function to return a Powershell version object from a string
Function Get-Version
{