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!

#

#
declare two switch parameters: -start and -stop

#

param([switch] $start,
[
switch] $stop)


 

#

#
get list of application hosts

#

function bts-list-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 …”

      $apphost.invokemethod(‘Stop’, $null)

   }

}


 

#

#
start the given host

#

function start-host($apphost)

{

   $hostname = $apphost.HostName

   if ( $apphost.ServiceState
-eq 1 )

   {

      “Starting
Host $hostname …”

      $apphost.invokemethod(‘Start’, $null)

   }

}


 

#

#
main script

#


 

if (
!(
$stop)
-and !(
$start)
)

{

   $stop = $true

   $start = $true

}


 

if ( $stop )

{

   bts-list-apphosts
| %{ stop-host(
$_)
}

}


 

if ( $start )

{

   bts-list-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 …

>