忽略空值

时间:2018-02-14 20:45:30

标签: powershell

我对Null值有疑问。

下面的脚本运行正常,但当我的Z(光驱)中没有任何内容时会返回错误。显然,如果光驱中没有任何东西它基本上是空的,但我还没有找到解决办法。我知道这可能是一个简单的修复,我希望有人可以提供帮助。

Function GetDisk
{
    Param(
        $ComputerName = $env:COMPUTERNAME,
        [Switch]$PassThru
    )
    # ... 
}

Function Get-ColorSplat
{
    # Create color Splats
    $C1 = @{ForegroundColor="Green"}
    $C2 = @{ForegroundColor="Yellow"}
    $C3 = @{ForegroundColor="Red"}


    # Create color constants in the previous scope.
    New-Variable -Name "Good" -Value $C1 -Scope 1
    New-Variable -Name "Problem" -Value $C2 -Scope 1
    New-Variable -Name "Bad" -Value $C3 -Scope 1

} # End: Get-ColorSplat

Function Write-ColorOutput
{
    Param($DiskInfo)

    Write-Host""
    Write-Host -NoNewLine "OS Version: "

    Get-CimInstance Win32_OperatingSystem | Select-Object  Caption | ForEach{ $_.Caption }

    Write-Host""

    # Display the headers.
    Write-host "SystemName DeviceID VolumeName         Size(GB)   FreeSpace(GB) %FreeSpace(GB)  Date"
    Write-host "---------- -------- -----------        --------   ------------  --------------  ----------------"

    # Display the data.
    ForEach ($D in $DiskInfo)
    {
        $PSComputerName = $D.PSComputerName.PadRight(6)
        $DeviceID = $D.DeviceID.ToString().PadRight(6).Remove(5)
        $VolumeName = $D.VolumeName.ToString().PadRight(20).Remove(19)
        $SizeGB = $D.SizeGB.ToString().PadRight(6).Remove(5)
        $FSGB = $D.FreeSpaceGB.ToString().PadRight(6).Remove(5)
        $PFS = $D.PercentFS.ToString().PadRight(7).Remove(6)
        $Date = $D.Date.ToString().PadRight(20).Remove(19)

        If ($D.PercentFS -ge 50)
        { Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)      $($FSGB)         $($PFS)          $($Date)" @Good }
        ElseIf (($D.PercentFS -lt 50) -and ($D.PercentFS -GE 40))
        { Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)      $($FSGB)         $($PFS)          $($Date)" @Problem }
        Else
        { Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)      $($FSGB)         $($PFS)          $($Date)" @Bad }
     }
}

# Get the color splats
Get-ColorSplat

    $DiskInfo = Get-WMIObject win32_logicalDisk -ComputerName $env:computername | Select-Object PSComputerName, DeviceID, Size, VolumeName,
        @{name="SizeGB"; expression={"{0:N2}" -f ($_.Size/1gb)}},
        @{Name="FreeSpaceGB"; expression={"{0:N2}" -f ($_.FreeSpace/1gb)}},
        @{Name="PercentFS"; expression={"{0:N2}%" -f(($_.FreeSpace/$_.size)*100)}},
        @{name="Date"; expression={$(Get-Date -Format "g")}} 
        #$ErrorActionPreference= 'silentlycontinue'

    If (!$PassThru) {
        Write-ColorOutput -DiskInfo $DiskInfo
    }
    Else {
        Write-Output $DiskInfo
    }
}

enter image description here

2 个答案:

答案 0 :(得分:0)

你不得不遇到问题,$ D的几个值都返回$ null,但你没有检查它们的值(例如$ D.VolumeName)。 第二个问题是你要多次创建全局变量(例如New-Variable -Name" Good")。

我建议你重构,以便变量创建在" Get-ColorSplat"如果你打算多次打电话。

在输出表中编辑#1固定填充

Function GetDisk
{
    Param(
        $ComputerName = $env:COMPUTERNAME,
        [Switch]$PassThru
    )
    # ... 
}

Function Get-ColorSplat
{
    # Create color Splats
    $C1 = @{ForegroundColor="Green"}
    $C2 = @{ForegroundColor="Yellow"}
    $C3 = @{ForegroundColor="Red"}


    # Create color constants in the previous scope.
    if(Get-Variable Good -Scope Global -ErrorAction SilentlyContinue){}
    else{New-Variable -Name "Good" -Value $C1 -Scope 1}

    if(Get-Variable Problem -Scope Global -ErrorAction SilentlyContinue){}
    else{New-Variable -Name "Problem" -Value $C2 -Scope 1}

    if(Get-Variable Bad -Scope Global -ErrorAction SilentlyContinue){}
    else{New-Variable -Name "Bad" -Value $C3 -Scope 1}

} # End: Get-ColorSplat

