From 96e2af2dda1670232dec8b413c14377b76c4b1f9 Mon Sep 17 00:00:00 2001 From: dyoder Date: Thu, 5 Aug 2021 09:42:15 -0400 Subject: [PATCH] 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 --- Test-InternetBandwidth.ps1 | 9 +++-- Tools.ps1 | 69 ++++++++++++++++++++++---------- Uninstall-NCentralComponents.ps1 | 10 +++-- 3 files changed, 59 insertions(+), 29 deletions(-) diff --git a/Test-InternetBandwidth.ps1 b/Test-InternetBandwidth.ps1 index 8e49356..f34e858 100644 --- a/Test-InternetBandwidth.ps1 +++ b/Test-InternetBandwidth.ps1 @@ -31,13 +31,14 @@ Catch { LogErr $_.Exception.Message } ## Run the SpeedTest CLI app $SPEEDTEST = "${WORKINGDIR}\speedtest.exe" If ( Test-Path $SPEEDTEST ) { - Try { Start-Process $SPEEDTEST -ArgumentList "--progress=no --selection-details --output-header --format=csv" -PassThru -Wait -RedirectStandardOutput $OUTPUT } + #Try { Start-Process $SPEEDTEST -ArgumentList "--progress=no --selection-details --output-header --format=csv" -PassThru -Wait -RedirectStandardOutput $OUTPUT } + Try { + $RESULTS = (cmd.exe /c "${SPEEDTEST} --progress=no") + LogMsg $RESULTS + } Catch { LogErr $_.Exception.Message } } Else { LogMsg "The file does not exist at the expected path: ""${SPEEDTEST}""" } -## Process the results - - ## Cleanup files and directories created by this script If ( Test-Path $ZIPFILE ) { LogMsg "Deleting downloaded zip file: ""$ZIPFILE""" diff --git a/Tools.ps1 b/Tools.ps1 index 5351b0e..ab3c94a 100644 --- a/Tools.ps1 +++ b/Tools.ps1 @@ -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 } -} \ No newline at end of file +} + +Setup-MSPDirectories +Setup-LogFile \ No newline at end of file diff --git a/Uninstall-NCentralComponents.ps1 b/Uninstall-NCentralComponents.ps1 index c61e2b3..2cf0c80 100644 --- a/Uninstall-NCentralComponents.ps1 +++ b/Uninstall-NCentralComponents.ps1 @@ -21,7 +21,7 @@ If ( Test-Path "${Env:ProgramFiles(x86)}\MspPlatform\RequestHandlerAgent\unins00 } ## Remove Windows Software Probe -If ( Test-Path "${ProgramFiles(x86)}\N-able Technologies\Windows Software Probe\bin\wsp.exe" ) { +If ( Test-Path "${ProgramFiles(x86)}\N-able Technologies\Windows Software Probe\" ) { $ProductCode = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Wow6432Node\N-able Technologies\Windows Software Probe").ProductCode LogMsg "Uninstalling Windows Software Probe" Try { Start-Process "msiexec.exe" -ArgumentList "/X {${ProductCode}} /qn" -Wait } @@ -29,9 +29,11 @@ If ( Test-Path "${ProgramFiles(x86)}\N-able Technologies\Windows Software Probe\ } ## Remove Windows Agent -LogMsg "Attempting to uninstall N-Central Windows Agent" -Try { Start-Process "msiexec.exe" -ArgumentList "/X{F5873D07-85EE-4010-A461-B60989884B1C} /qn" -Wait } -Catch { LogErr $_.Exception.Message } +If ( Test-Path "${ProgramFiles(x86)}\N-able Technologies\Windows Agent\" ) { + LogMsg "Uninstalling N-Central Windows Agent" + Try { Start-Process "msiexec.exe" -ArgumentList "/X{F5873D07-85EE-4010-A461-B60989884B1C} /qn" -Wait } + Catch { LogErr $_.Exception.Message } +} ## Remove NablePatchRepositoryService $ServiceName = "NablePatchRepositoryService"