PowerShell in VB.NET Webservices RRS feed

  • Question

  • User-1088707801 posted


     I dont know if this is the right place for this question, but perhaps somebody can direct me right.

    My plan is to write an solution for our administrative hosting system, to add (and remove) webbsites on a Windows 2008 Server.

    I have decied to use PowerShell and are running this from a VB.NET Webservices.
    The problem is when I run

    Get-ChildItem IIS:\Sites|format-list name,id

    I only see 2 of 6 webbsites (and that is the webservices itself and others in the same AppPool). I guess that I have to set som permissions so it could "see" from other AppPool, or are Im totally wrong here?

    Please advise :)

     // Magnus


    Monday, March 30, 2009 10:13 AM

All replies

  • User-1349367722 posted

    Hi, did you try your code directly on Powershell console?

    IIS Extension for powershell always list complete content based on your query.

    Monday, March 30, 2009 12:29 PM
  • User-1088707801 posted


    Yes. If I run the code directly on the console it works.
    My Code: Get-ChildItem IIS:\Sites|format-list name,id

    I have also tried with whoami to see that I execute as administrator, witch I am.

    And my vb.net code to execute it:

    Public Function RunScript(ByVal scriptText As String) As String

    Dim results As Object

    'create Powershell runspace

    Dim runspace As Runspace = RunspaceFactory.CreateRunspace()

    ' open it


    ' create a pipeline and feed it the script text

    Dim pipeline As Pipeline = runspace.CreatePipeline()pipeline.Commands.AddScript("Add-PSSnapin WebAdministration")



    ' add an extra command to transform the script

    ' output objects into nicely formatted strings

    ' remove this line to get the actual objects

    ' that the script returns. For example, the script

    ' "Get-Process" returns a collection

    ' of System.Diagnostics.Process instances.


    ' execute the script

    ' Collection<psobject />

    results = pipeline.Invoke()

    ' close the runspace


    ' convert the script result into a single string

    Dim stringBuilder As New StringBuilder()For Each obj As PSObject In results



    Return stringBuilder.ToString()

    End Function



    // Magnus

    Monday, March 30, 2009 3:57 PM
  • User-1088707801 posted

    Hi again,

     I have also tried and make a windows application with the same code, and then it works great. It seams just not work when running as webbapplication.


    // Magnus

    Monday, March 30, 2009 6:10 PM
  • User-267369003 posted

    I think you have encountered a bug in the IIS Powershell provider.  This is just a hunch so someone from the team should respond.

    When hosted in an IIS worker process (W3WP.EXE), any consumers of IIS configuration are redirected to an application-pool specific configuraiton file.  This config file contains all the config pertinent to the given application pool.  It is a security feature (worker processes shouldn't be able to read each other's config).  So if you are instantiating the powershell provider in-process, it is using this simpler config file. 

    This is a bug.  The powershell provider should use a new instance of the configuration system and go directly against system32\inetsrv\applicationhost.config.  Then it will work (though of course your app will have to run as a user who has access to applicationhost.config -> namely an admin). 

    As a work around, you can have your application launch a process which has the powershell instance doing your work. 

    Hope that helps.

    Sunday, April 5, 2009 5:17 AM
  • User1909752694 posted

     Have you found any answer to this? 

     I am having the same problem.  I have an script in and asp.net webservice that invokes powershell to run an exe (it converts files from docx to pdf).    If I copy and paste the script into powershell it works perfect. If I run it from the web service it doesn't seem to run.

     I gave the web application Administrator rights (just for testing) and it still doesn't run the script.  Any help would be greatly appreciated!

    Tuesday, September 1, 2009 6:07 PM
  • User-267369003 posted

    I think this is an issue for the IIS Powershell Snapin product team.  They follow this forum and someone should respond.

    Tuesday, September 1, 2009 11:43 PM
  • User-1210653391 posted

    I don't see these reported issues as a bug, snapin was designed this way and this is an expected behavior. The fact that you all are getting into that with PowerShell is just a specific case, you will see the same behavior with most of other APIs to configuration system, when you run them from worker process. To fix this we either should redesign how snapin works, or you could host PowerShell and IIS snapin in other process, that doesn't have limitations of w3wp.exe and then communicate with this instance from worker process. In my opinion, easiest way to do this is to use remoting feature of PowerShell v2.0. With this you will host PowerShell in the worker process only to create and open remote runspace to another instance of PowerShell that will be running under wsman service.

    I will look for some workarounds.




    Wednesday, September 2, 2009 1:20 AM