![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | WMI in IIS 6.0 Hello, 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: set locatorObj = CreateObject("Wbemscripting.SWbemLocator") set providerObj = locatorObj.ConnectServer("localhost", "root/MicrosoftIISv2") set serviceObj = providerObj.Get("IIsWebService='W3SVC'") Bindings = Array(0) Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_() Bindings(0).IP = "" Bindings(0).Port = "8383" Bindings(0).Hostname = "" Dim strSiteObjPath strSiteObjPath = serviceObj.CreateNewSite("MyNewSite", Bindings, "C:\Inetpub\Wwwroot") 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'") 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. 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. 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 muchappreciative. Thanks in advance, |
My System Specs![]() |
| | #2 (permalink) |
| | Re: WMI in IIS 6.0 NJC wrote: Quote: > Hello, > > 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: Problems invoking IIS WMI method CreateNewSite Marco -- ---------------- PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
| | #3 (permalink) |
| | Re: WMI in IIS 6.0 Hey Marco, cheers for info. "Marco Shaw" wrote: Quote: > NJC wrote: Quote: > > Hello, > > > > 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: > Seems to apply: > Problems invoking IIS WMI method CreateNewSite > > Marco > > -- > ---------------- > PowerGadgets MVP > http://www.powergadgets.com/mvp > > Blog: > http://marcoshaw.blogspot.com > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: WMI in IIS 6.0 On Oct 2, 9:16 am, NJC <N...@xxxxxx> wrote: Quote: > Hello, > > 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: > > set locatorObj = CreateObject("Wbemscripting.SWbemLocator") > set providerObj = locatorObj.ConnectServer("localhost", > "root/MicrosoftIISv2") > set serviceObj = providerObj.Get("IIsWebService='W3SVC'") > > Bindings = Array(0) > Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_() > Bindings(0).IP = "" > Bindings(0).Port = "8383" > Bindings(0).Hostname = "" > > Dim strSiteObjPath > strSiteObjPath = serviceObj.CreateNewSite("MyNewSite", Bindings, > "C:\Inetpub\Wwwroot") > > 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'") > > 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. > > 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. > > 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. > > Thanks in advance, 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_()) Quote: > $createNewSiteMethod = $serviceObj.Methods_.Item("CreateNewSite") 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" Quote: > $outParameters = $serviceObj.ExecMethod_("CreateNewSite", $inParameters) Quote: > $outParameters 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 |
My System Specs![]() |
| | #5 (permalink) |
| | Re: WMI in IIS 6.0 On Oct 2, 11:34 am, Oisin Grehan <ois...@xxxxxx> wrote: Quote: > On Oct 2, 9:16 am, NJC <N...@xxxxxx> wrote: > > > > > Quote: > > Hello, 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 - 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 |
My System Specs![]() |
| | #6 (permalink) |
| | Re: WMI in IIS 6.0 On Oct 2, 10:25 am, Marco Shaw <marco.shaw@_NO_SPAM_gmail.com> wrote: Quote: > NJC wrote: Quote: > > Hello, 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: > Seems to apply:http://www.vistax64.com/powershell/3...ing-iis-wmi-me... > > Marco > > -- > ---------------- > PowerGadgets MVPhttp://www.powergadgets.com/mvp > > Blog:http://marcoshaw.blogspot.com already and don't feel like changing the whole thing, you can quickly get a powershell "adapted" version using the [wmi] accelerator against the path, e.g. Quote: > ... > $serviceObj = $providerObj.Get("IIsWebService='W3SVC'") Quote: > $so = [wmi]$serviceObj.Path_.path > $so | get-member Name MemberType Definiti ---- ---------- -------- AddDependency Method System.M... AddExtensionFile Method System.M... Change Method System.M... ChangeStartMode Method System.M... Create Method System.M... CreateNewSite Method System.M... DeleteExtensionFileRecord Method System.M... As you can see, the methods are now available in "direct" style (without needing psbase it appears)? - Oisin |
My System Specs![]() |
| | #7 (permalink) |
| | Re: WMI in IIS 6.0 Hey Oisin, Yeah Marco's link was pretty useful and helped solve my problem. Thanks for your input though, never have too much info especially when i'm new to WMI and trying to fudge my way through it ![]() Cheers, "Oisin Grehan" wrote: Quote: > On Oct 2, 11:34 am, Oisin Grehan <ois...@xxxxxx> wrote: Quote: > > On Oct 2, 9:16 am, NJC <N...@xxxxxx> wrote: > > > > > > > > > > Quote: > > > Hello, 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 > > |
My System Specs![]() |