SHOW:
|
|
- or go back to the newest paste.
| 1 | $date = Get-Date -format "yyyy-MM-dd" | |
| 2 | $HomeD = @() | |
| 3 | $size = $NULL | |
| 4 | $sum = $NULL | |
| 5 | $MaxThreads = "10" | |
| 6 | $SleepTimer = "500" | |
| 7 | $MaxWaitAtEnd = "3600" | |
| 8 | $ScriptFile = "C:\Temp\script lab\New\getHomeDirSize.ps1" | |
| 9 | $i = 0 | |
| 10 | $deptArray = @("OU=Department1,OU=Company,OU=example,DC=example,DC=com",
| |
| 11 | "OU=Department2,OU=Company,DC=example,DC=com", | |
| 12 | "OU=Department3,OU=Company,DC=example,DC=com", | |
| 13 | "OU=Department4,OU=Company,DC=example,DC=com", | |
| 14 | "OU=Department5,OU=Company,DC=example,DC=com", | |
| 15 | "OU=Department6,OU=Company,DC=example,DC=com", | |
| 16 | "OU=Department7,OU=Company,DC=example,DC=com", | |
| 17 | "OU=Department8,OU=Company,DC=example,DC=com", | |
| 18 | "OU=Department9,OU=Company,DC=example,DC=com", | |
| 19 | "OU=Department10,OU=Company,DC=example,DC=com", | |
| 20 | "OU=Department11,OU=Company,DC=example,DC=com", | |
| 21 | "OU=Department12,OU=Company,DC=example,DC=com") | |
| 22 | ||
| 23 | foreach($dept in $deptArray){
| |
| 24 | $Users = get-aduser -filter * -searchbase $dept -properties HomeDirectory,SamAccountName,GivenName,Surname | where-object homeDirectory -ne $null | |
| 25 | ||
| 26 | ForEach ($User in $Users){
| |
| 27 | # Check to see if there are too many open threads | |
| 28 | # If there are too many threads then wait here until some close | |
| 29 | While ($(Get-Job -state running).count -ge $MaxThreads){
| |
| 30 | Write-Progress -Activity "Getting directory size" -Status "Waiting for threads to close" -CurrentOperation "$i threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($i / $Users.count * 100) | |
| 31 | Start-Sleep -Milliseconds $SleepTimer | |
| 32 | } | |
| 33 | ||
| 34 | #"Starting job - $User" | |
| 35 | $i++ | |
| 36 | Start-Job -FilePath $ScriptFile -ArgumentList $User | Out-Null | |
| 37 | Write-Progress -Activity "Getting directory size" -Status "Starting Threads" -CurrentOperation "$i threads created - $($(Get-Job -state running).count) threads open" -PercentComplete ($i / $Users.count * 100) | |
| 38 | } | |
| 39 | $Complete = Get-date | |
| 40 | ||
| 41 | While ($(Get-Job -State Running).count -gt 0){
| |
| 42 | $UsersStillRunning = "" | |
| 43 | ForEach ($System in $(Get-Job -state running)){$UsersStillRunning += ", $($System.name)"}
| |
| 44 | $UsersStillRunning = $UsersStillRunning.Substring(2) | |
| 45 | Write-Progress -Activity "Getting directory size" -Status "$($(Get-Job -State Running).count) threads remaining" -CurrentOperation "$UsersStillRunning" -PercentComplete ($(Get-Job -State Completed).count / $(Get-Job).count * 100) | |
| 46 | ||
| 47 | If ($(New-TimeSpan $Complete $(Get-Date)).totalseconds -ge $MaxWaitAtEnd){"Killing all jobs still running . . .";Get-Job -State Running | Remove-Job -Force}
| |
| 48 | Start-Sleep -Milliseconds $SleepTimer | |
| 49 | } | |
| 50 | ||
| 51 | "Reading all jobs" | |
| 52 | $Jobs = Get-Job | |
| 53 | ForEach($Job in $Jobs){
| |
| 54 | ||
| 55 | $data = Receive-Job $Job -Keep | |
| 56 | ||
| 57 | $obj = New-Object System.Object | |
| 58 | $obj | add-member -type NoteProperty -name User -value $Data.name | |
| 59 | $obj | add-member -type NoteProperty -name HomeDirectory -value $Data.homeDir | |
| 60 | $obj | add-member -type NoteProperty -name HomeDirectorySize -value $Data.homeDirSize | |
| 61 | $HomeD += $obj | |
| 62 | $sum = $sum+$data.HomeDirSize | |
| 63 | ||
| 64 | - | if ($User -eq $Jobs[-1]) {
|
| 64 | + | if ($Job -eq $Jobs[-1]) {
|
| 65 | $sum = $sum / 1KB | |
| 66 | $obj = New-Object System.Object | |
| 67 | $obj | add-member -type NoteProperty -name User -value "Total" | |
| 68 | $obj | add-member -type NoteProperty -name HomeDirectorySize -value $sum | |
| 69 | $HomeD += $obj | |
| 70 | } | |
| 71 | } | |
| 72 | ||
| 73 | $csvFileName = $NULL | |
| 74 | $dept = $dept.trimend(',OU=Company,DC=example,DC=com')
| |
| 75 | $dept = $dept.trimstart('OU=')
| |
| 76 | $csvFileName = $dept+"_"+$date | |
| 77 | $HomeD | export-csv c:\temp\$csvFileName.csv -notypeinformation -encoding unicode | |
| 78 | } |