我正在尝试使用从Ubuntu到Windows创建的会话来调用命令 我正在使用Ubuntu 16.04,我使用.NET Core编写了一个项目,并安装了以下nuget软件包:
System.Management.Automation 6.02
Microsoft.WSMan.Management 6.02
Microsoft.PowerShell.SDK 6.02
Microsoft.PowerShell.Commands.Management 6.02
Microsoft.Management.Infrastructure 1.00
使用以下代码:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("$secpasswd = ConvertTo-SecureString \"MyPassowrd\" -AsPlainText -Force");
pipeline.Commands.AddScript("$mycreds = New-Object System.Management.Automation.PSCredential (\"admin\", $secpasswd)");
pipeline.Commands.AddScript("$session = New-PSSession -ComputerName 10.81.4.4 -port 5985 -Credential $mycreds -Authentication Negotiate");
pipeline.Commands.AddScript("Invoke-Command -Session $session -ScriptBlock {get-service}");
var resPipe = pipeline.Invoke();
我收到此错误:
Exception :The method or operation is not implemented. The method or operation is not implemented. at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)\n at System.Management.Automation.Runspaces.Pipeline.Invoke()
问题是,如果我在Ubuntu中使用powershell 6.0,并尝试使用上述命令进行会话,则可以正常工作,但是在使用.NET Core时,出现错误,
有人知道为什么吗?
答案 0 :(得分:0)
最终,我使用以下代码运行我的命令集:
public static string RunCommand(string cmd)
{
var base64EncodedString = Convert.ToBase64String(ASCIIEncoding.Unicode.GetBytes(cmd));
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "pwsh",
Arguments = $"-encodedcommand \"{base64EncodedString}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
cmd参数是我所有命令与的串联;分隔符。
对于Windows到Windows远程处理,文件名应为:
FileName = @"C:\Program Files\PowerShell\6.0.4\pwsh.exe",