Windows-Server-2003
使用 powershell 的頂級文件大小
我有以下腳本,它可以執行我想要的操作,但僅適用於一個驅動器(我必須指定),並且在我正在執行它的伺服器上。
我希望能夠為我的伺服器中的每個磁碟和我擁有的每個伺服器執行此操作(我有一個包含所有伺服器名稱的文本文件)。
這是程式碼:
Param ( [string]$Path = "F:\", [string]$ReportPath = "F:\Monitor_Tasks" ) Function AddObject { Param ( $FileObject ) $Size = [double]($FSO.GetFolder($FileObject.FullName).Size) $Script:TotSize += $Size If ($Size) { $NiceSize = CalculateSize $Size } Else { $NiceSize = "0.00 MB" $Size = 0 } $Script:Report += New-Object PSObject -Property @{ 'Folder Name' = $FileObject.FullName 'Created on' = $FileObject.CreationTime 'Last Updated' = $FileObject.LastWriteTime Size = $NiceSize RawSize = $Size Owner = (Get-Acl $FileObject.FullName).Owner } } Function CalculateSize { Param ( [double]$Size ) If ($Size -gt 1000000000) { $ReturnSize = "{0:N2} GB" -f ($Size / 1GB) } Else { $ReturnSize = "{0:N2} MB" -f ($Size / 1MB) } Return $ReturnSize } cls $Report = @() $TotSize = 0 $FSO = New-Object -ComObject Scripting.FileSystemObject #First get the properties of the starting path $Root = Get-Item -Path $Path AddObject $Root #Now loop through all the subfolders ForEach ($Folder in (Get-ChildItem -Path $Path | Where { $_.PSisContainer })) { AddObject $Folder } #Create the HTML for our report $Header = @" <style> TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;} TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;} </style> <Title> Folder Sizes for "$Path" </Title> "@ $TotSize = CalculateSize $TotSize $Pre = "<h1>Folder Sizes for ""$Path"" on ""$comp_name"" </h1><h2>Run on $(Get-Date -f 'MM/dd/yyyy hh:mm:ss tt')</h2>" $Post = "<h2>Total Space Used In ""$($Path)"": $TotSize</h2>" #Create the report and save it to a file $Report | Sort RawSize -Descending | Select 'Folder Name',Owner,'Created On','Last Updated',Size | ConvertTo-Html -PreContent $Pre -PostContent $Post -Head $Header | Out-File $ReportPath\FolderSizes.html #Display the report in your default browser & $ReportPath\FolderSizes.html
我希望能夠為伺服器中的每個磁碟執行此操作
為此,您可以使用
Get-PSDrive -PSProvider FileSystem
檢索所有本地驅動器並遍歷結果,如下所示:Param ( [string]$ReportPath = "F:\Monitor_Tasks" ) Function AddObject { Param ( $FileObject ) $Size = [double]($FSO.GetFolder($FileObject.FullName).Size) $Script:TotSize += $Size If ($Size) { $NiceSize = CalculateSize $Size } Else { $NiceSize = "0.00 MB" $Size = 0 } $Script:Report += New-Object PSObject -Property @{ 'Folder Name' = $FileObject.FullName 'Created on' = $FileObject.CreationTime 'Last Updated' = $FileObject.LastWriteTime Size = $NiceSize RawSize = $Size Owner = (Get-Acl $FileObject.FullName).Owner } } Function CalculateSize { Param ( [double]$Size ) If ($Size -gt 1000000000) { $ReturnSize = "{0:N2} GB" -f ($Size / 1GB) } Else { $ReturnSize = "{0:N2} MB" -f ($Size / 1MB) } Return $ReturnSize } #Get the computer name $comp_name = $env:computername #Create the HTML for our report $Header = @" <style> TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;} TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;} TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;} </style> <Title> Folder Sizes for "$comp_name" </Title> "@ cls $FSO = New-Object -ComObject Scripting.FileSystemObject #Initialize empty result file Out-File -Force $ReportPath\FolderSizes.html #Now loop through drives and all the subfolders $Drives = Get-PSDrive -PSProvider FileSystem | select-object Root ForEach ($Drive in $Drives) { $Report = @() $TotSize = 0 $Path = $Drive.Root if(Test-Path $Path) { $Root = Get-Item -Path $Path AddObject $Root ForEach ($Folder in (Get-ChildItem -Path $Path | Where { $_.PSisContainer })) { AddObject $Folder } $TotSize = CalculateSize $TotSize $Pre = "<h1>Folder Sizes for ""$Path"" on ""$comp_name"" </h1><h2>Run on $(Get-Date -f 'MM/dd/yyyy hh:mm:ss tt')</h2>" $Post = "<h2>Total Space Used In ""$($Path)"": $TotSize</h2>" $Report | Sort RawSize -Descending | Select 'Folder Name',Owner,'Created On','Last Updated',Size | ConvertTo-Html -PreContent $Pre -PostContent $Post -Head $Header | Out-File -Append $ReportPath\FolderSizes.html } } #Display the report in your default browser & $ReportPath\FolderSizes.html
此外,我已經處理了驅動器中可能沒有任何 CDRom 的事實,因此,為了避免引發異常,我使用:
if(Test-Path $Path)
對於我擁有的每台伺服器(我有一個包含所有伺服器名稱的文本文件)
好吧,我無法回答這一點,因為正如我在評論中告訴你的那樣:
- 您打算如何訪問這些伺服器?
- 所有伺服器的驅動器是否共享?
- 伺服器是域的成員嗎?
最後,你在評論中說:
我設法獲得了每個磁碟,但是我的伺服器中的每個磁碟都可以正常工作,但是具有我正在執行的腳本的磁碟僅顯示了我的腳本位置下方的文件夾。有誰知道為什麼?
是的
這將指向目前文件夾:
Get-Item -Path "c:"
這將指向根驅動器:
Get-Item -Path "c:\"
我們想要根驅動器,所以你必須使用
Root
屬性 fromGet-PSDrive -PSProvider FileSystem
來獲得類似的東西c:\ d:\
看看我提供的這些特定程式碼部分:
$Drives = Get-PSDrive -PSProvider FileSystem | select-object Root ... ForEach ($Drive in $Drives) { ... $Path = $Drive.Root ... $Root = Get-Item -Path $Path ... }
希望它會有所幫助。