如何在C#中使用powershell invoke命令?
如果我在Powershell中运行以下命令,它将按预期工作如何在C#中使用powershell invoke命令?,c#,powershell,runspace,scriptblock,C#,Powershell,Runspace,Scriptblock,如果我在Powershell中运行以下命令,它将按预期工作 invoke命令-computername[name]-scriptblock{ipconfig.exe>c:\ipconfig.txt} 但当我试图将其合并到c#函数中时,我得到了这个错误 {”无法绑定参数“ScriptBlock”。无法转换 \“ipconfig.exe>c:\ipconfig.txt\”类型为“System.String\”的值 键入\“System.Management.Automation.ScriptBloc
invoke命令-computername[name]-scriptblock{ipconfig.exe>c:\ipconfig.txt}
但当我试图将其合并到c#函数中时,我得到了这个错误
{”无法绑定参数“ScriptBlock”。无法转换
\“ipconfig.exe>c:\ipconfig.txt\”类型为“System.String\”的值
键入\“System.Management.Automation.ScriptBlock\”}
即使我正在将scriptblock参数值转换为System.Management.Automation.scriptblock对象?我在做什么
private void btnInstallTest_Click(object sender, EventArgs e)
{
List<string> commands = new List<string>();
commands.Add("invoke-command");
List<Tuple<String, String>> parameters = new List<Tuple<string, string>>();
parameters.Add(new Tuple<string, string>("computername", @"server"));
parameters.Add(new Tuple<string, string>("ScriptBlock", @"ipconfig.exe > c:\ipconfig.txt"));
Collection<PSObject> psobject = runRemotePowerShellCommands(commands, parameters, "server", @"domain\user", convertToSecureString("password"));
}
private Collection<PSObject> runRemotePowerShellCommands(List<string> commands, List<Tuple<String, String>> parameters, string remoteMachineName, string domainAndUsername, SecureString securePW)
{
Collection<PSObject> psobjs = new Collection<PSObject>();
string result = "";
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
PSCredential psCredential = new PSCredential(domainAndUsername, securePW);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, remoteMachineName, 5985, "/wsman", shellUri, psCredential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; //.Basic;
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
PowerShell powershell = PowerShell.Create();
for (int i = 0; i < commands.Count; i++)
{
if (commands[i].Contains(";"))
{
string[] commandsSplit = commands[i].Split(';');
}
else
{
powershell.AddCommand(commands[i]);
}
System.Management.Automation.ScriptBlock sb = null;
if (parameters != null)
{
foreach (Tuple<string, string> param in parameters)
{
if (param.Item1.ToLower() == "scriptblock")
{
sb = ScriptBlock.Create(param.Item2);
powershell.AddParameter(param.Item1, sb);
//powershell.AddParameter(param.Item1, param.Item2);
}
else
{
powershell.AddParameter(param.Item1, param.Item2);
}
}
}
if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
{
// do nothing, the runspace is already open
}
else
{
runspace.Open();
powershell.Runspace = runspace;
}
try
{
psobjs = powershell.Invoke();
if (powershell.HadErrors == true)
{
result = "Failed - " + powershell.Streams.Error[0].ToString();
result = result.Replace("\"", "*");
psobjs.Add(result);
}
}
catch (Exception ex)
{
result = "Failed: " + ex.Message;
psobjs.Add(result);
}
}
powershell.Commands.Clear();
}
return psobjs;
}
private void btnInstallTest\u单击(对象发送方,事件参数e)
{
List命令=new List();
命令。添加(“调用命令”);
列表参数=新列表();
添加(新元组(“computername”,“服务器”));
添加(新元组(“ScriptBlock”,@“ipconfig.exe>c:\ipconfig.txt”);
集合psobject=runRemotePowerShellCommands(命令、参数、“服务器”、“域\用户”、ConvertToSecurity(“密码”);
}
私有集合runRemotePowerShellCommands(列表命令、列表参数、字符串remoteMachineName、字符串domainAndUsername、SecureString securePW)
{
集合psobjs=新集合();
字符串结果=”;
字符串shellUri=”http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
PSCredential PSCredential=新的PSCredential(域和用户名,securePW);
WSManConnectionInfo connectionInfo=新的WSManConnectionInfo(false,remoteMachineName,5985,“/wsman”,shellUri,psCredential);
connectionInfo.AuthenticationMechanism=AuthenticationMechanism.Kerberos;/.Basic;
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo))
{
PowerShell PowerShell=PowerShell.Create();
for(int i=0;i
我觉得你把这件事搞得太复杂了:
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript($@"Invoke-Command -ComputerName {name} -ScriptBlock {{ipconfig > C:\ipconfig.txt}}")
ps.Invoke()
}
或者,您可以链接
.AddCommand().AddParameter()
我觉得您已经把这件事复杂化了:
using (PowerShell ps = PowerShell.Create())
{
ps.AddScript($@"Invoke-Command -ComputerName {name} -ScriptBlock {{ipconfig > C:\ipconfig.txt}}")
ps.Invoke()
}
或者,您可以链接
.AddCommand().AddParameter()
或者,为了获取网络配置信息,您可以使用WMI查询。或者,为了获取网络配置信息,您可以使用WMI查询。这工作完美无瑕,谢谢。对于这个问题,我的功能绝对过于复杂,我以前使用过它,但它没有问题。我不确定为什么我会出现这个错误?Tuple
您的字符串与scriptblock
的比较失败,因此它被作为字符串传递,而不是创建正确的类型。@JayThis工作得非常完美,谢谢。由于我对这个问题的函数绝对过于复杂,我以前使用过它,但它工作得没有问题。我不确定为什么我会遇到这个问题t错误?Tuple
您的字符串与scriptblock
的比较失败,因此它被作为字符串传递,而不是创建正确的类型。@Jay