–human-readable

Очень часто получаешь в скрипте вывод в байтах. Если речь идет об очень больших цифрах, то чтобы понять приходится считать разряды :)

Написал маленькую функу которая форматирует число байт в “human readable” формат :) Функцию полезно засунуть в профиль.

Function FB ([double]$bytes){ switch($bytes){ {$bytes -le 1kb} {"$bytes B"; break} {$bytes -le 1mb} {"$([int]($bytes/1kb)) KB"; break} {$bytes -le 1gb} {"$([int]($bytes/1mb)) MB"; break} default {"$([int]($bytes/1gb)) GB"; break} } }

Например так можно посмотреть свободное место на дисках используя WMI:

PS C:\PowerShell> Get-WmiObject Win32_LogicalDisk | >> where {$_.drivetype -eq 3} | >> Format-Table DeviceId, VolumeName, {fb $_.size}, {fb $_.freespace} DeviceId VolumeName fb $_.size fb $_.freespace -------- ---------- ---------- --------------- C: Main 122 GB 28 GB E: Backup 27 GB 20 GB
Опубликовано в PowerShell. Комментарии (4) »

Комментариев: 4 на “–human-readable”

  1. Aleksandar пишет:

    We can give each column in the table a more meaningful title.

    Format-Table DeviceId, VolumeName, @{Label=”Size”;Expression={fb $_.size}}, @{Label=”Free”;Expression={fb $_.freespace}}

    DeviceId VolumeName Size Free
    ——– ———- —- —-
    C: Main 122 GB 28 GB
    E: Backup 27 GB 20 GB

    We can even control the width of the column @{Label=”Size”;Expression={fb $_.size};Width=15} or if we want pithy version @{l=”Size”;e={fb $_.size};w=15}.

  2. xaegr пишет:

    2 Aleksandar:
    Tnx for comment, I know about this :) I’m just trying to simplify the code for new users :)

  3. Aleksandar пишет:

    I know that you know, I left the comment for new users. :-)


Обсуждение закрыто.

Follow

Get every new post delivered to your Inbox.

Join 30 other followers