Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert C# code to a PowerShell Script?

I regularly have to convert an existing C# code snippet/.CS file to a PowerShell script. How could I automate this process?

While I am aware that there are methods that can convert a .cs file to a cmdlet, I'm only interested in converting the C# code to a script or module.

like image 410
Tangiest Avatar asked Jan 26 '10 23:01

Tangiest


People also ask

How do you convert Celsius to Fahrenheit easy?

To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.

What is the formula to C?

In order to convert Fahrenheit to Celsius, we use the formula, °C = (°F - 32) × 5/9, in which the value of the temperature in Fahrenheit is placed and we get the value in Celsius. Fahrenheit and Celsius are the scales that are used to measure temperature.

How do you convert numbers to Celsius?

The formula for converting Fahrenheit to Celsius is C = 5/9(F-32). Fahrenheit and Celsius are the same at -40°. At ordinary temperatures, Fahrenheit is a larger number than Celsius. For example, body temperature is 98.6 °F or 37 °C.


2 Answers

I know you're looking for something that somehow converts C# directly to PowerShell, but I thought this is close enough to suggest it.

In PS v1 you can use a compiled .NET DLL:

PS> $client = new-object System.Net.Sockets.TcpClient PS> $client.Connect($address, $port) 

In PS v2 you can add C# code directly into PowerShell and use it without 'converting' using Add-Type (copied straight from MSDN )

C:\PS>$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);     } } "@  C:\PS> Add-Type -TypeDefinition $source  C:\PS> [BasicTest]::Add(4, 3)  C:\PS> $basicTestObject = New-Object BasicTest  C:\PS> $basicTestObject.Multiply(5, 2) 
like image 73
James Pogran Avatar answered Sep 19 '22 07:09

James Pogran


There is a Reflector add-in for PowerShell that will allow you to see the corresponding PowerShell script for static methods on classes

There's a good post with the example: http://blogs.msmvps.com/paulomorgado/2009/09/17/powershell-for-the-net-developer/.

like image 33
Tangiest Avatar answered Sep 20 '22 07:09

Tangiest