![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 Specs![]() |
| | #2 (permalink) |
| 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 Specs![]() |
| | #3 (permalink) |
| 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 Specs![]() |
| | #4 (permalink) |
| 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 Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Invoking a certain section of a powershell file. | greatbarrier86 | PowerShell | 20 | 11-08-2007 02:13 PM |
| Right-button on folder invoking PowerShell | Hans Dingemans | PowerShell | 10 | 10-02-2007 03:39 PM |
| Invoking PowerShell functions with parameters from .NET: issues. | Roman Kuzmin | PowerShell | 0 | 04-23-2007 07:35 AM |
| Invoking PowerShell from WScript via COM | Joris van Lier | PowerShell | 1 | 03-28-2007 09:53 AM |
| invoking .net classes in powershell | Jim B | PowerShell | 4 | 01-05-2007 06:38 PM |