Posts

Showing posts with the label Runspace

C#13 params collection performance benchmark

Image
 Microsoft released the latest version of C# i.e. C#13 and now it allows params to be any of the collections supported by collection expressions rather than just arrays. So, I quickly wrote the benchmark code to test the performance and overall, it did improve the performance. public class ParamsBenchmark{ // Dummy methods to illustrate the params collections public int SumOld(params int[] numbers) { var sum = 0; foreach (var num in numbers) { sum += num; } return sum; } public int SumNew(params Span<int> numbers) { int sum = 0; foreach (var num in numbers) { sum += num; } return sum; } [Benchmark] public int SumOldParams() { return SumOld(1, 2, 3, 4, 5); } [Benchmark] public int SumNewParams() { return SumNew([1, 2, 3, 4, 5]); } } Below are my benchmark results:

Running Powershell Command Remotely Using C#

Image
C# has some really awesome nuget package if you want to run the PowerShell script or run the command. I am talking about the nuget package "Microsoft.PowerShell.SDK". Now running the batch file or any command or any executable from the local machine is quite easy and simple. You just need to create the PowerShell object, add the script you want to execute and invoke it. Above code is simple and self explanatory. But lately in my project we had a requirement in which we need to execute the command on the remote machine and fetch the output. It's as good as you are remoting to that machine and executing the command on that machine and fetching the output. Now you would be surprised to know that, you can actually achieve that using the PowerShell SDK and with only few lines of code you can create the magic. PowerShell SDK provides something called as "RunSpace". When you create the RunSpace object you need to provide the machine details and corresponding credential...