A few days ago I was running into errors and inconsistencies when trying to work with directory size checking. I realized eventually that it was only a permissions issue.
Feel free to use any portion of this script for yourselves to accomplish other tasks, if you don’t have better ways.
My domain just happens to be streamlined nicely to where the file structure for these drives matches their Name property in Active Directory, so this works perfectly for me. (I think I said that correctly?? – Sporadic brain :D)
Here is the updated and working version. I output results to a .txt, nothing fancy just a list that shows me how much space was cleared each time it runs (I set mine to once per week). It should also generate a new report file each time it runs, instead of appending the previous one.
<# .SYNOPSIS Written by: JBear If user account no longer exists in Active Directory, delete Home Drive for associated user. Outputs text file with deleted Home Drives and Total Space Cleared. Purpose of script to to assist SysAdmins with the deletion of End-User home Drives and to mitigate the Human Error factor. Providing the checks and balances to maintain a clean enviroment. This script is meant to be set as a scheduled task and run on all servers listed in the $ServerBase array. Reports are output to \SERVERITWeeklyReportsDeletedHomeDrives*.txt #> #Array of Home Drive paths $ServerBase = @('\SERVER01Home$', '\SERVER02HomeJ$', '\SERVER03Home$', '\SERVER04Home$') #Empty arrays for later use $TotalFileSize = @() $FinalOutput = @() #Out-File creation path $prevFiles = Get-ChildItem '\SERVERITWeeklyReportsDeletedHomeDrives*.txt' $prevVersion = ($prevFiles.Name -replace '[^d]' | foreach { [int]$_ } | sort | select -last 1) + 1 $OutPath = ('\SERVERITWeeklyReportsDeletedHomeDrives{0:D3}.txt' -f $prevVersion) ForEach($Server in $ServerBase) { #Retrieve object 'name' from each $Server path $Names = Get-ChildItem -LiteralPath $Server -Directory | Select-Object Name ForEach($Name in $Names) { $User = $( #Reference $Name.name against Active Directory object Try { Get-aduser $Name.name } Catch{ $Null }) #If user object still exists, do nothing #If user DOES NOT exist, start deletion and reporting process If(!($User -ne $Null)) { $HomeDrive = "$Server" + $Name.name #Measure file lengths (bytes) for each $HomeDrive recursively to retrieve full directory size $DirSize = (Get-ChildItem $HomeDrive -Recurse -ErrorAction "SilentlyContinue" | Where {-NOT $_.PSIscontainer} | Measure-Object -Property Length -Sum) #Divide value of $DirSize (bytes) by MegaBytes (MB) $SumSize = $DirSize.Sum/1MB #Format $SumSize by rounding and displaying 2 decimal places $Output = "Removed $HomeDrive : " + "{0:N2}" -f $SumSize + "MB" #Add $SumSize values to $TotalFileSize array $TotalFileSize += $SumSize #Output removed $HomeDrive along with total size of drive Write-Output $Output | Out-File -FilePath $OutPath -Append -Force #Delete user $HomeDrive Remove-Item $HomeDrive -Force -Recurse -Confirm:$false }#If }#ForEach($Name) #Invoke math operations on $Sum array $Sum = $TotalFileSize -Join '+' $InvokeSum = Invoke-Expression $Sum #Add $InvokeSum values to $FinalOutput array $FinalOutput += $InvokeSum #Invoke math operations on $Sum array $ServerSum = $FinalOutput -Join '+' $InvokeFinal = Invoke-Expression $ServerSum #Format $InvokeFinal output $TotalOutput = "Total Space Saved:" + "{0:N2}" -f $InvokeFinal + "MB" #Break line between $Server lists Write-Output "`n" | Out-File -FilePath $OutPath -Append -Force Write-Output "-------------------------------------" | Out-File -FilePath $OutPath -Append -Force Write-Output "`n" | Out-File -FilePath $OutPath -Append -Force }#ForEach($Server) #Output Total Space Saved in MB Write-Output "--------------------" | Out-File -FilePath $OutPath -Append -Force Write-Output $TotalOutput | Out-File -FilePath $OutPath -Append -Force
submitted by /u/JBear_Alpha
[link] [comments]
The post Retrieving Directory Sizes and Removing Home Drives appeared first on How to Code .NET.