103 lines
4 KiB
PowerShell
103 lines
4 KiB
PowerShell
|
|
Function Get-PCCleanup($ComputerName){
|
|||
|
|
|
|||
|
|
|
|||
|
|
Write-Host 'Starting PC Cleanup'
|
|||
|
|
Write-Host 'Clearing Windows Temp'
|
|||
|
|
|
|||
|
|
if(Test-Path \\$ComputerName\C$\Windows\Temp){
|
|||
|
|
|
|||
|
|
Get-ChildItem -Path \\$ComputerName\C$\Windows\Temp -Recurse -Force | Remove-Item -Force -Recurse
|
|||
|
|
Write-Host "Cleaned up files in \\$ComputerName\C$\Windows\Temp " -ForegroundColor Green
|
|||
|
|
|
|||
|
|
}else{
|
|||
|
|
|
|||
|
|
Write-Host "Could not access \\$ComputerName\C$\Windows\Temp" -ForeGround Red
|
|||
|
|
}
|
|||
|
|
Write-Host 'Clearing PACS Web Cache'
|
|||
|
|
if(Test-Path \\$ComputerName\C$\DRS\LocalCache\PACS-Web){
|
|||
|
|
|
|||
|
|
Get-ChildItem -Path \\$ComputerName\C$\DRS\LocalCache\PACS-Web -Recurse -Force | Remove-Item -Force -Recurse
|
|||
|
|
Write-Host "Cleaned all files in \\$ComputerName\C$\DRS\LocalCache\PACS-Web" -ForegroundColor Green
|
|||
|
|
|
|||
|
|
}else{
|
|||
|
|
|
|||
|
|
Write-Host "Could not access \\$ComputerName\C$\DRS\LocalCache\PACS-Web" -ForeGround Red
|
|||
|
|
}
|
|||
|
|
Write-Host 'Clearing Brance cache'
|
|||
|
|
try { Clear-BCCache -Force
|
|||
|
|
Write-Host 'Cleared branch cache' -ForegroundColor Green
|
|||
|
|
}
|
|||
|
|
catch {Write-Host 'Unable to clear branch cache' -ForegroundColor Yellow}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Function Get-PCProfileCleanup($ComputerName){
|
|||
|
|
|
|||
|
|
$diskspace = Get-FreeDiskSpace $ComputerName
|
|||
|
|
$out1 = "Free Space on " + $computerName + ": "
|
|||
|
|
$out2 = $diskspace.'Free Space'
|
|||
|
|
$out3 = " GB/" + $diskspace.'Total Space' + ' GB'
|
|||
|
|
Write-Host $out1 -NoNewline
|
|||
|
|
if($diskspace.'Free Space' -lt 5){
|
|||
|
|
Write-Host $out2 -ForegroundColor Red -NoNewline
|
|||
|
|
}
|
|||
|
|
elseif($diskspace.'Free Space' -lt 10){
|
|||
|
|
Write-Host $out2 -ForegroundColor Yellow -NoNewline
|
|||
|
|
}
|
|||
|
|
else{
|
|||
|
|
Write-Host $out2 -NoNewline
|
|||
|
|
}
|
|||
|
|
Write-Host $out3
|
|||
|
|
|
|||
|
|
if($null -eq $ComputerName){
|
|||
|
|
$ComputerName = $env:COMPUTERNAME
|
|||
|
|
}
|
|||
|
|
$userInput = Read-Host "Do you wish to proceed with removing inactive user profiles older than X days? (y/n)"
|
|||
|
|
if($userInput -ne "y"){
|
|||
|
|
Write-Host "No user profiles deleted"
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
$days = Read-Host "Clean up user profiles inactive for more than x days"
|
|||
|
|
|
|||
|
|
$allProfiles = Get-LastLoginTimes -ComputerName $ComputerName
|
|||
|
|
$userProfiles = $allProfiles | Where-Object {$_.LastLoginTime -lt $(Get-Date).Date.AddDays(-$days)}
|
|||
|
|
$userProfiles += $allProfiles | Where-Object LastLoginTime -eq 'Unknown'
|
|||
|
|
|
|||
|
|
|
|||
|
|
#$userProfiles = Get-CIMInstance -ComputerName $ComputerName -Class Win32_UserProfile | Where {$_.LastUseTime -lt $(Get-Date).Date.AddDays(-$days)}
|
|||
|
|
$userAmount = 0
|
|||
|
|
|
|||
|
|
foreach ($user in $userProfiles) {
|
|||
|
|
|
|||
|
|
$output = "{0} | {1:MM}/{1:dd}/{1:yyyy}" -f $user.Name, $user.LastLoginTime
|
|||
|
|
Write-Host $output
|
|||
|
|
$userAmount++
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
Write-host "There are $userAmount profiles inactive more than $days days"
|
|||
|
|
if($userProfiles){
|
|||
|
|
Write-Host "WARNING: ALL PROFILES DELETED ARE UNRETRIEVABLE" -ForegroundColor Yellow
|
|||
|
|
$delChoice = Read-Host "Delete these users (y/n)"
|
|||
|
|
}
|
|||
|
|
else {
|
|||
|
|
Write-Host "No users inactive for more than $days days"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if ($delChoice -eq 'y'){
|
|||
|
|
$current = 0
|
|||
|
|
foreach ($user in $userProfiles){
|
|||
|
|
$current++
|
|||
|
|
Write-Progress -Activity "Deleting Profiles" -Status "$current / $userAmount" -PercentComplete ($current/$userAmount*100) -CurrentOperation $user.LocalPath
|
|||
|
|
Write-Host "Deleting " $user.LocalPath " | " $user.LastLoginTime
|
|||
|
|
Get-CIMInstance -Class Win32_UserProfile -ComputerName $ComputerName | Where-Object { $_.LocalPath -eq $user.LocalPath } | Remove-CIMInstance -Verbose
|
|||
|
|
}
|
|||
|
|
Write-Host "$current profiles have been deleted"
|
|||
|
|
|
|||
|
|
$newDiskspace = Get-FreeDiskSpace $ComputerName
|
|||
|
|
[int]$percentFreed = (($newDiskspace.'Free Space' - $diskspace.'Free Space') / $diskspace.'Total Space') * 100
|
|||
|
|
$out = "Free Space on " + $computerName + ": " + $newDiskspace.'Free Space' + "GB/" + $newDiskspace.'Total Space' + 'GB' + " ($percentFreed% Freed)"
|
|||
|
|
Write-Host $out
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|