Question:
I have been using the excellent BizTalk Provider for PowerShell from Codeplex for a while now. However in my new company the build team are not comfortable using it so I need to rewrite all my handy configuration scripts to not use it.
I have re-done most of them but am having some problems finding examples of how to do the following two things:
- Create host instances
- Create send/receive adapter handers
Can anyone point me to examples of how to do these things WITHOUT the PowerShell provider for BizTalk please? I have done some looking and it seems like everyone uses this now.
Many thanks in advance.
Answer:
OK here are my finished hacky powershell functions if anyone else needs them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
function write-WarnMessage([string]$message) { Write-Host $(Get-Date) $message -ForegroundColor Yellow } function write-SucessMessage([string]$message) { Write-Host $(Get-Date) $message -ForegroundColor Green } function write-InfoMessage([string]$message) { Write-Host $(Get-Date) $message -ForegroundColor Blue -BackgroundColor White } function write-ErrorMessage ([string]$message) { Write-Host $(Get-Date) $message -ForegroundColor Red } # Gets the execution directory function Get-ScriptDirectory { $Invocation = (Get-Variable MyInvocation -Scope 1).Value Split-Path $Invocation.MyCommand.Path } function Delete-Bts-Receive-Handler ( [string]$adapter, [string]$hostName ) { try { [System.Management.ManagementObject]$objHandler = get-wmiobject 'MSBTS_ReceiveHandler' -namespace 'root\MicrosoftBizTalkServer' -filter "HostName='$hostName' AND AdapterName='$adapter'" $objHandler.Delete() write-SucessMessage "Deleted $adapter receive handler for $hostName" } catch [System.Management.Automation.RuntimeException] { if ($_.Exception.Message -eq "You cannot call a method on a null-valued expression.") { write-WarnMessage "$adapter receive handler for $hostName does not exist" } elseif ($_.Exception.Message.IndexOf("Cannot delete a receive handler that is used by") -ne -1) { write-WarnMessage "$adapter receive handler for $hostName is in use. Cannot delete." } else { write-Error "$adapter receive handler for $hostName could not be deleted: $_.Exception.ToString()" } } } function Delete-Bts-Send-Handler ( [string]$adapter, [string]$hostName ) { try { [System.Management.ManagementObject]$objHandler = get-wmiobject 'MSBTS_SendHandler2' -namespace 'root\MicrosoftBizTalkServer' -filter "HostName='$hostName' AND AdapterName='$adapter'" $objHandler.Delete() write-SucessMessage "Deleted $adapter send handler for $hostName" } catch [System.Management.Automation.RuntimeException] { if ($_.Exception.Message -eq "You cannot call a method on a null-valued expression.") { write-WarnMessage "$adapter send handler for $hostName does not exist" } elseif ($_.Exception.Message.IndexOf("Cannot delete a send handler that is used by") -ne -1) { write-WarnMessage "$adapter send handler for $hostName is in use. Cannot delete." } else { write-Error "$adapter send handler for $hostName could not be deleted: $_.Exception.ToString()" } } } function Delete-Bts-Instance( [string]$hostName, [string]$Server ) { try { # Unintall [System.Management.ManagementObject]$objHostInstance = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostInstance").CreateInstance() $name = "Microsoft BizTalk Server " + $hostName + " " + $Server $objHostInstance["Name"] = $name $objHostInstance.Uninstall() # Unmap [System.Management.ManagementObject]$objServerHost = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ServerHost").CreateInstance() $objServerHost["HostName"] = $hostName $objServerHost["ServerName"] = $Server $objServerHost.Unmap() write-SucessMessage "Deleted host instance for $hostName on $Server" } catch [System.Management.Automation.RuntimeException] { write-Error "$hostName host instance on server $Server could not be deleted: $_.Exception.ToString()" } } function Stop-Bts-HostInstance ( [string] $HostName, [string] $Server ) { try { $filter = "HostName = '" + $HostName + "' and RunningServer = '" + $Server + "'" $HostInstance = get-wmiobject "MSBTS_HostInstance" -namespace "root\MicrosoftBizTalkServer" -filter $filter $HostInstanceState = $HostInstance.GetState().State write-InfoMessage "Current state of $HostName instance on server $Server : $HostInstanceState (1=Stopped, 2=Start pending, 3=Stop pending, 4=Running, 8=Unknown)" if ($HostInstanceState -eq 4) { $HostInstance.Stop() $HostInstanceState = $HostInstance.GetState().State write-SucessMessage "New state of $HostName instance on server $Server: $HostInstanceState (1=Stopped, 2=Start pending, 3=Stop pending, 4=Running, 8=Unknown)" } else { $HostInstanceState = $HostInstance.GetState().State write-WarnMessage "Failed to stop host instance $HostName on server $Server because host instance state $HostInstanceState was not the expected value of 4 (running)" } } catch [System.Management.Automation.RuntimeException] { write-Error "$hostName host instance could not be stopped on $Server : $_.Exception.ToString()" } } function Delete-Bts-Host ( [string]$hostName ) { # TODO: This only works intermittently try { [System.Management.ManagementObject]$objHostSetting = get-wmiobject 'MSBTS_HostSetting' -namespace 'root\MicrosoftBizTalkServer' -filter "HostName='$hostName'" $objHostSetting.Delete() write-SucessMessage "Deleted host $hostName" } catch [System.Management.Automation.RuntimeException] { write-Error "$hostName host could not be deleted: $_.Exception.ToString()" } } # function to create BizTalk host function Create-Bts-Host( [string]$hostName, [int]$hostType, [string]$ntGroupName, [bool]$authTrusted, [bool]$tracking, [bool]$32BitOnly) { try { [System.Management.ManagementObject]$objHostSetting = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").CreateInstance() $objHostSetting["Name"] = $hostName $objHostSetting["HostType"] = $hostType $objHostSetting["NTGroupName"] = $NTGroupName $objHostSetting["AuthTrusted"] = $authTrusted $objHostSetting["IsHost32BitOnly"] = $32BitOnly $objHostSetting["HostTracking"] = $tracking $putOptions = new-Object System.Management.PutOptions $putOptions.Type = [System.Management.PutType]::CreateOnly; [Type[]] $targetTypes = New-Object System.Type[] 1 $targetTypes[0] = $putOptions.GetType() $sysMgmtAssemblyName = "System.Management" $sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName) $objHostSettingType = $sysMgmtAssembly.GetType("System.Management.ManagementObject") [Reflection.MethodInfo] $methodInfo = $objHostSettingType.GetMethod("Put", $targetTypes) $methodInfo.Invoke($objHostSetting, $putOptions) write-SucessMessage "Host $hostName created" } catch [System.Management.Automation.RuntimeException] { write-Error "$hostName host could not be created: $_.Exception.ToString()" } } function Update-Bts-Host ( [string]$hostName, [int]$hostType, [string]$ntGroupName, [bool]$authTrusted, [bool]$tracking, [bool]$32BitOnly) { try { [System.Management.ManagementObject]$objHostSetting = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostSetting").CreateInstance() $objHostSetting["Name"] = $hostName $objHostSetting["HostType"] = $hostType $objHostSetting["NTGroupName"] = $ntGroupName $objHostSetting["AuthTrusted"] = $authTrusted $objHostSetting["IsHost32BitOnly"] = $32BitOnly $objHostSetting["HostTracking"] = $tracking $putOptions = new-Object System.Management.PutOptions $putOptions.Type = [System.Management.PutType]::UpdateOnly; # This tells WMI it's an update. [Type[]] $targetTypes = New-Object System.Type[] 1 $targetTypes[0] = $putOptions.GetType() $sysMgmtAssemblyName = "System.Management" $sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName) $objHostSettingType = $sysMgmtAssembly.GetType("System.Management.ManagementObject") [Reflection.MethodInfo] $methodInfo = $objHostSettingType.GetMethod("Put", $targetTypes) $methodInfo.Invoke($objHostSetting, $putOptions) write-SucessMessage "Host updated" } catch [System.Management.Automation.RuntimeException] { write-Error "$hostName host could not be updated: $_.Exception.ToString()" } } # function to create BizTalk send adapter handler function Create-Bts-SendHandler([string]$adapter, [string]$hostName) { try { [System.Management.ManagementObject]$objSendHandler = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_SendHandler2").CreateInstance() $objSendHandler["AdapterName"] = $adapter $objSendHandler["HostName"] = $hostName $objSendHandler["IsDefault"] = $false $putOptions = new-Object System.Management.PutOptions $putOptions.Type = [System.Management.PutType]::CreateOnly; [Type[]] $targetTypes = New-Object System.Type[] 1 $targetTypes[0] = $putOptions.GetType() $sysMgmtAssemblyName = "System.Management" $sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName) $objSendHandlerType = $sysMgmtAssembly.GetType("System.Management.ManagementObject") [Reflection.MethodInfo] $methodInfo = $objSendHandlerType.GetMethod("Put", $targetTypes) $methodInfo.Invoke($objSendHandler, $putOptions) write-SucessMessage "Send handler created for $adapter / $hostName" } catch [System.Management.Automation.RuntimeException] { write-Error "Send handler for $adapter / $hostName could not be created: $_.Exception.ToString()" } } # function to create BizTalk receive adapter handler function Create-Bts-ReceiveHandler([string]$adapter, [string]$hostName) { try { [System.Management.ManagementObject]$objReceiveHandler = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ReceiveHandler").CreateInstance() $objReceiveHandler["AdapterName"] = $adapter $objReceiveHandler["HostName"] = $hostName $putOptions = new-Object System.Management.PutOptions $putOptions.Type = [System.Management.PutType]::CreateOnly; [Type[]] $targetTypes = New-Object System.Type[] 1 $targetTypes[0] = $putOptions.GetType() $sysMgmtAssemblyName = "System.Management" $sysMgmtAssembly = [System.Reflection.Assembly]::LoadWithPartialName($sysMgmtAssemblyName) $objReceiveHandlerType = $sysMgmtAssembly.GetType("System.Management.ManagementObject") [Reflection.MethodInfo] $methodInfo = $objReceiveHandlerType.GetMethod("Put", $targetTypes) $methodInfo.Invoke($objReceiveHandler, $putOptions) write-SucessMessage "Receive handler created for $adapter / $hostName" } catch [System.Management.Automation.RuntimeException] { write-Error "Receive handler for $adapter / $hostName could not be created: $_.Exception.ToString()" } } # function to create BizTalk host instance function Create-Bts-Instance([string]$hostName, [string]$login, [string]$password, [string]$Server) { try { [System.Management.ManagementObject]$objServerHost = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ServerHost").CreateInstance() $objServerHost["HostName"] = $hostName $objServerHost["ServerName"] = $Server $objServerHost.Map() [System.Management.ManagementObject]$objHostInstance = ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostInstance").CreateInstance() $name = "Microsoft BizTalk Server " + $hostName + " " + $Server $objHostInstance["Name"] = $name $objHostInstance.Install($Login, $Password, $True) write-SucessMessage "Created host instance for $hostName on $Server" } catch [System.Management.Automation.RuntimeException] { write-Error "$hostName host instance on server $Server could not be created: $_.Exception.ToString()" } } function Start-Bts-HostInstance ( [string] $HostName, [string] $Server ) { try { $filter = "HostName = '" + $HostName + "' and RunningServer = '" + $Server + "'" $HostInstance = get-wmiobject "MSBTS_HostInstance" -namespace "root\MicrosoftBizTalkServer" -filter $filter $HostInstanceState = $HostInstance.GetState().State write-InfoMessage "Current state of $HostName instance on server $Server: $HostInstanceState (1=Stopped, 2=Start pending, 3=Stop pending, 4=Running, 8=Unknown)" if ($HostInstanceState -eq 1) { $HostInstance.Start() $HostInstanceState = $HostInstance.GetState().State write-SucessMessage "New state of $HostName instance on server $Server: $HostInstanceState (1=Stopped, 2=Start pending, 3=Stop pending, 4=Running, 8=Unknown)" } else { $HostInstanceState = $HostInstance.GetState().State write-WarnMessage "Failed to start host instance $HostName on server $Server because host instance state $HostInstanceState was not the expected value of 1 (stopped)" } } catch [System.Management.Automation.RuntimeException] { write-Error "$hostName host instance could not be started on $Server : $_.Exception.ToString()" } } function Install-BTSMsi ( [string]$bts_application, [string]$msi_package, [string]$install_env ) { write-InfoMessage "Installing $msi_package in $bts_application for $install_env" BTSTask ImportApp /ApplicationName:$bts_application /Package:$msi_package /Overwrite /Environment:$install_env if ($LASTEXITCODE -ne 0) { write-ErrorMessage "Failed to Import MSI $msi_package" } else { write-SucessMessage "Installed $bts_application for $install_env" } } function Remove-BTSApplication ( [string]$appServer, [string]$appDatabase, [string]$appName ) { write-InfoMessage "Uninstalling Application: $appName " BTSTask RemoveApp /Server:"$appServer" /ApplicationName:"$appName" /Database:"$appDatabase" #| out-null if ($LASTEXITCODE -ne 0) { write-ErrorMessage "Failed to remove $appServer $appName" } else { write-SucessMessage "Removed $appName from $appServer" } } # Accesses SSO and will require the build user account to belong to the SSO Admins group. # Also requires Microsoft.BizTalk.ExplorerOM.dll to be loaded. function StartStop-BTSApplication ( [string]$appServer, [string]$appName, [string]$appCommand ) { if ( ($appName -eq '') -or ($appName -eq $null) ) { throw 'you must supply the application name' } #write-InfoMessage " Finding Application: $appServer:$appName " $exp = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer $exp.ConnectionString = Get-BTSConnectionString($appServer) $app = $exp.Applications[$appName] if($app -eq $null) { if ($appCommand -eq "stop" ) { write-WarnMessage "WARNING failed to stop $appName" } else { write-WarnMessage "FAILED to start $appName" } } else { switch -wildcard ( $app.Status.ToString() ) { 'Stopped' { if ($appCommand -eq "start" ) { write-InfoMessage "Starting Application: $appName " $null = $app.Start([Microsoft.BizTalk.ExplorerOM.ApplicationStartOption]::StartAll) $null = $exp.SaveChanges() write-SucessMessage " Started Application: $appName " } else { write-InfoMessage "Application Already Stopped: $appName " } } '*Started' { # includes Started and PartiallyStarted if ($appCommand -eq "stop" ) { write-InfoMessage "Stopping Application: $appName " $null = $app.Stop([Microsoft.BizTalk.ExplorerOM.ApplicationStopOption]::StopAll) $null = $exp.SaveChanges() write-SucessMessage " Stopped Application: $appName " } else { write-InfoMessage "Application Already Started : $appName " } } 'NotApplicable' { write-InfoMessage "Application doesn't require $appCommand" } default { $msg = "Unkown STATUS: " + $app.Status write-ErrorMessage $msg } } } } function Get-BTSConnectionString ( [string] $server ) { $group = Get-WmiObject MSBTS_GroupSetting -n root\MicrosoftBizTalkServer -computername $server $grpdb = $group.MgmtDBName $grpsvr = $group.MgmtDBServerName [System.String]::Concat("server=", $grpsvr, ";database=", $grpdb, ";Integrated Security=SSPI") write-InfoMessage " Server: $grpsvr - Database $grpdb" } |