Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Invoking Powershell from C#

Reply
 
Old 02-15-2007   #1 (permalink)
Janssen


 
 

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
Old 02-16-2007   #2 (permalink)
Bruce Payette [MSFT]


 
 

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
Old 02-16-2007   #3 (permalink)
Janssen


 
 

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
Old 02-16-2007   #4 (permalink)
Janssen


 
 

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
Reply

Thread Tools


Similar Threads
Thread Forum
Invoking a certain section of a powershell file. PowerShell
Right-button on folder invoking PowerShell PowerShell
Invoking PowerShell functions with parameters from .NET: issues. PowerShell
Invoking PowerShell from WScript via COM PowerShell
invoking .net classes in powershell PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46