我正在尝试在远程计算机路径上获取文件夹结构和每个文件夹大小。我能够获得文件夹结构,但文件夹大小返回标准数字901.00 KB,无论我指定的任何文件夹路径。我可以看到脚本能够正确获取远程文件夹路径,但不能获取文件/文件夹大小。
这是我到目前为止提出的代码。
param(
[Parameter(ValueFromPipeline=$True, Mandatory=$True)]
[System.String] $ComputerName,
[Parameter(ValueFromPipeline=$True, Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[System.String] $Path
)
function Get-FolderSize ($_ = (get-item .)) {
Process {
$RemoteServer = @($ComputerName)
ForEach($Computer in $ComputerName) {
$ErrorActionPreference = "SilentlyContinue"
$length = Invoke-Command -ComputerName $ComputerName { (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum
} -ArgumentList $Path
$obj = New-Object PSObject
$obj | Add-Member NoteProperty Folder ($_.FullName)
$obj | Add-Member NoteProperty Length ($length)
Write-Output $obj
}
}
}
Function Class-Size($size)
{
IF($size -ge 1GB)
{
"{0:n2}" -f ($size / 1GB) + " GB"
}
ELSEIF($size -ge 1MB)
{
"{0:n2}" -f ($size / 1MB) + " MB"
}
ELSE
{
"{0:n2}" -f ($size / 1KB) + " KB"
}
}
Get-ChildItem $Path | Get-FolderSize | Sort-Object -Property Length -Descending | Select-Object -Property Folder, Length |
Format-Table -Property Folder, @{ Label="Size of Folder" ; Expression = {Class-Size($_.Length)} }
提前致谢!!
答案 0 :(得分:3)
-ComputerName $Computer
$_.FullName
错误以及其他许多人,请查看更新的脚本
Param(
[Parameter(ValueFromPipeline=$True, Mandatory=$True)]
[Array] $Computers,
[Parameter(ValueFromPipeline=$True, Mandatory=$True, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[System.String] $Path
)
Function Class-Size($size)
{
IF($size -ge 1GB)
{
"{0:n2}" -f ($size / 1GB) + " GB"
}
ELSEIF($size -ge 1MB)
{
"{0:n2}" -f ($size / 1MB) + " MB"
}
ELSE
{
"{0:n2}" -f ($size / 1KB) + " KB"
}
}
function Get-FolderSize
{
Param(
$Path, [Array]$Computers
)
$Array = @()
Foreach($Computer in $Computers)
{
$ErrorActionPreference = "SilentlyContinue"
$Length = Invoke-Command -ComputerName $Computer -ScriptBlock {
(Get-ChildItem $args[0] -Recurse | Measure-Object -Property Length -Sum).Sum
} -ArgumentList $Path
$Result = "" | Select Computer,Folder,Length
$Result.Computer = $Computer
$Result.Folder = $Path
$Result.Length = Class-Size $length
$array += $Result
}
return $array
}
Get-FolderSize -Computers $Computers -Path $Path
运行它:
.\Get-Size.ps1 -Computers Computer1,Computer2 -Path c:\windows\system32
Computer Folder Length
-------- ------ ------
Computer1 c:\windows\system32 2.96 GB
Computer2 c:\windows\system32 3.43 GB