Tools.ps1: move all processes into functions and explicitly declare global variables

Test-InternetBandwidth.ps1: output to console instead of file
Uninstall-NCentralComponents.ps1: minor updates
This commit is contained in:
2021-08-05 09:42:15 -04:00
parent 2c257b6fed
commit 96e2af2dda
3 changed files with 59 additions and 29 deletions
+48 -21
View File
@@ -1,30 +1,54 @@
## Define the MSP name
$MSPName = "Emberkom"
[string]$Global:MSPName = "Emberkom"
## This section will define several global variables that can be used in scripts that source this one
[string]$Global:RootDirectory
[string]$Global:ScriptsDirectory
[string]$Global:OutputDirectory
[string]$Global:ToolsDirectory
[string]$Global:LogsDirectory
[string]$Global:LogFile
## Setup the MSP management directories
If ( Test-Path ${Env:ProgramData} ) { $RootDirectory = "${Env:ProgramData}\${MSPName}" } Else { $RootDirectory = "${Env:SystemDrive}\${MSPName}" }
$ScriptsDirectory = "${RootDirectory}\Scripts"
$OutputDirectory = "${RootDirectory}\Output"
$ToolsDirectory = "${RootDirectory}\Tools"
$LogsDirectory = "${RootDirectory}\Logs"
Function Setup-MSPDirectories {
If ( Test-Path ${Env:ProgramData} ) { $Global:RootDirectory = "${Env:ProgramData}\${MSPName}" } Else { $Global:RootDirectory = "${Env:SystemDrive}\${MSPName}" }
$Global:ScriptsDirectory = "${RootDirectory}\Scripts"
$Global:OutputDirectory = "${RootDirectory}\Output"
$Global:ToolsDirectory = "${RootDirectory}\Tools"
$Global:LogsDirectory = "${RootDirectory}\Logs"
## Make sure all the management directories exist
$RootDirectory, $ScriptsDirectory, $OutputDirectory, $ToolsDirectory, $LogsDirectory | ForEach-Object {
If ( !(Test-Path $_) ) {
Try { New-Item -Path $_ -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null }
Catch { Write-Output $_.Exception.Message }
}
## Make sure all the management directories exist
$RootDirectory, $ScriptsDirectory, $OutputDirectory, $ToolsDirectory, $LogsDirectory | ForEach-Object {
If ( !(Test-Path $_) ) {
Try { New-Item -Path $_ -ItemType Directory -Force -ErrorAction SilentlyContinue | Out-Null }
Catch { Write-Output $_.Exception.Message }
}
}
}
## Function to get a datestamp for right now in a long format
Function Get-LongDateTime {
Return $(Get-Date -Format "yyyyMMddHHmmssffff")
}
## Setup the log file for messages generated when this script is run
$LogFilePrefix = "ManagementScript"
$LogFilePostfix = Get-Date -Format "yyyyMMddHHmmssffff"
$LogFile = "${LogsDirectory}\${LogFilePrefix}_${LogFilePostfix}.log"
If ( !(Test-Path $LogFile) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null }
## Delete log files older than 30 days
$LogFileAllowedAge = (Get-Date).AddDays(-30)
Get-ChildItem -Path "${LogsDirectory}\${LogFilePrefix}_*.*" -Filter '*.log' | Where-Object {$_.LastWriteTime -lt $LogFileAllowedAge} | Remove-Item -Force -ErrorAction SilentlyContinue | Out-Null
Function Setup-LogFile {
$LogFilePrefix = "ManagementScript"
$LogFilePostfix = Get-LongDateTime
$Global:LogFile = "${LogsDirectory}\${LogFilePrefix}_${LogFilePostfix}.log"
## Delete log files older than 30 days
$LogFileAllowedAge = (Get-Date).AddDays(-30)
Try {
Get-ChildItem -Path "${LogsDirectory}\${LogFilePrefix}_*.*" -Filter '*.log' | `
Where-Object {$_.LastWriteTime -lt $LogFileAllowedAge} | `
Remove-Item -Force -ErrorAction SilentlyContinue | Out-Null
}
Catch { Write-Output $_.Exception.Message }
Try { If ( !(Test-Path $LogFile) ) { New-Item -Path $LogFile -ItemType File -Force | Out-Null } }
Catch { Write-Output $_.Exception.Message }
}
## Log a message to the log file
Function LogMsg {
@@ -455,4 +479,7 @@ function Toggle-Log {
$log.SaveChanges()
}
Catch { LogMsg $_.Exception.Message }
}
}
Setup-MSPDirectories
Setup-LogFile