Function Write-ColorOutput
{
    Param($DiskInfo)

    Write-Host""

    Write-Host -NoNewLine "OS Version: "

    Get-CimInstance Win32_OperatingSystem | Select-Object  Caption | ForEach{ $_.Caption }

    Write-Host""

    # Display the headers.
    Write-host "SystemName DeviceID VolumeName         Size(GB)   FreeSpace(GB) %FreeSpace(GB)  Date"
    Write-host "---------- -------- -----------        --------   ------------  --------------  ----------------"

    # Display the data.
    ForEach ($D in $DiskInfo)
    {
    $PSComputerName = $D.PSComputerName.PadRight(6)
    $DeviceID = $D.DeviceID.ToString().PadRight(6).Remove(5)

    $Dname = $D.VolumeName
    if($Dname){$VolumeName =     $D.VolumeName.ToString().PadRight(20).Remove(19)}
    else{$VolumeName = '                   '}

    $SizeGB = $D.SizeGB.ToString().PadRight(6).Remove(5)
    $FSGB = $D.FreeSpaceGB.ToString().PadRight(6).Remove(5)

    $PercentFS = $D.PercentFS
    if($PercentFS){$PFS = $D.PercentFS.ToString().PadRight(7).Remove(6)}
    else{$PFS = '      '}

    $Date = $D.Date.ToString().PadRight(20).Remove(19)

    If ($D.PercentFS -ge 50)
    { Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)      $($FSGB)         $($PFS)          $($Date)" @Good }
    ElseIf (($D.PercentFS -lt 50) -and ($D.PercentFS -GE 40))
    { Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)      $($FSGB)         $($PFS)          $($Date)" @Problem }
    Else
    { Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)      $($FSGB)         $($PFS)          $($Date)" @Bad }

    }
}

# Get the color splats
Get-ColorSplat

$DiskInfo = Get-WMIObject win32_logicalDisk -ComputerName     $env:computername | Select-Object PSComputerName, DeviceID, Size, VolumeName,
        @{name="SizeGB"; expression={"{0:N2}" -f ($_.Size/1gb)}},
        @{Name="FreeSpaceGB"; expression={"{0:N2}" -f ($_.FreeSpace/1gb)}},
        @{Name="PercentFS"; expression={"{0:N2}%" -f(($_.FreeSpace/$_.size)*100)}},
        @{name="Date"; expression={$(Get-Date -Format "g")}} 
        #$ErrorActionPreference= 'silentlycontinue'

 If (!$PassThru) {Write-ColorOutput -DiskInfo $DiskInfo}
 Else {Write-Output $DiskInfo}

答案 1 :(得分:0)

弄清楚了 - 感谢所有评论和帮助我的人。

功能GetDisk {     参数(         $ ComputerName = $ env:COMPUTERNAME,         [转] $的PASSThru     )

Function Write-ColorOutput
{  
    Param($DiskInfo)

    Write-Host""

    Write-Host -NoNewLine "OS Version: "

    Get-CimInstance Win32_OperatingSystem | Select-Object  Caption | ForEach{ $_.Caption }

    Get-CimInstance Win32_OperatingSystem | Select-Object CSName, InstallDate, BuildNumber, OSArchitecture, BootDevice | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Green

    Get-PhysicalDisk | Select-Object $env:computername, FriendlyName, SerialNumber, MediaType, OperationalStatus, HealthStatus | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Green

    Write-Host""

    # Display the headers.
    Write-host "SystemName DeviceID VolumeName         Size(GB)  FreeSpace(GB) %FreeSpace(GB)  Date"
    Write-host "---------- -------- -----------        --------  ------------  --------------  ----------------"

    # Display the data.
    ForEach ($D in $DiskInfo)
    {
        $PSComputerName = $D.PSComputerName.PadRight(6)
        $DeviceID = $D.DeviceID.ToString().PadRight(6).Remove(5)
        $Dname = $D.VolumeName
        if($Dname){$VolumeName = $D.VolumeName.ToString().PadRight(20).Remove(19)} else{$VolumeName = '                   '}
        $SizeGB = $D.SizeGB.ToString().PadRight(6).Remove(5)
        $FSGB = $D.FreeSpaceGB.ToString().PadRight(6).Remove(5)
        $PercentFS = $D.PercentFS
        if($PercentFS){$PFS = $D.PercentFS.ToString().PadRight(7).Remove(6)} else{$PFS = '      '}
        $Date = $D.Date.ToString().PadRight(20).Remove(19)

        If ($D.PercentFS -ge 50)
         {Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)     $($FSGB)         $($PFS)          $($Date)" -ForegroundColor Green}
        ElseIf (($D.PercentFS -lt 50) -and ($D.PercentFS -GE 40))
         {Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)     $($FSGB)         $($PFS)          $($Date)" -ForegroundColor Yellow}
        Else 
         {Write-Host "$PSComputerName    $($DeviceID)    $($VolumeName)$($SizeGB)     $($FSGB)         $($PFS)          $($Date)" -ForegroundColor Red} 

    }

    Get-PhysicalDisk | Get-StorageReliabilityCounter | Select-Object $env:computername, PowerOnHours, Temperature, Wear, ReadErrorsTotal, WriteErrorsTotal, FlushLatencyMax, WriteLatencyMax | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Green

    Disk | Select-Object | Format-Table -AutoSize | Out-String | Write-Host -ForegroundColor Green
}

    $DiskInfo = Get-WMIObject win32_logicalDisk -ComputerName $env:computername | Select-Object PSComputerName, DeviceID, Size, VolumeName,

        @{name="SizeGB"; expression={"{0:N2}" -f ($_.Size/1gb)}},
        @{Name="FreeSpaceGB"; expression={"{0:N2}" -f ($_.FreeSpace/1gb)}},
        @{Name="PercentFS"; expression={"{0:N2}%" -f(($_.FreeSpace/$_.size)*100)}},
        @{name="Date"; expression={$(Get-Date -Format "g")}} 
        #$ErrorActionPreference= 'silentlycontinue'



 If (!$PassThru) {Write-ColorOutput -DiskInfo $DiskInfo} Else {Write-Output $DiskInfo}

 Write-Host""
 Write-Host""

}