Search code examples
powershellwmipowershell-2.0

How to refresh properties of a wmi object after a method call, in powershell script


I am looping through a set of wmi objects and calling a function on it. I need to get a property after the function all, but don't know how to refresh the object. Do all objects have some sofr of refresh function?

$hosts = Get-WmiObject MSBTS_HostInstance -Namespace 'root/MicrosoftBizTalkServer'
    
foreach($hostInstance in $hosts) 
{       
    $name = $hostInstance.hostname
    $state = $hostInstance.ServiceState
    $stateName = Get-ServiceStateDescription $state
    
    Write-Diagnostic "Biztalk host instance '$name'. State: $stateName ($state)."
    
    #If started
    if (($hostInstance.ServiceState -eq 4))  
    {
      Write-Diagnostic "Stopping host instance '$name'..."
      $hostInstance.Stop()
      
      $state = $hostInstance.ServiceState
      $stateName = Get-ServiceStateDescription $state
      Write-Diagnostic "Stopped host instance '$name' stopped.  State: $stateName ($state)".
    }       
    
    #If stopped
    if (($hostInstance.ServiceState -eq 1))  
    {
        Write-Diagnostic "Starting host instance '$name'..."
        $r = $hostInstance.Start()
      
        $state = $hostInstance.ServiceState         
        $stateName = Get-ServiceStateDescription $state
        Write-Diagnostic "Starting host instance '$name' started.  State: $stateName ($state)." 
    }
}

Solution

  • Thank you Daniel and Theo. Posting your suggestion as an answer to help community members.

    "MSBTS_HostInstance does not appear to but it does include a GetState() method if that is what you are after. MSBTS_HostInstance (WMI)"

    "To refresh the data you may need to do something like $hostInstance = Get-WmiObject MSBTS_HostInstance -Namespace 'root/MicrosoftBizTalkServer' | Where-Object { $_.hostname -eq $hostInstance.hostname }"