码迷,mamicode.com
首页 > Windows程序 > 详细

C#中调用PowerShell代码

时间:2015-04-28 09:34:44      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

在C#中调用PowerShell代码,很多时候Add是不好使的!要用AddScript!记录一下!

        using (Runspace runspace = RunspaceFactory.CreateRunspace())
            {
                try
                {
                    runspace.Open();
                    Pipeline pipeline = runspace.CreatePipeline();
                    //必须要用AddScript。
                    pipeline.Commands.AddScript("Add-PSSnapin Microsoft.SharePoint.PowerShell");
                    pipeline.Commands.AddScript("$webApplications = Get-SPWebApplication");
                    pipeline.Commands.AddScript("$webApplicationUrls = $webApplications.url");
                    pipeline.Commands.AddScript("Get-SPWebApplication");
                    Collection<PSObject> results = pipeline.Invoke();
                    foreach (PSObject result in results)
                    {
                        MessageBox.Show(result.ToString());
                    }
                }
                catch (Exception ex) 
                {
                    MessageBox.Show(ex.ToString());
                }
            }

 

C#中调用PowerShell代码

标签:

原文地址:http://www.cnblogs.com/LanTianYou/p/4462047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!