I donβt know if this is exactly what you need, but you can enable .NET 4 support in powershell. Remember that it uses .NET 2 by default. To do this, you need to add powershell.exe.config and this content in $ PSHome XML with this name:
<?xml version="1.0"?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0.30319"/> <supportedRuntime version="v2.0.50727"/> </startup> </configuration>
After that, any code oriented to a large .net version will work, for example:
Slytherin>> gc new.cs using System.IO; public static class testeo { public static string joinP(string[] arr) { return Path.Combine(arr); } } Slytherin>> $arr = "uno", "dos", "tres", "cuatro", "cinco" Slytherin>> Add-Type -Path .\new.cs Slytherin>> [testeo]::joinP($arr) uno\dos\tres\cuatro\cinco
This method uses Path.Combine with N arguments that are defined in .NET4. In .NET2, Combine can handle up to two arguments. I am testing another example that uses anonymous delegates, one of the characteristics of C # 3:
Slytherin>> $sharp = [IO.file]::readalltext((resolve-path new.cs)) Slytherin>> add-type -typedef $sharp -Language CsharpVersion3 Slytherin>> gc .\new.cs using System.IO; using System; using System.Linq; namespace grantest { public static class testeo { public static void joinP(string[] arr) { arr.ToList<string>().ForEach(x=>Console.WriteLine(x)); } } } Slytherin>> [grantest.testeo]::joinP($arr) uno dos tres cuatro cinco Slytherin>>
Last example, if you try the following code, it will not work
Slytherin>> $sharp = [IO.file]::readalltext((resolve-path new.cs)) Slytherin>> $sharp using System.IO; using System; using System.Linq; namespace grantest { public static class testeo4 { public static string joinP(string[] arr) { arr.ToList<string>().ForEach(x=>Console.WriteLine(x)); return Path.Combine(arr); } } } Slytherin>> Add-Type -TypeDefinition $sharp Add-Type : c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(3) : The type or namesp ace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(2) : using System; c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(3) : >>> using System.Linq; c:\Users\voodoomsr\AppData\Local\Temp\5ikkmpfn.0.cs(4) : At line:1 char:9 + Add-Type <<<< -TypeDefinition $sharp + CategoryInfo : InvalidData: (c:\Users\voodoo...bly reference?):Compile rError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddType Command
try to specify the language parameter
Slytherin>> add-type -TypeDefinition $sharp -Language CSharpVersion3 Add-Type : c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(13) : No overload for m ethod 'Combine' takes '1' arguments c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(12) : arr.ToList<string>() .ForEach(x=>Console.WriteLine(x)); c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(13) : >>> return Path.Comb ine(arr); c:\Users\voodoomsr\AppData\Local\Temp\v3r5whoc.0.cs(14) : } At line:1 char:9 + add-type <<<< -TypeDefinition $sharp -Language CSharpVersion3 + CategoryInfo : InvalidData: (c:\Users\voodoo...s '1' arguments:Compile rError) [Add-Type], Exception + FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddType Command
This is because if you use the .net4 method and, apparently, if you say that the version 3 language automatically uses the .net 3 libraries. To do this, you need to add System.Core as a reference assembly and forget about language parameter (Csharpversion4 does not have an enumeration of possible values), so it will use version 4 because we included it earlier.
Slytherin>> Add-Type -TypeDefinition $sharp -ReferencedAssemblies System.core Slytherin>> [grantest.testeo4]::joinP($arr) uno dos tres cuatro cinco uno\dos\tres\cuatro\cinco
Good luck and happy coding.