我想用遥控机器指挥权力。 这是我使用的方法(当地人:131 是因为我使用隧道去遥远的机器5985号港口):
public string RunRemotePowerShellCommand(string command)
{
System.Security.SecureString password = new System.Security.SecureString();
foreach (char c in _password.ToCharArray())
{
password.AppendChar(c);
}
string schema = "http://schemas.microsoft.com/powershell/Microsoft.Powershell";
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false,
"localhost", 131, "/wsman", schema, new PSCredential(_domain + @"" + _userName, password));
using (Runspace remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
remoteRunspace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = remoteRunspace;
powershell.AddCommand(command);
powershell.Invoke();
Collection<PSObject> results = powershell.Invoke();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
}
}
我试图在指挥下行动:
D:FolderNamescriptName.ps1 -action editbinding -component "comp1","comp2","comp3","comp4"
与此类似:
RunRemotePowerShellCommand(@"D:FolderNamescriptName.ps1 -action editbinding -component ""comp1"",""comp2"",""comp3"",""comp4""");
但我收到:
Error: System.Management.Automation.RemoteException: The term D:FolderNamescriptName.ps1 -action editbinding -component "comp1","comp2","comp3","comp4" is not recognized as a name of cmdlet, function, script file, or operable program. Check the spelling of the name, or if the path is included, verify that the path is correct and try again.
这种方法用简单的指挥进行罚款,而我想要掌握的指挥在我用遥远的机器操作时ok。
提前感谢。
Regards, Dusan