Invoke-MyFunction是我寫的一個命令行開關,它接受輸入文件並進行更改,並在指定位置創建新的輸出文件。如果我打開我的桌面,進口MyCommandlet.ps1在PowerShell中,並運行PowerShell命令行開關被調用但沒有響應
Invoke-MyFunction -InputPath path\to\input -OutputPath path\to\output
一切正常。但是,當我嘗試使用下面的代碼從C#程序導入和調用該命令時,commandlet不會運行,不記錄輸出並且不會生成輸出文件。它不會拋出CommandNotFoundException,所以我假定PowerShell對象識別我的commandlet。但我不明白爲什麼它不執行它。
//set up the PowerShell object
InitialSessionState initial = InitialSessionState.CreateDefault();
initial.ImportPSModule(new string[] { @"C:\path\to\MyCommandlet.ps1" });
Runspace runspace = RunspaceFactory.CreateRunspace(initial);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
//have MyFunction take input and create output
ps.AddCommand("Invoke-MyFunction");
ps.AddParameter("OutputPath", @"C:\path\to\output");
ps.AddParameter("InputPath", @"C:\path\to\input");
Collection<PSObject> output = ps.Invoke();
此外,調用MyFunction後,PowerShell對象ps無法執行任何其他命令。即使是已知的。
我試着調用'Copy-Item'而不是'Invoke-MyFunction',它的工作原理。所以我認爲關於'Invoke-MyFunction'的一些事情正在引發這個問題。 – dmeyerson