Windows Vista Forums

Invoking Powershell from C#
  1. #1


    Janssen Guest

    Invoking Powershell from C#

    I've been collaborating with a C#/ASP.NET developer in writing a web
    application that invokes Powershell functions which connect to AD, WMI, etc.,
    and we've been running into some problems. The developer has no Powershell
    experience, and I have no C# experience, so I just write the Powershell code
    as parameterized functions and then pass the whole .ps1 for him to invoke
    from a webpage.

    First, let me say that there are a few examples of invoking Powershell from
    C# on the web, such as
    http://blogs.msdn.com/powershell/arc...25/583250.aspx
    and we've been able to get simple things like get-process to work, but when
    calling more complex scripts, it always has trouble.
    For example, any time I ever set any variable to null, (e.g. $myvar = $null
    or $myvar = ""), it works fine from a Powershell command prompt, but crashes
    when invoking from C#. Is there any kind of documentation anywhere for
    concerns, best practices, gotchas etc. when trying to invoke Powershell from
    another .Net language?



    Thanks in advance for any help,
    Janssen

      My System SpecsSystem Spec

  2. #2


    Bruce Payette [MSFT] Guest

    Re: Invoking Powershell from C#

    Have you taken a look at the samples in the SDK? For example, take a look
    at:

    http://msdn2.microsoft.com/en-us/library/ms714593.aspx

    (Note - there is some weird MSDN bug that causes tags to show up in the code
    some time.)

    There's even a moderately complete example of writing an interactive host
    for PowerShell in C# at:

    http://msdn2.microsoft.com/en-us/library/ms714612.aspx

    This example does a lot more stuff than you'll need in most applications but
    it's a place to start if you want to play with building your own PowerShell
    host.

    -bruce

    --
    Bruce Payette [MSFT]
    Windows PowerShell Technical Lead
    Microsoft Corporation
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Visit the Windows PowerShell Team blog at:
    http://blogs.msdn.com/PowerShell
    Visit the Windows PowerShell ScriptCenter at:
    http://www.microsoft.com/technet/scr.../hubs/msh.mspx
    My Book: http://manning.com/powershell

    "Janssen" <Janssen@discussions.microsoft.com> wrote in message
    news:A7F157BA-0C08-4AF9-BF15-28A9992741F4@microsoft.com...
    > I've been collaborating with a C#/ASP.NET developer in writing a web
    > application that invokes Powershell functions which connect to AD, WMI,
    > etc.,
    > and we've been running into some problems. The developer has no Powershell
    > experience, and I have no C# experience, so I just write the Powershell
    > code
    > as parameterized functions and then pass the whole .ps1 for him to invoke
    > from a webpage.
    >
    > First, let me say that there are a few examples of invoking Powershell
    > from
    > C# on the web, such as
    > http://blogs.msdn.com/powershell/arc...25/583250.aspx
    > and we've been able to get simple things like get-process to work, but
    > when
    > calling more complex scripts, it always has trouble.
    > For example, any time I ever set any variable to null, (e.g. $myvar =
    > $null
    > or $myvar = ""), it works fine from a Powershell command prompt, but
    > crashes
    > when invoking from C#. Is there any kind of documentation anywhere for
    > concerns, best practices, gotchas etc. when trying to invoke Powershell
    > from
    > another .Net language?
    >
    > Thanks in advance for any help,
    > Janssen




      My System SpecsSystem Spec

  3. #3


    Janssen Guest

    Re: Invoking Powershell from C#

    Bruce,

    Thanks for the reply! I've passed your suggestions on to the C# developer.
    As I'm only writing the Powershell portion, I'm not sure how much of this
    code he has implemented. Here's an example though of some working code that
    I can run from the command line, but which errors out when he dumps into a
    big string and invokes it. This script is MUCH longer, but i cut it down to
    this little snippet just to see if we could get that to work. The overall
    task of this script is to be implemented on an end-user self-service website,
    allowing the user to create their own team folders on our file server, create
    corresponding AD groups, setting ACLS on the directories, etc. If we can just
    launch it via his ASP.net page, we'll about be finished, but we seem to get
    stuck once he tries to launch it.

    code:
    function file-top-level ([string]$dept)
    {
    function create-ad-group ([string]$mygroup)
    {
    $objOU = [adsi]"LDAP://OU=Security Groups-File
    Server,OU=Groups,OU=DSA,OU=BL-DSA,OU=BL,DC=ADS,DC=IU,DC=EDU"
    $objGroup = $objOU.Create("group", "cn=$mygroup")
    $objGroup.Put("sAMAccountName", "$mygroup")
    $objGroup.SetInfo()
    }
    create-ad-group BL-DSA-FILE-$dept-LVL1
    }
    file-top-level -add Joshtest54321
    Webpage result:
    Command: function file-top-level ([string]$dept) { function create-ad-group
    ([string]$mygroup) { $objOU = [adsi]"LDAP://OU=Security Groups-File
    Server,OU=Groups,OU=DSA,OU=BL-DSA,OU=BL,DC=ADS,DC=IU,DC=EDU" $objGroup =
    $objOU.Create("group", "cn=$mygroup") $objGroup.Put("sAMAccountName",
    "$mygroup") $objGroup.SetInfo() } create-ad-group BL-DSA-FILE-$dept-LVL1 }
    file-top-level -add Joshtest54321 Error: Object reference not set to an
    instance of an object.


      My System SpecsSystem Spec

  4. #4


    Janssen Guest

    Re: Invoking Powershell from C#

    One addendum. The -add shouldn't be on the last line in this example. The
    function generally has two sections - add and remove - which are determined
    by two switches, $add or $remove, and an if statement. I took all of that
    out to see if we could just get this nested function to work.

    "Janssen" wrote:

    > Bruce,
    >
    > Thanks for the reply! I've passed your suggestions on to the C# developer.
    > As I'm only writing the Powershell portion, I'm not sure how much of this
    > code he has implemented. Here's an example though of some working code that
    > I can run from the command line, but which errors out when he dumps into a
    > big string and invokes it. This script is MUCH longer, but i cut it down to
    > this little snippet just to see if we could get that to work. The overall
    > task of this script is to be implemented on an end-user self-service website,
    > allowing the user to create their own team folders on our file server, create
    > corresponding AD groups, setting ACLS on the directories, etc. If we can just
    > launch it via his ASP.net page, we'll about be finished, but we seem to get
    > stuck once he tries to launch it.
    >
    > code:
    > function file-top-level ([string]$dept)
    > {
    > function create-ad-group ([string]$mygroup)
    > {
    > $objOU = [adsi]"LDAP://OU=Security Groups-File
    > Server,OU=Groups,OU=DSA,OU=BL-DSA,OU=BL,DC=ADS,DC=IU,DC=EDU"
    > $objGroup = $objOU.Create("group", "cn=$mygroup")
    > $objGroup.Put("sAMAccountName", "$mygroup")
    > $objGroup.SetInfo()
    > }
    > create-ad-group BL-DSA-FILE-$dept-LVL1
    > }
    > file-top-level -add Joshtest54321
    > Webpage result:
    > Command: function file-top-level ([string]$dept) { function create-ad-group
    > ([string]$mygroup) { $objOU = [adsi]"LDAP://OU=Security Groups-File
    > Server,OU=Groups,OU=DSA,OU=BL-DSA,OU=BL,DC=ADS,DC=IU,DC=EDU" $objGroup =
    > $objOU.Create("group", "cn=$mygroup") $objGroup.Put("sAMAccountName",
    > "$mygroup") $objGroup.SetInfo() } create-ad-group BL-DSA-FILE-$dept-LVL1 }
    > file-top-level -add Joshtest54321 Error: Object reference not set to an
    > instance of an object.
    >


      My System SpecsSystem Spec

Invoking Powershell from C# problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking a certain section of a powershell file. greatbarrier86 PowerShell 20 08 Nov 2007
Right-button on folder invoking PowerShell Hans Dingemans PowerShell 10 02 Oct 2007
Invoking PowerShell functions with parameters from .NET: issues. Roman Kuzmin PowerShell 0 23 Apr 2007
Invoking PowerShell from WScript via COM Joris van Lier PowerShell 1 28 Mar 2007
invoking .net classes in powershell Jim B PowerShell 4 05 Jan 2007