1
我看到了很多解決方案,但沒有解決我的問題。 我想導入一個名爲「DSInternals」的自定義PowerShell模塊到我的C#DLL中。從C#導入PowerShell模塊失敗
https://github.com/MichaelGrafnetter/DSInternals
一切都在我的代碼似乎就好了,但是當我試圖得到它沒有加載可用的模塊。
流與
術語「GET-ADReplAccount」未被識別爲cmdlet,函數,腳本文件或可操作的程序的名稱響應。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後重試。
我在哪裏出錯了這段代碼?
InitialSessionState init = InitialSessionState.CreateDefault();
init.ImportPSModule(new string[] { @"D:\\DSInternals\\dsinternals.psd1" }); //location of the module files
Runspace runspace = RunspaceFactory.CreateRunspace(init);
runspace.Open();
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.Commands.AddCommand("Get-ADReplAccount"); //this command is un-recognized
foreach (PSObject result in ps.Invoke())
{
Console.WriteLine(result); //this always returns null
}
*此命令是未確認的*最後的代碼你怎麼知道的?拋出的任何異常? 'ps.Streams.Error'中的任何錯誤? – PetSerAl
這些流以「Get-ADReplAccount」這一術語不被識別爲cmdlet,函數,腳本文件或可操作程序的名稱。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是正確的,然後再試一次。「 DSInternals模塊包含此cmdlet –
將此添加到您的代碼中:'init.ThrowOnRunspaceOpenError = true;'。 – PetSerAl