BTSReset with PowerShell

Link. September 2, 2006. Comments [2]. Posted in: BizTalk | 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 ...



Saturday, September 02, 2006 2:34:54 AM (SA Pacific Standard Time, UTC-05:00)
Nice job -- looks like you managed to squeeze out quite a few lines of code while you were at it :)
Saturday, September 02, 2006 1:47:27 PM (SA Pacific Standard Time, UTC-05:00)
Very nice.

PSMDTAG:FAQ: How do I start/stop BizTalk Server Applications?

PSMDTAG:TYPE:WMI: MSBTS_HostInstance

Jeffrey Snover [MSFT]
Windows PowerShell/Aspen Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Jeffrey Snover
Comments are closed.

Syndicate

About

Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...

tomasrestrepo @ twitter My Flickr photostream My saved links on delicious My Technorati Profile

email: tomas@winterdom.com
msn: tomasr@passport.com

View my profile on LinkedIn

MVP logo

Ads


Categories

Statistics

Total Posts: 1050
This Year: 1
This Month: 1
This Week: 0
Comments: 825

Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 2.2.8279.16125

Sign In