我明白主题行看起来很平常,并且有帖子可以解决很多这样的问题。我没有找到我正在寻找什么,因此这篇文章。将参数传递给C#环境中的powershell脚本文件
我从机器A调用powershell文件在远程机器(机器B)上执行。
//这里是代码片段:
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 = “调用命令-Session $ S -FilePath 'helper.ps1' |外空”; 我必须传递一些参数(例如,来自c#environment:useralias的用户名)到这个helper.ps1文件进行处理。任何人都可以指导我采取正确的方法吗?
TIA, 马尼什
任何有关完整源代码的最终解决方案? – Kiquenet