From 4b28dadd248fe5c0594c9d6e010f1beb8bf3a4e0 Mon Sep 17 00:00:00 2001 From: dyoder Date: Wed, 23 Sep 2020 15:24:26 -0400 Subject: [PATCH] new function Run-Script --- Tools.ps1 | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/Tools.ps1 b/Tools.ps1 index 86f08d6..f350e40 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -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 {