powershell - 将函数作为计算属性调用以显示MB和GB Powershell脚本

标签 powershell

如何修改下面的Powershell代码以查询Linux磁盘大小,以便可以在列中将其查看为MBytes还是Gigabytes?
脚本:

#requires -version 2
function DisplayInBytes($num)
{
    $suffix = "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"
    $index = 0
    while ($num -gt 1kb)
    {
        $num = $num / 1kb
        $index++
    }
    
    "{0:N1} {1}" -f $num, $suffix[$index]
}

Function Format-FileSize()
{
    Param ([int]$size)
    If ($size -gt 1TB) { [string]::Format("{0:0.00} TB", $size / 1TB) }
    ElseIf ($size -gt 1GB) { [string]::Format("{0:0.00} GB", $size / 1GB) }
    ElseIf ($size -gt 1MB) { [string]::Format("{0:0.00} MB", $size / 1MB) }
    ElseIf ($size -gt 1KB) { [string]::Format("{0:0.00} kB", $size / 1KB) }
    ElseIf ($size -gt 0) { [string]::Format("{0:0.00} B", $size) }
    Else { "" }
}


function ConvertFrom-LinuxDfOutput
{
    param ([string]$Text)
    [regex]$HeaderRegex = '\s*File\s*system\s+1024-blocks\s+Used\s+Available\s+Capacity\s+Mounted\s*on\s*'
    [regex]$LineRegex = '^\s*(.+?)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+\s*%)\s+(.+)\s*$'
    $Lines = @($Text -split '[\r\n]+')
    if ($Lines[0] -match $HeaderRegex)
    {
        foreach ($Line in ($Lines | Select -Skip 1))
        {
            [regex]::Matches($Line, $LineRegex) | foreach {
                New-Object -TypeName PSObject -Property @{
                    Filesystem = $_.Groups[1].Value
                    '1024-blocks' = [decimal]$_.Groups[2].Value
                    Used       = [decimal]$_.Groups[3].Value
                    Available  = [decimal]$_.Groups[4].Value
                    CapacityPercent = [decimal] ($_.Groups[5].Value -replace '\D')
                    MountedOn  = $_.Groups[6].Value
                } | Select Filesystem, 1024-blocks, Used, Available, CapacityPercent, MountedOn
            }
        }
    }
    else
    {
        Write-Warning -Message "Error in output. Failed to recognize headers from 'df --portability' output."
    }
}
资料来源:https://www.powershelladmin.com/wiki/Get_Linux_disk_space_report_in_PowerShell
因为以下结果很难用HTML电子邮件解释。
PS C:\ PS> ConvertFrom-LinuxDfOutput -Text $ dfoutput [0] |英尺
Filesystem 1024-blocks    Used Available CapacityPercent MountedOn
---------- -----------    ---- --------- --------------- ---------
udev           1014160       4   1014156               1 /dev
tmpfs           204992    1332    203660               1 /run
/dev/sda1     23606716 7834904  14549620              36 /
none                 4       0         4               0 /sys/fs/cgroup
none              5120       0      5120               0 /run/lock
none           1024960     144   1024816               1 /run/shm
none            102400      32    102368               1 /run/user     
我可以这样称呼它吗:

Used = Format-FileSize(DisplayInBytes([decimal]$.Groups[3].Value))
Available = Format-FileSize(DisplayInBytes([decimal]$
.Groups[4].Value))

最佳答案

看来您只需要其中之一。
像其他任何PowerShell命令一样调用它。 Command-Name -ParameterName $argumentValue:

New-Object -TypeName PSObject -Property @{
    Filesystem      = $_.Groups[1].Value
    '1024-blocks'   = [decimal]$_.Groups[2].Value
    Used            = DisplayInBytes -num ([decimal]$_.Groups[3].Value)
    Available       = DisplayInBytes -num ([decimal]$_.Groups[4].Value)
    CapacityPercent = [decimal]($_.Groups[5].Value -replace '\D')
    MountedOn       = $_.Groups[6].Value
}

关于powershell - 将函数作为计算属性调用以显示MB和GB Powershell脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63547732/

相关文章:

powershell - 合并不同目录中的同名 .txt 文件?

powershell - 来自 cs 的 Add-Type,需要引用程序集

powershell - 如何从 Foreach 循环中获取添加到集合中的 Powershell 对象的所有值?

powershell - 如何在没有内存问题的情况下浏览 Azure Blob

PowerShell 更改文件和文件夹的所有者

security - 验证 PowerShell PSCredential

powershell - 在Powershell中比较两个CSV对象

.net - PowerShell 通用集合

.net - 仅使用 .NET GetBytes 方法转换有效字节而不创建问号

powershell - PowerShell 是否具有 Infinity 值