如何在方法调用后刷新 wmi 对象的属性,在 powershell 脚本中

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

我正在遍历一组 wmi 对象并在其上调用一个函数。我需要在所有函数之后得到一个 属性,但不知道如何刷新对象。是不是所有对象都有刷新功能?

$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)." 
    }
}

谢谢Daniel and Theo。将您的建议作为答案发布以帮助社区成员。

"MSBTS_HostInstance 似乎没有,但它确实包含一个 GetState() 方法,如果这是您所追求的。MSBTS_HostInstance (WMI)"

“要刷新数据,您可能需要执行 $hostInstance = Get-WmiObject MSBTS_HostInstance -Namespace 'root/MicrosoftBizTalkServer' | Where-Object { $_.hostname -eq $hostInstance.hostname }”之类的操作