我寫了2個應用程序之一與C#和另一個與PowerShell 1.0,在我的代碼的某些點我想傳遞一個字符串,表明服務器名稱從我的C#應用程序到我寫的PowerShell腳本文件,怎麼做我發了?我如何接受它?如何將字符串參數從C#發送到PowerShell?
我的代碼:
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
String scriptfile = @"c:\test.ps1";
Command myCommand = new Command(scriptfile, false);
CommandParameter testParam = new CommandParameter("username", "serverName");
myCommand.Parameters.Add(testParam);
pipeline.Commands.Add(myCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke();
runspace.Close();
和我的PowerShell腳本
param([string]$Username)
write-host $username
我缺少什麼?我有點新的PowerShell。
嘗試從PowerShell文件中除去「write-host $ username」行的所有內容 – laszlokiss88 2013-04-11 12:39:18
當您運行該命令時會發生什麼? PowerShell腳本是不是完全運行,或者輸出錯誤,或者根本沒有輸出,或者......?如果您直接從PowerShell運行PowerShell腳本,它可以工作嗎? – 2013-04-11 14:44:38
我發現一個答案是這樣的: 轉到開始菜單並搜索「Windows PowerShell ISE」。 右鍵單擊x86版本,然後選擇「以管理員身份運行」。 在頂部,粘貼Set-ExecutionPolicy RemoteSigned;運行腳本。選擇「是」。 但現在我得到了一個新問題。現在我得到: 無法找到接受參數'$ null'的位置參數。 有什麼想法? – woolford 2013-04-11 14:53:27