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()); } }
本文来自博客园,作者:天外归云,转载请注明原文链接:https://www.cnblogs.com/LanTianYou/p/4462047.html