简体   繁体   中英

new-object PSObject causes null-valued expression error

My PowerShell script:

Param([string]$Computers) #Must supply a comma seperated list of servers
$Threshold = 20 #Only show CPU over this number
$NoP = 20 #Number of processes to list
$NoRS = 4 #Number of result sets
If (! $Computers) { 
    Write-Host "Connection to server failed - please specify a server name." -ForegroundColor Red
    Break
} Else {
    $ComputerList = $Computers -Split " "#,[StringSplitOptions]'RemoveEmptyEntries')
}
$Credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBiosUserName")
If (! $Credential) { 
    Write-Host "Authentication failed - please verify your username and password." -ForegroundColor Red
    Break
}
$UserName = $Credential.Username
$Password = $Credential.GetNetworkCredential().Password
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
$Domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
If ($Domain.Name -eq $null){
    Write-Host "Authentication failed - please verify your username and password." -ForegroundColor Red
    Break
}
ForEach ($ComputerName In $ComputerList) {
    $LoadPercentage = $Processors.LoadPercentage
    If (!$LoadPercentage) {$LoadPercentage = 0}
    Write-Host "Server: $ComputerName (CPU load $LoadPercentage%)" -NoNewline
    $Processors = Get-WmiObject win32_processor -ComputerName $ComputerName -Credential $Credential
    $i = 1
    $TopProcess = @()
    $PercentComplete = 0
    Do{
        $PercentComplete = [Math]::Floor($i/$NoRS*100)
        Write-Progress -Activity $ComputerName -Status "$PercentComplete% Complete:" -PercentComplete $PercentComplete
        $ProcessList = gwmi Win32_PerfFormattedData_PerfProc_Process -ComputerName $ComputerName -Credential $Credential | 
            Select IDProcess,Name,PercentProcessorTime | 
            Where {$_.Name -ne "_Total" -and $_.Name -ne "Idle"} | 
            Sort PercentProcessorTime -Descending | 
            Select -First $NoP
        ForEach ($Process In $ProcessList) {
            $row = New-Object PSObject -Property @{
                Id = $Process.IDProcess
                Name = $Process.Name
                User = (gwmi Win32_Process -ComputerName $ComputerName -Credential $Credential | Where {$_.ProcessId -eq $Process.IDProcess}).GetOwner().User
                CPU = $Process.PercentProcessorTime/$Processors.NumberOfLogicalProcessors -f {P}
                Description = (gwmi Win32_Process -ComputerName $ComputerName -Credential $Credential | Where {$_.ProcessId -eq $Process.IDProcess}).Description 
            }
            $TopProcess += $row
        }
        $i++
    } While ($i -lt $NoRS + 1)
    Write-Progress -Activity $ComputerName -Completed
    $Group = $TopProcess | Where {$_.CPU -gt $Threshold} | Group 'ID' | Where Count -eq $NoRS
    If (!$Group) {
        Write-Host " has no processes persistently above $Threshold percent CPU usage."
    } Else {
        $Processes = @()
        ForEach ($Groupee In $Group) {
            $Ungroup = $Groupee | Select -ExpandProperty Group
            $CPU = 0
            ForEach ($ugr in $Ungroup) {
                $CPU += $ugr.CPU
            } 
            $row = new-object PSObject -Property @{
                Id = $Ungroup.Id | Select -First 1
                Name = $Ungroup.Name | Select -First 1
                CPU = $CPU/$NoRS
                User = $Ungroup.User | Select -First 1
                Description = $Ungroup.Description | Select -First 1
            }
            $Processes += $row
        }
        $Processes | Format-Table @{Expression={$_.User};Label="User Name";width=25},@{Expression={$_.CPU};Label="CPU";width=5},@{Expression={$_.Id};Label="ID";width=8},@{Expression={$_.Description};Label="Description";width=48}
    }
}

intermittantly gives the following error:

You cannot call a method on a null-valued expression. At C:\\Users\\Jasons1\\CPUusage.ps1:41 char:4

  • $row = new-object PSObject -Property @{

  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

which I fail to understand as it is within a loop and should either work or get skipped as there is a test for null.

Pretty sure that your issues are stemming from this line:

 User = (gwmi Win32_Process -ComputerName $ComputerName -Credential $Credential | Where {$_.ProcessId -eq $Process.IDProcess}).GetOwner().User 

Specifically from .GetOwner() . Your where clause must not be finding a matching process for that item while it is in the loop. I realize there is not much time elapsed but WMI queries are not the fastest things out there.

What is happening is likely a result of a process queried earlier in $ProcessList = gwmi Win32_PerfFormattedData_PerfProc_Process and then later when you are using gwmi Win32_Process the list of processes changed. You need to account for this as well. Time has elapsed and threads do not live forever.

$queryResult = gwmi Win32_Process -ComputerName $ComputerName -Credential $Credential | Where {$_.ProcessId -eq $Process.IDProcess}
$owner = if($queryResult){$queryResult.GetOwner().User}else{"Process DNE"}
#...

User = $owner

Not very pretty but accounts for the potential of a null return from the wmi query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM