views:

5442

answers:

6

I have a very good understanding of C# (I'm probably an intermeddiate programmer) and a very basic understanding of powershell. I'm using Windows PowerShell CTP 3, which has been really fun. But I want to go beyond writing scripts/functions. Is there any cool stuff to do with C#?

+1  A: 

Answer is 'It depends'. You can do a variety of stuff with c# (build windows, web clients, and mobile clients).

You can invoke powershell scripts from C#. Look at this site ==> link

You can even convert your c# code to powershell ==> link

CodeToGlory
+13  A: 

I think the most interesting thing you can do with C# and PowerShell is to build CmdLet's. These are essentially plugins to PowerShell that are written in managed code and act like normal functions. They have a verb-noun pair and many of the functions you already use are actually cmdlets under the hood.

http://msdn.microsoft.com/en-us/library/ms714598(VS.85).aspx

JaredPar
Very nice link, Thanks!
Lucas McCoy
+1 Cmdlets can be really powerful, e.g. for providing a command line interface to managed applications and services.
Tormod Fjeldskår
+1 indeed - cmdlets are cool and very powerful!
marc_s
+17  A: 

At the highest level you have two different options You can from a C# program host PowerShell and execute PowerShell commands via RunSpaces and pipelines.

Or you can from within PowerShell run C# code. This can be done two ways. With a PowerShell snapin, a compiled dll which provides PowerShell cmdlets and navigation providers, or via the new cmdlet Add-Type, which lets you dynamically import C#, VB, F# code. From the help

source = @"
public class BasicTest
{
    public static int Add(int a, int b)
    {
        return (a + b);
    }

    public int Multiply(int a, int b)
    {
        return (a * b);
    }
}
"@

Add-Type -TypeDefinition $source
[BasicTest]::Add(4, 3)
$basicTestObject = New-Object BasicTest 
$basicTestObject.Multiply(5, 2)
Scott Weinstein
+1 for hosting PowerShell as a scripting engine for your application. Built in .NET scripting with a language that is rapidly becoming a staple tool.
Steven Murawski
Is there any way to plug in VB codes?
David.Chu.ca
David - Yes there is. You can use the [-Language {CSharp | CSharpVersion3 | VisualBasic | JScript}] flag
Scott Weinstein
+1  A: 

You can look at it one of two ways: 1. How can you leverage PowerShell inside your C# program 2. How can you leverage C# programming inside PowerShell.

To some degree, they are quite different questions with different answers.

From C# you can leverage the PowerShell engine, runspaces, pipe-lines, etc. As is done with Exchante, you can use C# to do all the GUI stuff, then invoke a PowerShell cmdlet to do all the hard stuff. This option is appropriate if you can find PowerShell cmdlets or scripts to leverage.

From PowerShell, you use C# to expand what you can do in PowerShell. You can create cmdlts and providers to enable others to access application data. Or you can just create objects that can be used within a PowerShell script. This option is the way you help to open up your application to be managed in a more automated way.

So depending on what you are looking to do, you have options.

Thomas ([email protected])

Thomas Lee
+1  A: 

Scott Hanselman aka Hanselminutes link text has a podcast about Powershell, CmdLets, C# and more. Its the best if you want to learn what it is, how it works and more. Do a search on his website to grab the podcast.

By shear luck the latest podcast is about Powershell 2.0 - im pretty sure he has another about PS1.

mP
A: 

A fun-kind but useful thing you can do is to convert your C# code into DLL using powershell and csc command line complier. An example here: http://www.ankitk.com/techpatch/?p=107

ankitwww