Summary
This is a standard problem space that Microsoft, for whatever reason, has chosen not to solve. The two problems are 1) you can’t run remote commands on a windows computer from .NET code 2) you can’t send or receive a file via the WinRM Protocol. I wrote Naos.WinRM to solve both of these.
Project
Naos is an opensource project dedicated to providing infrastructure tools for free to aid in small companies having solid platforms to launch on, learn more at http://naosproject.com.
Source
The entire project is open sourced under the MIT license and available at: https://github.com/NaosFramework/Naos.WinRM
Referencing
The entire implemenation is in a single file so it can be included without taking a dependency on the NuGet package if that is necessary or preferred.
- Reference the NuGet package: http://www.nuget.org/packages/Naos.WinRM.
OR - Include the single file (will still REQUIRE System.Management.Automation.dll reference or package reference to Naos.External.MS-WinRM) in your project: https://raw.githubusercontent.com/NaosFramework/Naos.WinRM/master/Naos.WinRM/Naos.WinRM.cs.
Use
Below is an example of usage in C#.
// this is the entrypoint to interact with the system (interfaced for testing). var machineManager = new MachineManager( "10.0.0.1", "Administrator", MachineManager.ConvertStringToSecureString("xxx"), true); // will perform a user initiated reboot. machineManager.Reboot(); // can run random script blocks WITH parameters. var fileObjects = machineManager.RunScript("{ param($path) ls $path }", new[] { @"C:\PathToList" }); // can transfer files to AND from the remote server (over WinRM's protocol!). var localFilePath = @"D:\Temp\BigFileLocal.nupkg"; var fileBytes = File.ReadAllBytes(localFilePath); var remoteFilePath = @"D:\Temp\BigFileRemote.nupkg"; machineManager.SendFile(remoteFilePath, fileBytes); var downloadedBytes = machineManager.RetrieveFile(remoteFilePath);