On Oct 2, 11:34 am, Oisin Grehan <ois...@xxxxxx> wrote:
Quote:
> On Oct 2, 9:16 am, NJC <N...@xxxxxx> wrote:
>
>
>
>
>> Quote:
> > I am trying to create a new website on IIS 6.0. I have successfully
> > completed this task using VBScript, but I'm struggling converting the code
> > into Powershell, because I am newbie to WMI and fairly new to Powershell. My
> > VBScript code is as follows:
> Quote:
> > set locatorObj = CreateObject("Wbemscripting.SWbemLocator")
> > set providerObj = locatorObj.ConnectServer("localhost",
> > "root/MicrosoftIISv2")
> > set serviceObj = providerObj.Get("IIsWebService='W3SVC'")
> Quote:
> > Bindings = Array(0)
> > Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_()
> > Bindings(0).IP = ""
> > Bindings(0).Port = "8383"
> > Bindings(0).Hostname = ""
> Quote:
> > Dim strSiteObjPath
> > strSiteObjPath = serviceObj.CreateNewSite("MyNewSite", Bindings,
> > "C:\Inetpub\Wwwroot")
> Quote:
> > I have tried to convert the variable declaration and object instantiation to
> > the following:
> > $locatorObj = new-object -com WbemScripting.SWbemLocator
> > $providerObj = $locatorObj.ConnectServer("localhost","root/MicrosoftIISv2")
> > $serviceObj = $providerObj.Get("IIsWebService='W3SVC'")
> Quote:
> > This conversion does not provide a build error but when I try to invoke the
> > CreateNewSite() on the $serviceObj variable I get an error explaining that
> > $serviceObj does not contain a function or method CreateNewSite(). This is
> > strange because I reckon it should, considering WMI is supported by
> > Powershell.
> Quote:
> > So I'm guessing there is a problem passing "root/MicrosoftIIsv2" to
> > $locatorObj.ConnectServer but I'm not entirely sure what to pass as an
> > alternative.
> Quote:
> > If anyone can explain where I'm going wrong, or provide some direction or
> > even convert the VBScript code snippet for me
I'd be very much
> > appreciative. > Quote:
> > Thanks in advance,
>
> Hi NJC
>
> While I don't profess to be a wbem/wmi ninja, I can help you here - an
> explanation of the techniques is worth a blog entry in itself (I've
> started writing it now, so I'll post back to this thread when it's
> done) so for the moment, I've written how this is performed using what
> MS refer to as "indirect method calls." Some languages, like vbscript
> allow "direct" calls like your example above. It doesn't appear [to
> me] that powershell has a fully featured adapter for this kind of
> stuff yet, but the powershell team has done an otherwise excellent job
> in enabling COM interop as it is - anyway, try this out:
> Quote:
> > $locatorObj = new-object -com WbemScripting.SWbemLocator
> > $providerObj = $locatorObj.ConnectServer("localhost","root/MicrosoftIISv2")
> > $serviceObj = $providerObj.Get("IisWebService='W3SVC'")
> > $bindings = @($providerObj.Get("ServerBinding").SpawnInstance_())
>
> # get reference to method
> Quote:
> > $createNewSiteMethod = $serviceObj.Methods_.Item("CreateNewSite")
>
> # define "in" parameters
> Quote:
> > $inParameters = $createNewSiteMethod.InParameters.SpawnInstance_()
> > $inParameters.Properties_.Item("PathOfRootVirtualDir").value = "c:\inetpub\newroot"
> > $inParameters.Properties_.Item("ServerBindings").value = $bindings
> > $inParameters.Properties_.Item("ServerComment").value = "My new virtual server"
>
> # execute method
> Quote:
> > $outParameters = $serviceObj.ExecMethod_("CreateNewSite", $inParameters)
>
> # display result
> Quote:
> > $outParameters
>
> Qualifiers_ : {}
> Properties_ : {ReturnValue}
> Methods_ : {}
> Derivation_ : {}
> Path_ : System.__ComObject
> Security_ : System.__ComObject
> SystemProperties_ : {__PATH, __NAMESPACE, __SERVER, __DERIVATION...}
>
> This was tested on Windows 2003 SP1. This will NOT work on Windows XP.
>
> For more information on indirect method usage, see:http://msdn2.microsoft.com/en-us/library/aa384833.aspx
>
> Hope this helps,
>
> - Oisin- Hide quoted text -
>
> - Show quoted text - Well, that must be the shortest life for a post ever. I've just read
Marco's link (posted while I was preparing the last post) and I see
that you _can_ call the methods directly via PSBase. LOL - like I
said, I ain't no WMI ninja anyway...
Anyway, hopefully it demonstrates something useful!
- Oisin