'How to solve an error "you cannot call a method on a null-valued expression"?
While I have written the below code. while Migrating all the servers
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
}
}
When I am running the above code I'm getting error as below.
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
