我见过很多解决方案,但没有一个能解决我的问题。我正在尝试将一个名为“DSInternals”的自定义 PowerShell 模块导入我的 C# DLL。
https://github.com/MichaelGrafnette/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
}