Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Get-SidToUser {
- param (
- [parameter(Mandatory=$true)]
- [String]$SID
- )
- $ad = [adsi]"LDAP://<SID=$SID>"
- $UserID = $ad.sAMAccountName
- $UserName = $ad.Name
- if ($UserID) {
- $obj = [PSCustomObject]@{UserID = $UserID ; Name = $UserName}
- return $obj
- } else {
- throw "Invalid SID."
- }
- }
- Function Write-ArrayToTable{
- param(
- [String[]]$Names,
- [Object[][]]$Data
- )
- $myProps = for($i = 0;; ++$i){
- $Props = [ordered]@{}
- for($j = 0; $j -lt $Data.Length; ++$j){
- if($i -lt $Data[$j].Length){
- $Props.Add($Names[$j], $Data[$j][$i])
- }
- }
- if(!$Props.get_Count()){
- break
- }
- [PSCustomObject]$Props
- }
- $myProps | Format-Table
- }
- Function Read-YesNo {
- param(
- [String]$Title="Confirmation needed.",
- [String]$Message="Are you sure?"
- )
- $choiceYes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Answer Yes."
- $choiceNo = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Answer No."
- $options = [System.Management.Automation.Host.ChoiceDescription[]]($choiceYes, $choiceNo)
- $result = $host.ui.PromptForChoice($title, $message, $options, 1)
- switch ($result) {
- 0 {
- return $true
- }
- 1 {
- return $false
- }
- }
- }
- $IgnoredAccounts = @($env:UserName,'Public','default','administrator', `
- 'technology','tech','tech2','cnb')
- Write-Host
- $Computer = Read-Host -Prompt "Enter the computer name"
- if (Test-Connection -ComputerName $Computer -Quiet) {
- [System.Collections.ArrayList]$BadProfiles = @()
- [System.Collections.ArrayList]$RemovedProfiles = @()
- [System.Collections.ArrayList]$LockedProfiles = @()
- [System.Collections.ArrayList]$objBadProfiles = @()
- [System.Collections.ArrayList]$tmp = @()
- $Profiles = Get-CimInstance Win32_UserProfile -ComputerName $Computer -ea 0 `
- | Where-Object { !($_.Special) } `
- | Where-Object { $_.LocalPath.split('\')[-1] -notin $IgnoredAccounts }
- foreach ($profile in $Profiles) {
- try {
- $objSID = $profile.SID
- $objUser = Get-SidToUser -SID $objSID
- }
- # SID not found in AD. Add to BadProfiles array.
- catch {
- $err = $_.Exception
- $BadProfiles.Add($profile.LocalPath) > $null
- # If profile is locked, add to LockedProfiles array.
- if ($profile.Loaded -eq $true) {
- $LockedProfiles.Add($profile.LocalPath) > $null
- } else {
- $objBadProfiles.Add($profile) > $null
- }
- }
- }
- # Print results
- if ($BadProfiles.Count -gt 0) {
- Write-ArrayToTable "Bad Profiles", "Locked Profiles" $BadProfiles, $LockedProfiles | Format-Table
- } else {
- Write-Host
- Write-Host "No bad profiles found on $($Computer.ToUpper())." -ForegroundColor Yellow
- Write-Host
- Read-Host "Press enter to exit"
- exit 0
- }
- $result = Read-YesNo -Title "" -Message "Remove all unlocked bad profiles?"
- if ($result -eq $true) {
- foreach ($profile in $objBadProfiles) {
- Write-Host
- Write-Host "Removing profile:"
- Write-Host $profile.LocalPath
- $profile | Remove-CimInstance
- $RemovedProfiles.Add($profile.LocalPath) > $null
- }
- if ($RemovedProfiles.Count -gt 0) {
- Write-ArrayToTable "Removed Profiles","tmp" $RemovedProfiles,$tmp
- } else {
- Write-Host
- Write-Host "No profiles removed." -ForegroundColor Yellow
- Write-Host
- }
- Read-Host "Press enter to exit"
- }
- else {
- Write-Host
- Write-Host "No profiles removed." -ForegroundColor Yellow
- Write-Host
- Read-Host "Press enter to exit"
- exit 0
- }
- } else {
- Write-Host
- Write-Host "ERROR: $Computer is not accessible." -ForegroundColor Red
- Read-Host "Press enter to exit"
- exit 1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement