•  

    Blog

    19


    Apr

    How to get the status of host instances with PowerShell

    In my scripts I regularly check the status of host instances on a BizTalk system. For example, if I want to start a host instance in a script, I first check if it isn’t in started state already.

     

    To show how you can check the status of a host instance, I created a small script. The scripts returns the state of all host instances on all servers in the array servers:

     

    $servers = ("BizTalk2010n1", "BizTalk2010n2")
     
    #This function checks the status of the host instances on a BizTalk server ($Server).
    function checkhostinstancestatusstarted ($server)
        {
        #gets all host instances on the server. Isolated (hosttype = 2) or disabled hosts are excluded .
        $hostinstances = get-wmiobject MSBTS_HostInstance -namespace 'root\MicrosoftBizTalkServer' | where {$_.runningserver -match $server -AND $_.hosttype -ne "2" -and $_.IsDisabled -ne "True"}
     
        write-host "Checking the state of all host instances on the server $server`:"
     
        foreach ($hostinstance in $hostinstances)
            {
            $HostInstanceName = $HostInstance.hostname
     
            #Checks the host instance state
            if ($HostInstance.ServiceState -eq 1)
                {
                write-host "$HostInstanceName`: Stopped."
                }
            elseif ($HostInstance.ServiceState -eq 2)
                {
                write-host "$HostInstanceName`: Start pending."
                }
            elseif ($HostInstance.ServiceState -eq 3)
                {
                write-host "$HostInstanceName`: Stop pending."
                }
            elseif ($HostInstance.ServiceState -eq 4)
                {
                write-host "$HostInstanceName`: Started."
                }
            elseif ($HostInstance.ServiceState -eq 5)
                {
                write-host "$HostInstanceName`: Continue pending."
                }
            elseif ($HostInstance.ServiceState -eq 6)
                {
                write-host "$HostInstanceName`: Pause pending."
                }
            elseif ($HostInstance.ServiceState -eq 7)
                {
                write-host "$HostInstanceName`: Paused."
                }
            elseif ($HostInstance.ServiceState -eq 8)
                {
                write-host "$HostInstanceName`: Unknown."
                }
            }
        write-host `n    
        }
     
    foreach ($server in $servers)
        {
        checkhostinstancestatusstarted $server
        }
     
    <#
    Service states:
    Stopped 1
    Start pending   2
    Stop pending    3
    Running 4
    Continue pending    5
    Pause pending   6
    Paused  7
    Unknown 8
     
    See: http://msdn.microsoft.com/en-us/library/aa577472.aspx
    #>
    

    19 April 2012