¿Cómo convertir código C # a un Script de PowerShell?
Regularmente tengo que convertir un fragmento de código C# existente/.Archivo CS a un script de PowerShell. ¿Cómo podría automatizar este proceso?
Aunque soy consciente de que hay métodos que pueden convertir a .cs a un cmdlet , solo estoy interesado en convertir el código C# a un script o módulo.
3 answers
Sé que estás buscando algo que de alguna manera convierta C# directamente a PowerShell, pero pensé que esto es lo suficientemente cercano como para sugerirlo.
En PS v1 puede usar una DLL. NET compilada:
PS> $client = new-object System.Net.Sockets.TcpClient
PS> $client.Connect($address, $port)
En PS v2 puede agregar código C # directamente a PowerShell y usarlo sin 'convertir' usando Add-Type (copiado directamente desde 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)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-05 18:00:22
Hay un reflector complemento para PowerShell que le permitirá ver el script de PowerShell correspondiente para los métodos estáticos en las clases
Hay un buen post con el ejemplo: http://blogs.msmvps.com/paulomorgado/2009/09/17/powershell-for-the-net-developer/.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-05 22:00:04
Adam Discroll ha creado un convertidor basado en Roslyn, e incluso proporciona un convertidor de código en línea-parece funcionar para scripts simples, pero tiene problemas con, por ejemplo, miembros o clases estáticas.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-13 15:09:12