使用 powershell 调整顶级文件大小

使用 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

答案1

我希望能够对服务器中的每个磁盘执行此操作

为此,您可以用来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:\"

我们想要根驱动器,因此您必须使用属性RootGet-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
  ...
}

希望它会有所帮助。

相关内容