C# 在C中使用winrm在远程windows上运行命令#

C# 在C中使用winrm在远程windows上运行命令#,c#,winrm,C#,Winrm,我有一种使用winrm从本地windows计算机连接到远程windows计算机的简单方法。 以下是正在运行的powershell代码: Set-Item WSMan:\localhost\Client\TrustedHosts -Value $ip -Force $securePassword = ConvertTo-SecureString -AsPlainText -Force 'mypass' $cred = New-Object System.Management.Automation.

我有一种使用winrm从本地windows计算机连接到远程windows计算机的简单方法。 以下是正在运行的powershell代码:

Set-Item WSMan:\localhost\Client\TrustedHosts -Value $ip -Force
$securePassword = ConvertTo-SecureString -AsPlainText -Force 'mypass'
$cred = New-Object System.Management.Automation.PSCredential 'Administrator', $securePassword
$cmd = {ls C:\temp}
Invoke-Command -ComputerName $ip -Credential $cred -ScriptBlock $cmd
我想弄清楚如何在c#中做正确的事情

另外,如果有人告诉我是否有一种方法可以在c#winrm中发送文件,这将非常有用


注意:在我的本地机器上只需要一个c代码。远程计算机已经设置好了。

好吧,我找到了一种方法,我将在下面发布,但尽管它在windows 8上运行良好,但在windows 7上遇到错误“强名称验证失败”,因此我应该继续研究这个问题。 但请随时发布其他想法

-->将System.Management.Automation.dll添加到项目中

    WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
    connectionInfo.ComputerName = host;
    SecureString securePwd = new SecureString();
    pass.ToCharArray().ToList().ForEach(p => securePwd.AppendChar(p));
    connectionInfo.Credential = new PSCredential(username, securePwd);
    Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
    runspace.Open();
    Collection<PSObject> results = null;
    using (PowerShell ps = PowerShell.Create())
    {
        ps.Runspace = runspace;
        ps.AddScript(cmd);
        results = ps.Invoke();
        // Do something with result ... 
    }
    runspace.Close();

    foreach (var result in results)
    {
        txtOutput.AppendText(result.ToString() + "\r\n");
    }
WSManConnectionInfo connectionInfo=newwsmanconnectioninfo();
connectionInfo.ComputerName=主机;
SecureString securePwd=新SecureString();
pass.ToCharArray().ToList().ForEach(p=>securePwd.AppendChar(p));
connectionInfo.Credential=新的PSCredential(用户名,securePwd);
Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo);
Open();
收集结果=空;
使用(PowerShell ps=PowerShell.Create())
{
ps.运行空间=运行空间;
ps.AddScript(cmd);
结果=ps.Invoke();
//做一些有结果的事情。。。
}
runspace.Close();
foreach(结果中的var结果)
{
txtOutput.AppendText(result.ToString()+“\r\n”);
}

我有一篇文章介绍了一种从.NET上通过WinRM运行Powershell的简单方法

如果您只想复制代码,那么代码将位于单个文件中,并且它也是一个NuGet包,其中包含对System.Management.Automation的引用

它可以自动管理受信任的主机,可以运行脚本块,还可以发送文件(这实际上不受支持,但我创建了一个解决方案)。返回的总是来自Powershell的原始对象

// 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 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);
如果这有帮助,请标记为答案。我已经在自动化部署中使用了一段时间了。如果您发现问题,请留下评论