PowerShell.Invoke()不执行任何操作

时间:2016-05-10 15:54:21

标签: c# powershell

我正在编写一个应该使用PowerShell执行远程命令的c#应用程序。我读了很多例子,这就是我的做法:

var command = "\"cmd.exe /C dir C:\\ > C:\\dir.log\"";
const string shellUri = @"http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
var credentials = new PSCredential("username", "password".GetSecureString());
var connection = new WSManConnectionInfo(false, "serverName" , 5985, "/wsman", shellUri,credentials);
connection.AuthenticationMechanism = AuthenticationMechanism.Default;
var runspace = RunspaceFactory.CreateRunspace(connection);
runspace.Open();
using (var powershell = PowerShell.Create())
{
    powershell.Runspace = runspace;
    powershell.AddScript(command);
    var results = powershell.Invoke();
}

我面临的问题是,一切似乎都在执行(代码方面),我得到了结果对象,但实际的cmd命令没有执行。 有什么想法吗?

0 个答案:

没有答案