错误因为您无法在空值表达式上调用方法?

时间:2015-12-07 23:14:57

标签: question2answer

Import-Module OperationsManager
    $loc= Get-Location
    $servers= Get-Content -path $Loc\agentlist.txt
    foreach($server in $servers)
    {$agent = Get-SCOMAgent -DNSHostName "$server"
    $primaryMS = $agent.GetPrimaryManagementServer()
    write-host "$server has Current Primary ManagementServer: "$primaryMS.Name
    Write-Host "----------------------------------------------------" -ForegroundColor Cyan
    }foreach($server in $servers)
    {
    $agent = Get-SCOMAgent -DNSHostName "$server"
    $failoverMSs = $agent.GetFailoverManagementServers()
    write-host "Got"$failoverMSs.Count"failover Management Servers for $server"
    foreach($failoverMS in $failoverMSs) {
     $failoverMS.Name
     Write-Host "------------------------------------" -ForegroundColor Cyan
    }
    }

当我运行上面的代码时,我收到如下错误。

 You cannot call a method on a null-valued expression. At
 C:\Untitled1.ps1:9 char:1
 + $primaryMS = $agent.GetPrimaryManagementServer()
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
     + FullyQualifiedErrorId : InvokeMethodOnNull   DB3CRMS106APW02 has Current Primary ManagementServer:  
 ---------------------------------------------------- You cannot call a method on a null-valued expression. At C:\Untitled1.ps1:19 char:1
 + $failoverMSs = $agent.GetFailoverManagementServers()
 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
     + FullyQualifiedErrorId : InvokeMethodOnNull   Got 0 failover Management Servers for DB3CRMS106APW02

0 个答案:

没有答案