如何将powershell脚本中的值转换为C#变量?

如何将powershell脚本中的值转换为C#变量?,c#,powershell,C#,Powershell,我有.ps1脚本,它应该返回当前用户(或管理员)的SID。但我不知道如何将该脚本中的值输入到我的C#代码中。谁能帮帮我吗 目前我以这种方式调用一个脚本: ProcessStartInfo newProcessInfo = new ProcessStartInfo(); newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe"; newProcessInfo.Verb = "runa

我有.ps1脚本,它应该返回当前用户(或管理员)的SID。但我不知道如何将该脚本中的值输入到我的C#代码中。谁能帮帮我吗

目前我以这种方式调用一个脚本:

ProcessStartInfo newProcessInfo = new ProcessStartInfo();
newProcessInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
newProcessInfo.Verb = "runas";
newProcessInfo.Arguments = @"sfc /scannow";
Process.Start(newProcessInfo);

newProcessInfo.Arguments = @"-Command ""sfc /scannow""";
newProcessInfo.Arguments = @"–ExecutionPolicy Bypass -File ""c:\\Users\\HP\\Desktop\\HotelMode-PB\\HotelMode.ps1""";
我需要从另一个.ps1脚本获取用户的SID,然后在这个cmd命令中使用它:

HotelMode.ps1-UserSid”“[-debug]


直接使用
System.Management.Automation
API,而不是启动
powershell.exe

using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
// ...
using(PowerShell ps = PowerShell.Create())
{
    ps.AddCommand("Set-ExecutionPolicy")
      .AddParameter("ExecutionPolicy","Bypass")
      .AddParameter("Scope","Process")
      .AddParameter("Force");

    ps.AddScript(@"C:\Users\HP\Desktop\HotelMode-PB\HotelMode.ps1");

    Collection<PSObject> result = ps.Invoke();
    foreach(var outputObject in result)
    {
        // outputObject contains the result of the powershell script
    }
}
使用系统;
使用System.Collections.ObjectModel;
使用系统、管理、自动化;
// ...
使用(PowerShell ps=PowerShell.Create())
{
ps.AddCommand(“设置执行策略”)
.AddParameter(“ExecutionPolicy”、“Bypass”)
.AddParameter(“范围”、“过程”)
.AddParameter(“力”);
ps.AddScript(@“C:\Users\HP\Desktop\HotelMode PB\HotelMode.ps1”);
收集结果=ps.Invoke();
foreach(结果中的var outputObject)
{
//outputObject包含powershell脚本的结果
}
}

如何调用ps1脚本?请出示密码。一种典型的方法是脚本回显结果,如果脚本成功,调用方将字符串解析为变量。有关如何编辑您的问题,请参阅和。我已更新了我的问题,请查看。请在StackOverflow搜索框中键入:“C#PowerShell”--我看到了许多示例