What can I do with C# and Powershell? [closed]
Asked Answered
T

5

46

I have a decent understanding of C# 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#?

Tuna answered 12/4, 2009 at 19:21 Comment(0)
P
34

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/magazine/cc163293.aspx

Polka answered 12/4, 2009 at 19:33 Comment(1)
+1 Cmdlets can be really powerful, e.g. for providing a command line interface to managed applications and services.Precessional
D
64

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)
Descartes answered 12/4, 2009 at 19:48 Comment(3)
+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.Ananias
David - Yes there is. You can use the [-Language {CSharp | CSharpVersion3 | VisualBasic | JScript}] flagDescartes
Missing the "$" on first line source = @" $source = @"Turgid
P
34

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/magazine/cc163293.aspx

Polka answered 12/4, 2009 at 19:33 Comment(1)
+1 Cmdlets can be really powerful, e.g. for providing a command line interface to managed applications and services.Precessional
F
7

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

Flatulent answered 12/4, 2009 at 19:27 Comment(0)
H
5

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.

Hardy answered 22/5, 2009 at 10:51 Comment(0)
A
3

Scott Hanselman aka Hanselminutes has several podcasts about Powershell, CmdLets, C# and more. It's 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.

List of PS related podcasts on his site (in reverse chronological order):

#190: State of Powershell/Lee Holmes & Jason Shirk
#162: Powershell 2.0
#49: Powershell/Bruce Payette
#36: Jeffrey Snover, Powershell architect
#24: Windows Powershell (MONAD), Part II

Accuse answered 22/5, 2009 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.