Is there a guide to the (somewhat) convoluted PowerShell syntax? An example with Biztalk

I probably shouldn't be asking a general question with a specific example, but I'm having a hard time translating some basic commands from the PowerShell console into reusable functions and custom cmdlets. Is there a definitive PowerShell syntax guide somewhere, with gotchas, hints, and hints?

For example, I am trying to create a function to automate the administration of BizTalk Host instances. The following function does not work (does not execute at runtime), whereas every single line works and executes as expected when individually pasted in the PowerShell console.

function AddNewHostInstance([string]$ServerName, [string]$HostName, [string]$Login, [string]$Password)
{
    [System.Management.ManagementObject]$objServerHost = `
        ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_ServerHost").CreateInstance()

    $objServerHost["ServerName"] = $ServerName
    $objServerHost["HostName"] = $HostName
    $objServerHost.Map()

    $name = "Microsoft BizTalk Server " + $HostName + " " + $ServerName

    [System.Management.ManagementObject]$objServerHost = `
        ([WmiClass]"root/MicrosoftBizTalkServer:MSBTS_HostInstance").CreateInstance()

    $objHostInstance["Name"] = $name
    $objHostInstance.Install($Login, $Password, $True)
}

      

By the way, in this particular case I am getting the error:

PS C:\Users\username> createHostInstances $server, $host, $user, $pwd
Exception calling "Map" : "Invalid parameter "
At line:14 char:39
+     $objServerHost.Map <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException
Exception calling "Install" : "Instance of the WMI class is not found.
No instance was found with the specified key.  This could be the result of the instance being deleted by another BizTalk Admin session."
At line:19 char:29
+     $objHostInstance.Install <<<< ($Login, $Password, $True)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException
PS C:\Users\username>

      

[Edit] Upon further investigation, it seems that the function doesn't like to assign properties to a WMI object via a variable. If I hardcode all the values โ€‹โ€‹(instead of relying on the supplied function parameters) then it works as expected!

Basically, this works:

# Using hard-coded value
$objServerHost["HostName"] = "TestHost"

      

If it doesn't:

# Using function supplied parameter
$objServerHost["HostName"] = $HostName

      

However, I don't understand why ...

+2


source to share


2 answers


In terms of manual, the best book out there is Windows PowerShell in Action by Bruce Payette. There is a second edition in February, but you can get early access to the electronic project. There are also some free books out there. Mastering PowerShell from Dr. Tobias Weltner and I also have a short & 60 page eBook - Effective Windows PowerShell . This last one covers a few bugs and also provides you with a mental model for PowerShell to work.

WRT error, I wonder if you had better luck with built-in PowerShell support for WMI like:

$namespace = 'root/MicrosoftBizTalkServer' 
$host = Get-WmiObject -namespace $namespace -class MSBTS_HostInstance

      



See if the corresponding WMI object has the appropriate data and methods (Map and Install):

$host | fl *
$host | Get-Member

      

+6


source


As for the Map () error, sometimes with WMI you need to cast back and execute $ objServerHost.psbase.Invoke ("Map") instead. In addition, I have a few sample PowerShell scripts for BizTalk administration that you might find useful as a guide.



0


source







All Articles