C#中调用PowerShell代码

在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());
                }
            }

 

上一篇:阿里云创建E-MapReduce 4 创建执行计划


下一篇:阿里云E-MapReduce 创建集群