代码
using System.Diagnostics;
var p = new Process//基本配置
{
StartInfo = {
WorkingDirectory = Environment.CurrentDirectory,
UseShellExecute = false,
FileName = "PowerShell",
CreateNoWindow = true,
Verb = "runas",//以管理员身份启动没用
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
}
};
p.Start();
p.StandardInput.WriteLine("Set-Item wsman:\\localhost\\Client\\Trustedhosts -value '123' -Force -Confirm:$false");
p.StandardInput.WriteLine("Enable-PSRemoting -SkipNetworkProfileCheck");
p.StandardInput.WriteLine("Get-Item wsman:\\localhost\\Client\\Trustedhosts");
p.StandardInput.Close();
String error = null;
String output = null;
while (!p.StandardOutput.EndOfStream)
{
output += p.StandardOutput.ReadToEnd();
error += p.StandardError.ReadToEnd();
}
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine(output);//输出执行信息
Console.WriteLine("-----------------------------------------------------------------------------");
Console.WriteLine(error);//输出执行信息
Console.WriteLine("-----------------------------------------------------------------------------");
p.Close();
结果