通过C使用Powershell执行脚本#
我需要执行以下脚本: 获取MailboxDatabase-状态|选择服务器名称、名称、数据库大小 我尝试了一些使用Powershell和命令类的解决方案,但都不起作用。 我收到的错误:通过C使用Powershell执行脚本#,powershell,Powershell,我需要执行以下脚本: 获取MailboxDatabase-状态|选择服务器名称、名称、数据库大小 我尝试了一些使用Powershell和命令类的解决方案,但都不起作用。 我收到的错误: 值参数不能为null。我认为这将满足您的要求: private string RunLocalExchangePowerShell(string script) { // create the runspace and load the snapin RunspaceConfiguration r
值参数不能为null。我认为这将满足您的要求:
private string RunLocalExchangePowerShell(string script)
{
// create the runspace and load the snapin
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
Runspace runSpace = RunspaceFactory.CreateRunspace(rsConfig);
runSpace.Open();
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);
Pipeline pipeLine = runSpace.CreatePipeline();
// load the script and convert the output to a string
pipeLine.Commands.AddScript(script);
pipeLine.Commands.Add("out-string");
// get the results
Collection<PSObject> results;
results = pipeLine.Invoke();
// loop through the results and build the output
StringBuilder sb = new StringBuilder();
foreach (PSObject obj in results)
{
sb.AppendLine(obj.ToString());
}
// close the pipeline and runspace
pipeLine.Dispose();
runSpace.Close();
return sb.ToString();
}
您是如何初始化psExec变量的?@TimurYaroshenko抱歉,我已经编辑并测试了上面的内容。
Console.WriteLine(prog.RunLocalExchangePowerShell("Get-MailboxDatabase -Status | select ServerName,Name,DatabaseSize"));