【发布时间】:2019-08-25 14:01:26
【问题描述】:
我的 C# 文件中有以下函数:
private void RunScriptFile(string scriptPath, string computerName, PSCredential credential)
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
string scriptCommand = "Invoke-Command -ComputerName " + computerName + " -FilePath " + scriptPath + " -ArgumentList " + credential;
pipeline.Commands.AddScript(scriptCommand);
Collection<PSObject> results = pipeline.Invoke();
runspace.Close();
}
我正在使用上述 C# 代码中传递的凭据调用以下 PowerShell 脚本; 脚本.ps1
Param([PSCredential]$Credentials)
<code part using credentials>
pipeline.Invoke() 之后,C# 代码直接关闭,没有任何操作,也不会抛出任何错误。
拨打电话时我做错了什么吗? 如果像下面这样从 PowerShell 调用,同样的调用可以正常工作:
Invoke-Command -ComputerName <computerName> -FilePath <scipt.ps1> -ArgumentList " + credential;
【问题讨论】:
-
您确定要拨打
.AddScript(x)而不仅仅是.Add(x)吗? -
@LarryTang 我是这么认为的,因为我尝试在没有 Credential 参数的情况下使用
.AddScript(x)运行它并且它有效,但我将不得不检查.Add(x)做了什么。
标签: c# powershell