1

I have .ps1 script which should return me SID of current user (or administrator). But i have no idea how can i get this value from that script into my C# code. Can somebody help me, please?

Currently i am calling one script in this way:

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""";

And i need to get SID of user from another .ps1 script and then use it in this cmd command:

HotelMode.ps1 -UserSid "" [-debug]

user7968180
  • 145
  • 1
  • 15
  • 1
    How are you calling the ps1 script? Please show code. A typical approach is for the script to echo results and the caller parses the string into variables if the script succeeded. See [ask] and [mcve] for how to edit your question. – Dave S Jun 10 '17 at 19:17
  • I have updated my question, have a look please. – user7968180 Jun 10 '17 at 19:20
  • Type this in StackOverflow search box: "C# PowerShell" -- I see many examples – Dave S Jun 10 '17 at 19:29

1 Answers1

1

Use the System.Management.Automation API directly instead of launching 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
    }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206