« Previous Next »

Thread: PowerShell in VB.NET Webservices

Last post 09-02-2009 1:20 AM by sergeia. 7 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (8 items)

Sort Posts:

  • 03-30-2009, 10:13 AM

    • MagnusVMI
    • Not Ranked
    • Joined on 03-30-2009, 2:07 PM
    • Sweden
    • Posts 3

    PowerShell in VB.NET Webservices

    Hallo,

     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

     

  • 03-30-2009, 12:29 PM In reply to

    • v-edsojo
    • Not Ranked
    • Joined on 09-22-2008, 2:29 PM
    • Venezuela
    • Posts 10

    Re: PowerShell in VB.NET Webservices

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

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

    - -
    Eduardo Sojo
    http://eng.eduardosojo.com
    Caracas - Venezuela
  • 03-30-2009, 3:57 PM In reply to

    • MagnusVMI
    • Not Ranked
    • Joined on 03-30-2009, 2:07 PM
    • Sweden
    • Posts 3

    Re: PowerShell in VB.NET Webservices

    Hi,

    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

    runspace.Open()

    ' create a pipeline and feed it the script text

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

    pipeline.Commands.AddScript(scriptText)

     

    ' 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.

    pipeline.Commands.Add("Out-String")

    ' execute the script

    ' Collection<psobject />

    results = pipeline.Invoke()

    ' close the runspace

    runspace.Close()

    ' convert the script result into a single string

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

    stringBuilder.AppendLine(obj.ToString())

    Next

    Return stringBuilder.ToString()

    End Function

     

     

    // Magnus

  • 03-30-2009, 6:10 PM In reply to

    • MagnusVMI
    • Not Ranked
    • Joined on 03-30-2009, 2:07 PM
    • Sweden
    • Posts 3

    Re: PowerShell in VB.NET Webservices

    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.

    Anyone?

    // Magnus

  • 04-05-2009, 5:17 AM In reply to

    Re: PowerShell in VB.NET Webservices

    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.

  • 09-01-2009, 6:07 PM In reply to

    • meSync
    • Not Ranked
    • Joined on 11-07-2006, 1:01 AM
    • Indiana
    • Posts 1

    Re: PowerShell in VB.NET Webservices

     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!

    Jacob R. Miller
    MeSync Technologies Inc.
    MeSync@gmail.com
    www.MeSync.com
  • 09-01-2009, 11:43 PM In reply to

    Re: PowerShell in VB.NET Webservices

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

  • 09-02-2009, 1:20 AM In reply to

    • sergeia
    • Top 100 Contributor
    • Joined on 04-14-2008, 8:47 PM
    • Posts 68

    Re: PowerShell in VB.NET Webservices

    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.

     

    Regards,

    Sergei

Page 1 of 1 (8 items)
Microsoft Communities