获取 C:来自多个服务器的驱动器容量


$computers = Get-Content -Path c:servercomputers.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:serverdiskreport.txt

由于确实没有针对您拥有的内容发布任何问题或给出错误,因此这是我用来获取服务器上磁盘空间的方法。你总是可以获取一个列表,并将其扔到一个 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"
}
}

相关内容

最新更新