The linked article describes how to use Exchange Powershell with C#,但它很麻烦,显然已经过时了。
在托管代码中调用远程PowerShell命令的正确或现代方法是什么?
以下只是感觉过于冗长和陈旧。
Runspace myRunSpace = RunspaceFactory.CreateRunspace();
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
Command getCommand = new Command("Get-Command");
pipeLine.Commands.Add(getCommand);
Command getMember = new Command("Get-Member");
pipeLine.Commands.Add(getMember);
Collection<PSObject> commandResults = pipeLine.Invoke();