Files
management-scripts/Cleanup-SystemPath.ps1
T
2020-09-28 15:14:18 -04:00

42 lines
1.4 KiB
PowerShell

## Source the tools script if it isn't already available
If ( !($MSPName) ) { . $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1'))) }
## Set the name of the log file
$LogName = 'Cleanup-SystemPath'
## Get contents of SYSTEM PATH environment variable
$REG_ENVVAR = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
$CURRENT_PATH = (Get-ItemProperty -Path $REG_ENVVAR -Name Path).Path
$NEW_PATH = $null
$REMOVE_PATH =$null
## Verify each path
Foreach ( $path in $CURRENT_PATH.Split(";") )
{
If ( (Test-Path $path) -or ($path -like "*%*%*") )
{
If ( $NEW_PATH ) { $NEW_PATH += ";${path}" }
Else { $NEW_PATH = $path }
}
Else
{
If ( $REMOVE_PATH ) { $REMOVE_PATH += ";${path}" }
Else { $REMOVE_PATH = $path }
}
}
Log "Removed Paths:`n`r${REMOVE_PATH}" -Name $LogName
Try { Set-ItemProperty -Path $REG_ENVVAR -Name Path -Value $NEW_PATH }
Catch
{
$_.Exception.Message | Log -Error -Name $LogName
Log "Failed to cleanup system path, using original values" -Error -Name $LogName
Try { Set-ItemProperty -Path $REG_ENVVAR -Name Path -Value $CURRENT_PATH }
Catch
{
$_.Exception.Message | Log -Error -Name $LogName
Log "This is not good... !! DO NOT RESTART UNTIL SYSTEM PATH IS FIXED !!" -Error -Name $LogName
Exit
}
}