iterate throught directory creation

This commit is contained in:
2020-09-23 21:31:52 -04:00
parent caafbaa815
commit d5c5c5cb4e
+21 -17
View File
@@ -9,14 +9,13 @@ $MSPTools = "${MSPRoot}\Tools"
$MSPLogs = "${MSPRoot}\Logs" $MSPLogs = "${MSPRoot}\Logs"
## Make sure all the management directories exist ## Make sure all the management directories exist
If ( !(Test-Path $MSPRoot) ) $MSPRoot, $MSPScripts, $MSPTools, $MSPLogs | ForEach-Object {
{ New-Item -Path $MSPRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } If ( !(Test-Path $_) )
If ( !(Test-Path $MSPScripts) ) {
{ New-Item -Path $MSPScripts -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null } Try { New-Item -Path $MSPRoot -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null }
If ( !(Test-Path $MSPTools) ) Catch { Write-Output $_.Exception.Message ; Exit }
{ 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 }
## Set a datestamp for any log files that might need to be created ## Set a datestamp for any log files that might need to be created
$LogFilePrefix = Get-Date -Format "yyyyMMddHHmmss" $LogFilePrefix = Get-Date -Format "yyyyMMddHHmmss"
@@ -35,7 +34,7 @@ Else { $LogEntry = "${Timestamp} - ${Message}" }
If ( $Name ) If ( $Name )
{ {
$LogFile = "${MSPLogs}\${LogFilePrefix}-${Name}.log" $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 Add-Content -Path $LogFile -Value $LogEntry
Write-Output $LogEntry Write-Output $LogEntry
} }
@@ -90,7 +89,7 @@ If ( Test-Path $Script )
{ {
Log "Checking for running instances of ${AppName}" -Name $LogName Log "Checking for running instances of ${AppName}" -Name $LogName
$RunningInstances = Get-Process | Where { $_.Name -like "${AppName}" } $RunningInstances = Get-Process | Where { $_.Name -like "${AppName}" }
If (! ($RunningInstances) ) If ( !($RunningInstances) )
{ {
Log "Executing: ${Script} ${Arguments}" -Name $LogName Log "Executing: ${Script} ${Arguments}" -Name $LogName
Try { Start-Process "${Script}" -ArgumentList "${Arguments}" -Wait } Try { Start-Process "${Script}" -ArgumentList "${Arguments}" -Wait }
@@ -138,8 +137,8 @@ param(
) )
$LogName = $MyInvocation.MyCommand $LogName = $MyInvocation.MyCommand
If ( $Arguments -eq "null" ) { $Arguments = $null } If ( $Arguments -eq "null" ) { $Arguments = $null }
If ( $Path ) { If (! (Test-Path $Path) ) { Log "The specified script does not exist: ${Path}" -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) -and ($ForceClose) -and ( !(IsAdmin) ) ) { Log "Cannot close dependent apps because this task does not have administrative privileges" -Name $LogName ; Exit }
If ( $HaltIfRunning ) If ( $HaltIfRunning )
{ {
$Halt = $false $Halt = $false
@@ -149,7 +148,12 @@ If ( $HaltIfRunning )
$RunningInstances = Get-Process | Where { $_.Name -like "${name}" } $RunningInstances = Get-Process | Where { $_.Name -like "${name}" }
If ( $RunningInstances ) 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 { $Halt = $true ; Log " ""${name}"" (running)" -Name $LogName }
} }
Else { Log " ""${name}"" (not 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" $URL = "https://dev.emberkom.com/emberkom/${Type}/raw/branch/master/${LivePSScript}.ps1"
Log "Retrieving : ${LivePSScript}.ps1" -Name $LogName Log "Retrieving : ${LivePSScript}.ps1" -Name $LogName
Try { $Script = (New-Object Net.WebClient).DownloadString($URL) } 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) } Try { $ScriptBlock = [Scriptblock]::Create($Script) }
Catch { $_.Exception.Message | Log -Name $LogName } Catch { $_.Exception.Message | Log -Name $LogName ; Exit }
Log "Executing: ${LivePSScript}.ps1 ${Arguments}" Log "Executing: ${LivePSScript}.ps1 ${Arguments}"
Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments } Try { Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $Arguments }
Catch { $_.Exception.Message | Log -Name $LogName } Catch { $_.Exception.Message | Log -Name $LogName ; Exit }
} }
'local' 'local'
@@ -184,7 +188,7 @@ switch ($PSCmdlet.ParameterSetName)
Log " Finished" -Name $LogName Log " Finished" -Name $LogName
} }
} }
Catch { $_.Exception.Message | Log -Name $LogName } Catch { $_.Exception.Message | Log -Name $LogName ; Exit }
} }
} }
} }