diff --git a/Tools.ps1 b/Tools.ps1 index a74bcce..a6c1583 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -9,14 +9,13 @@ $MSPTools = "${MSPRoot}\Tools" $MSPLogs = "${MSPRoot}\Logs" ## Make sure all the management directories exist -If ( !(Test-Path $MSPRoot) ) -{ New-Item -Path $MSPRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } -If ( !(Test-Path $MSPScripts) ) -{ New-Item -Path $MSPScripts -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } -If ( !(Test-Path $MSPTools) ) -{ New-Item -Path $MSPTools -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } -If ( !(Test-Path $MSPLogs) ) -{ New-Item -Path $MSPLogs -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } +$MSPRoot, $MSPScripts, $MSPTools, $MSPLogs | ForEach-Object { + If ( !(Test-Path $_) ) + { + Try { New-Item -Path $MSPRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } + Catch { Write-Output $_.Exception.Message ; Exit } + } +} ## Set a datestamp for any log files that might need to be created $LogFilePrefix = Get-Date -Format "yyyyMMddHHmmss" @@ -35,7 +34,7 @@ Else { $LogEntry = "${Timestamp} - ${Message}" } If ( $Name ) { $LogFile = "${MSPLogs}\${LogFilePrefix}-${Name}.log" - If (! (Test-Path $LogFile) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null } + If ( !(Test-Path $LogFile) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null } Add-Content -Path $LogFile -Value $LogEntry Write-Output $LogEntry } @@ -90,7 +89,7 @@ If ( Test-Path $Script ) { Log "Checking for running instances of ${AppName}" -Name $LogName $RunningInstances = Get-Process | Where { $_.Name -like "${AppName}" } - If (! ($RunningInstances) ) + If ( !($RunningInstances) ) { Log "Executing: ${Script} ${Arguments}" -Name $LogName Try { Start-Process "${Script}" -ArgumentList "${Arguments}" -Wait } @@ -138,8 +137,8 @@ param( ) $LogName = $MyInvocation.MyCommand If ( $Arguments -eq "null" ) { $Arguments = $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 ( $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 @@ -149,7 +148,12 @@ If ( $HaltIfRunning ) $RunningInstances = Get-Process | Where { $_.Name -like "${name}" } If ( $RunningInstances ) { - If ( $ForceClose -eq $true ) { $name | Stop-Process -Force ; Log " ""${name}"" (force closed)" -Name $LogName } + If ( $ForceClose -eq $true ) + { + Try { $name | Stop-Process -Force } + Catch { $_.Exception.Message | Log -Name $LogName ; Exit } + Log " ""${name}"" (force closed)" -Name $LogName + } Else { $Halt = $true ; Log " ""${name}"" (running)" -Name $LogName } } Else { Log " ""${name}"" (not running)" -Name $LogName } @@ -163,12 +167,12 @@ switch ($PSCmdlet.ParameterSetName) $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 } + Catch { $_.Exception.Message | Log -Name $LogName ; Exit } Try { $ScriptBlock = [Scriptblock]::Create($Script) } - Catch { $_.Exception.Message | Log -Name $LogName } + Catch { $_.Exception.Message | Log -Name $LogName ; Exit } Log "Executing: ${LivePSScript}.ps1 ${Arguments}" Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments } - Catch { $_.Exception.Message | Log -Name $LogName } + Catch { $_.Exception.Message | Log -Name $LogName ; Exit } } 'local' @@ -184,7 +188,7 @@ switch ($PSCmdlet.ParameterSetName) Log " Finished" -Name $LogName } } - Catch { $_.Exception.Message | Log -Name $LogName } + Catch { $_.Exception.Message | Log -Name $LogName ; Exit } } } }