$computers = Get-Content -Path c:\server\computers.txt
$computers | foreach {
$os = Get-WmiObject win32_OperatingSystem -computername $_
Get-WMIObject Win32_Logicaldisk -filter "deviceid='$($os.systemdrive)'" -ComputerName $_
} | Select PSComputername,DeviceID,
@{Name="SizeGB";Expression={$_.Size/1GB -as [int]}},
@{Name="FreeGB";Expression={[math]::Round($_.Freespace/1GB,2)}} |
Sort FreeGB | Format-Table -Autosize >> C:\server\diskreport.txt
答案 0 :(得分:0)
由于确实没有问题或存在任何错误,这就是我用来获取服务器磁盘空间的方法。您总是可以获取一个列表,并将其放入foreach循环中,然后将它们发送到此函数,然后将输出随意地输出到文件中。
Function Get-DiskInventory
{
[CmdletBinding()]
Param (
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
[alias("Name")]
[string[]]$ComputerName="localhost",
[validateset(2,3)]
[int]$DriveType=3,
[Parameter()]
[string]$DriveLetter
)
Begin{
Write-Verbose "Getting disk inventory on $ComputerName"
}
Process {
foreach ($Computer in $ComputerName) {
Write-Verbose "Connecting to $Computer"
Write-Verbose "Looking for drive type $DriveType"
$Result = Get-WmiObject -Class win32_logicaldisk -ComputerName $Computer -Filter "drivetype=$DriveType" |
Select-Object -Property @{label='Computer';expression={$Computer}},
DeviceID,
@{label='Size(GB)';expression={$_.Size / 1GB -as [int]}},
@{label='UsedSpace(GB)';expression={($_.Size - $_.FreeSpace) / 1GB -as [int]}},
@{label='FreeSpace(GB)';expression={$_.FreeSpace / 1GB -as [int]}},
@{label='%Free';expression={$_.FreeSpace / $_.Size * 100 -as [int]}}
if ($DriveLetter) {
Write-Verbose "Filtering drives"
$Result = $Result | where deviceid -EQ "$DriveLetter`:"
}
$Result | Select-Object -Property * -Unique
}
}
End{
Write-Verbose "Finished running command"
}
}