refactor removal process for provisioning admin account and profile
This commit is contained in:
@@ -20,16 +20,20 @@ If ( $(Get-CimInstance -Class Win32_Process -Filter 'name = "explorer.exe"' | In
|
||||
}
|
||||
|
||||
# Remove the provisioning admin account
|
||||
Try { Remove-LocalUser -SID $ProvisioningAdmin.SID.Value -ErrorAction Stop }
|
||||
$SID = $ProvisioningAdmin.SID.Value
|
||||
Write-Output "Removing provisioning admin account`nUsername: ${ProvisioningAdminUsername}`nSID: $SID"
|
||||
Try { Remove-LocalUser -SID $SID -ErrorAction Stop }
|
||||
Catch { Write-Error "The provisioning admin account could not be removed`n"+$_.Exception.Message ; Exit 1 }
|
||||
|
||||
# Fully remove the provisioning admin profile
|
||||
Write-Output "Removing provisioning admin CIM instance"
|
||||
Try { Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.SID -eq $ProvisioningAdmin.SID.Value } -ErrorAction SilentlyContinue | Remove-CimInstance }
|
||||
Catch { Write-Error "The provisioning admin profile could not be removed`n"+$_.Exception.Message ; Exit 1 }
|
||||
|
||||
# Delete any remaining provisioning admin directories
|
||||
$ProfilePath = "${Env:SystemDrive}\Users\$ProvisioningAdminUsername"
|
||||
If ( Test-Path $ProfilePath ) {
|
||||
Write-Output "Deleting ""$ProfilePath"""
|
||||
Remove-Item $ProfilePath -Recurse -Force
|
||||
Write-Output "Deleting provisioning admin profile directory: ${ProfilePath}"
|
||||
Try { Remove-Item $ProfilePath -Recurse -Force }
|
||||
Catch { Write-Error "The provisioning admin profile directory could not be removed`n"+$_.Exception.Message ; Exit 1 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user