Thursday, 19 April 2012 13:56

How to get the status of host instances with PowerShell

Written by 
Rate this item
(2 votes)

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 is the script (You can also download it below):

$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
#>

Read 5984 times Last modified on Thursday, 19 April 2012 14:59
Jeroen Hendriks

Jeroen Hendriks started his career in 2005 as a middleware administrator. One of the products he was responsible for was BizTalk Server 2004. Since that time he mainly focusses on designing, implementing and supporting BizTalk Server infrastructures. Currently he works as a consultant for Axon Olympus. He hopes that his day-to-day experiences will result in useful and practical blog posts. His certifications are MCSE, Certified Ethical Hacker, MCITP: Enterprise Administrator, Server Administrator and Database Administrator 2008.

twitterlinkedin

Website: www.axonolympus.nl

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.