2013-02-07 16 views
2

我需要一個PowerShell腳本,它將通過系統中的所有用戶,並將查找任何用戶擁有的所有文件的總大小...我有腳本是通過所有用戶,但後來我不知道,繼續計算該用戶所擁有的每個用戶Powershell獲取用戶擁有的文件的總大小

這裏總規模是一個腳本,它現在I`ve:

$users = Get-WmiObject -class Win32_UserAccount 

foreach($user in $users) { 
    $name = $user.Name 
    $fullName = $user.FullName; 
    if(Test-Path "C:\Users\$name") { 
     $path = "C:\Users\$name" 
    } else { 
     $path = "C:\Users\Public" 
    } 

    $dirSize = (Get-ChildItem $path -recurse | Measure-Object -property length -sum) 
"{0:N2}" -f ($dirSize.sum/1Gb) + " Gb" 
echo "$dirSize" 


Add-Content -path "pathototxt..." -value "$name $fullName $path" 

} 

我會更高興。如果有人知道答案並告訴我它... 謝謝

回答

0

我不知道你在這裏要求什麼。 "to continue with counting total size which user owns for each user"。是吧?是否想要檢查系統上的每個文件,或者只是像當前那樣檢查用戶文件夾?

如果您只是調整它以在輸出中包含文件大小,那麼您的腳本可以正常工作。我個人會考慮使用csv來存儲這個,因爲並非所有的用戶都會有全名(管理員,客人等)。另外,atm。您的腳本會多次統計公用文件夾(每次用戶沒有配置文件時)。例如。管理員(如果它從未登錄),客人等可能都得到它指定。

更新腳本它同時輸出文本文件和CSV

$users = Get-WmiObject -class Win32_UserAccount 

$out = @() 
#If you want to append to a csv-file, replace the $out line above with the one below 
#$out = Import-Csv "file.csv" 

foreach($user in $users) { 
    $name = $user.Name 
    $fullName = $user.FullName; 
    if(Test-Path "C:\Users\$name") { 
     $path = "C:\Users\$name" 
    } else { 
     $path = "C:\Users\Public" 
    } 

    $dirSize = (Get-ChildItem $path -Recurse -ErrorAction SilentlyContinue | ? { !$_.PSIsContainer } | Measure-Object -Property Length -Sum) 
    $size = "{0:N2}" -f ($dirSize.Sum/1Gb) + " Gb" 

    #Saving as textfile 
    #Add-Content -path "pathototxt..." -value "$name $fullName $path $size" 
    Add-Content -path "file.txt" -value "$name $fullName $path $size" 

    #CSV-way 
    $o = New-Object psobject -Property @{ 
    Name = $name 
    FullName = $fullName 
    Path = $path 
    Size = $size 
    } 

    $out += $o 
} 

#Exporting to csv format 
$out | Export-Csv "file.csv" -NoTypeInformation 

編輯:使用由@mjolinor和@ C.B提供了答案的另一解決方案。修改掃描您c:\驅動器,而排除一些「rootfolders」喜歡「程序文件」,「窗口」等,出口的結果到CSV文件準備好爲Excel:

$oSIDs = @{} 

$exclude = @("Program Files", "Program Files (x86)", "Windows", "Perflogs"); 

Get-ChildItem C:\ | ? { $exclude -notcontains $_.Name } | % { Get-ChildItem $_.FullName -Recurse -ErrorAction SilentlyContinue | ? { !$_.PSIsContainer } } | % { 
    $oSID = $_.GetAccessControl().Sddl -replace '^o:(.+?).:.+','$1' 
    $oSIDs[$oSID] += $_.Length 
    } 

$out = @() 
$oSIDs.GetEnumerator() | % { 
    $user = (New-Object System.Security.Principal.SecurityIdentifier($_.Key)).Translate([System.Security.Principal.NTAccount]).Value 
    $out += New-Object psobject -Property @{ 
     User = if($user) { $user } else { $_.Key } 
     "Size(GB)" = $oSIDs[$_.Key]/1GB 
    } 
} 

$out | Export-Csv file.csv -NoTypeInformation -Delimiter ";" 
+0

我知道,這,我編程的幾乎是正確的。但是我需要做下一部分,它是系統中所有文件的計數大小,當前(第i個)用戶是所有者。到目前爲止,我不知道這一點。 – DaMi

+0

你有任何特定的文件夾?你可以排除「程序文件」,「窗口」等?有多個磁盤嗎? –

+0

它沒有告訴我應該通過哪個文件夾。這就是爲什麼我認爲它應該通過整個文件系統。但它可能是荒謬的..所以,然後我認爲它可以開始在家裏的文件夾,幷包括在這個 – DaMi

2

如果有大量的文件,您可能需要考慮:

$oSIDs = @{} 

get-childitem <filespec> | 
foreach { 
    $oSID = $_.GetAccessControl().Sddl -replace '^o:(.+?).:.+','$1' 
    $oSIDs[$oSID] += $_.length 
    } 

然後解決完成後的SID。從SDDL字符串解析所有者SID或衆所周知的安全主體ID可以使提供者不必進行大量的重複名稱解析,從而讓您恢復「友好」名稱。

+2

+1 Easy sid converter:'(new-object system.security.principal.securityidentifier($ sid))。translate([system.security.principal.ntaccount])' –

+0

+1在代碼上做的很棒。 :-)僅供參考,我在下面的解決方案中使用了您的代碼,將它們放在一起並進行了修改。不要試圖成爲一個小偷,只是爲他提供一個完整的解決方案。 –

+0

不是問題:)。 – mjolinor