Kako najti Disk zmogljivosti in prosta površina računalnikih

Iskanje disk zmogljivosti in prostega prostora na lokalni računalnik je enostavna, vendar ni tako enostavno iz oddaljenega računalnika, predvsem preko GUI vmesnika. To je veliko lažje uporabiti moč PowerShell in tukaj je, kako lahko to storite.

Get-PSDrive is a native PowerShell cmdlet that lists all storage drives on your local system.

You can narrow down to only list the file systems by piping out the result to a Where clause.

Get-PSDrive | Where {$_.Free -gt 0}

Since the cmdlet doesn’t have a -ComputerName switch to access remote computers, we need Invoke-Command to run the cmdlet on a remote computer.

Invoke-Command -ComputerName remote_computer {Get-PSDrive | Where {$_.Free -gt 0}}

This works pretty well only when you have WinRM and PSRemoting enabled on the remote computers. And that’s why I like the Get-WmiObject method even better.

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, FreeSpace, Velikost

To list the size in GB format,

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}

How about display the result with the thousands separators?

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}

You can access multiple remote computers from one run by putting all of them after the -ComputerName stikalo, separated by a comma.

Get-WmiObject Win32_LogicalDisk -ComputerName computer1,computer2,computer3 -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}

tudi, here is the script I put together that has a better formatted output.

$servers = @("computer1", "computer2", "computer3")

Foreach ($server in $servers)
{
    $disks = Get-WmiObject Win32_LogicalDisk -ComputerName $server -Filter DriveType=3 | 
        Select-Object DeviceID, 
            @{'Name'='Size'; 'Expression'={[math]::truncate($_.size / 1GB)}}, 
            @{'Name'='Freespace'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}

    $server

    foreach ($disk in $disks)
    {
        $disk.DeviceID + $disk.FreeSpace.ToString("N0") + "GB / " + $disk.Size.ToString("N0") + "GB"

     }
 }

vir

Related Post

Pustite Odgovori

Ta stran uporablja Akismet zmanjšati spam. Več o tem, kako se obdelujejo vaš komentar podatki.