BTSReset with PowerShell

In a previous post I mentioned that you could work with the WMI interfaces in BizTalk Server easier in PowerShell than you could with C#, and in a more interactive fashion. Here's something else I cooked while playing with the PowerShell RC1: A PowerShell version of my BTSReset tool!

Update: Fixed code so that it works with PowerShell V1 RTM.

#
# declare two switch parameters: -start and -stop
#
param([switch] $start, [switch] $stop)

#
# get list of application hosts
#
function get-apphosts
{
   get-wmiobject MSBTS_HostInstance `
      -namespace 'root\MicrosoftBizTalkServer' `
      -filter HostType=1
}

#
# stop the given host
#
function stop-host($apphost)
{
   $hostname = $apphost.HostName
   if ( $apphost.ServiceState -ne 1 )
   {
      "Stopping Host $hostname ..."
      [void]$apphost.Stop()
   }
}

#
# start the given host
#
function start-host($apphost)
{
   $hostname = $apphost.HostName
   if ( $apphost.ServiceState -eq 1 )
   {
      "Starting Host $hostname ..."
      [void]$apphost.Start()
   }
}

#
# main script
#

if ( !($stop) -and !($start) )
{
   $stop = $true
   $start = $true
}

if ( $stop )
{
   get-apphosts | %{ stop-host($_) }
}

if ( $start )
{
   get-apphosts | %{ start-host($_) }
}

Here's a few examples of running the tool from the PowerShell prompt:

PS E:\temp> .\bts-reset.ps1
Stopping Host BizTalkServerApplication ...
Starting Host BizTalkServerApplication ...

PS E:\temp> .\bts-reset.ps1 -stop
Stopping Host BizTalkServerApplication ...

PS E:\temp> .\bts-reset.ps1 -stop

PS E:\temp> .\bts-reset.ps1 -start
Starting Host BizTalkServerApplication ...

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>