使用powershell导入模块配置管理器的C#项目

时间:2017-08-15 15:15:15

标签: c# powershell configurationmanager sccm

我正在使用Powershell集成创建这个C#项目。它可以很好地导入像ActiveDirectory这样的模块。

但我无法使用Configuration Manager,不会抛出错误,而是使用' Get-PSDrive'结果不显示ConfMgr Sitecode,就像在普通的Powershell窗口中一样,只显示来自普通Get-PSDrive命令的其他驱动器。

我已尝试过应用程序的x64和x86版本,无论是否有管理权限。

谁能告诉我这个问题可能是什么?

代码:

        try
        {
            InitialSessionState initial = InitialSessionState.CreateDefault();
            initial.ImportPSModule(new string[] { @"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1" });
            //initial.ImportPSModulesFromPath(@"C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\ConfigurationManager.psd1");

            Runspace runspace = RunspaceFactory.CreateRunspace(initial);
            runspace.Open();

            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(scriptText);
            pipeline.Commands.Add("Out-String");

            Collection<PSObject> results = pipeline.Invoke();

            runspace.Close();

            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject obj in results)
            {
                stringBuilder.AppendLine(obj.ToString());
            }

            return stringBuilder.ToString();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }

最诚挚的问候 托马斯尼森

0 个答案:

没有答案
相关问题