Wednesday, June 29, 2005

Biztalk: Delete ReceiveLocation using WMI and VBScript

The default samples that are shipped with the Biztalk SDK do not contain code to delete a ReceiveLocation. Here is something I've written that will delete a ReceiveLocation, specified on the command line.

Please note that:
  • this is a very-very basic script (e.g. no checks on primary ReceiveLocation)

  • I know nothing about VBScript, so if you want parts of it nominated as a WTF, go ahead, make my day ;)

Option Explicit



Sub RemoveRecLoc()

    Dim objArgs: Set objArgs = WScript.Arguments


    'error handling is done by explicity checking the err object rather than using

    'the VB ON ERROR construct, so set to resume next on error.

    'on error resume next


    Dim strReceiveLocation   

    strReceiveLocation = objArgs(0)


    Dim InstSet, Inst

    set InstSet = GetObject ("winmgmts:\root\MicrosoftBizTalkServer").InstancesOf("MSBTS_ReceiveLocation")


    Dim objReceivePort


    'Check for error condition before continuing.

    If Err <> 0    Then

        PrintWMIErrorThenExit Err.Description, Err.Number

    End If


    'Report on number of receive locations found and list each one.

    wscript.echo "A Total of " & InstSet.Count & " Receive Locations were found."

    If InstSet.Count > 0 Then

        For Each Inst In InstSet

            If Inst.Name = strReceiveLocation Then

                wscript.echo "Receive Location Name  : " & Inst.Name


                wscript.echo "  Deleted ..."

            End If           


    End If


End Sub


'This subroutine deals with all errors using the WbemScripting object.  Error descriptions

'are returned to the user by printing to the console.

Sub    PrintWMIErrorThenExit(strErrDesc, ErrNum)

    On Error Resume    Next

    Dim    objWMIError    : Set objWMIError =    CreateObject("WbemScripting.SwbemLastError")


    If ( TypeName(objWMIError) = "Empty" ) Then

        wscript.echo strErrDesc & " (HRESULT: "    & Hex(ErrNum) & ")."


        wscript.echo objWMIError.Description & "(HRESULT: "    & Hex(ErrNum) & ")."

        Set objWMIError    = nothing

    End    If


    'bail out

    wscript.quit 0

End    Sub

No comments: