我知道主题行看起来太普通了,并且有一些帖子解决了许多此类问题。我没有找到我正在寻找的东西,因此没有找到这篇文章。
我正在从机器 A 调用要在远程机器(机器 B)上执行的 powershell 文件。
//这里是代码片段:
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
string scripttext = "$secpasswd = ConvertTo-SecureString '222_bbbb' -AsPlainText –Force";
string scripttext1 = "$mycreds = New-object -typename System.Management.Automation.PSCredential('TS-TEST-09\\Administrator',$secpasswd)";
string scripttext2 = "$s = New-PSSession -ComputerName TS-TEST-09 -Credential $mycreds";
**string scripttext5 = "Invoke-Command -Session $s -FilePath 'helper.ps1' | out-null";**
pipeline.Commands.AddScript(scripttext);
pipeline.Commands.AddScript(scripttext1);
pipeline.Commands.AddScript(scripttext2);
pipeline.Commands.AddScript(scripttext5);
Collection<PSObject> results = pipeline.Invoke();
现在在行字符串 scripttext5 = "Invoke-Command -Session $s -FilePath 'helper.ps1' | out-null"; 我必须将一些参数(例如,来自 c# 环境的用户名:useralias)传递给这个 helper.ps1 文件进行处理。任何人都可以指导我正确的方法吗?
TIA,马尼什