Invoke Powershell command from C# with different credential Invoke Powershell command from C# with different credential powershell powershell

Invoke Powershell command from C# with different credential


Untested code...but this should work for you. I use something similar to run remote powershell (Just set WSManConnectionInfo.ComputerName).

public static Collection<PSObject> GetPSResults(string powerShell,  PSCredential credential, bool throwErrors = true){    Collection<PSObject> toReturn = new Collection<PSObject>();    WSManConnectionInfo connectionInfo = new WSManConnectionInfo() { Credential = credential };    using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))    {        runspace.Open();        using (PowerShell ps = PowerShell.Create())        {            ps.Runspace = runspace;            ps.AddScript(powerShell);            toReturn = ps.Invoke();            if (throwErrors)            {                if (ps.HadErrors)                {                    throw ps.Streams.Error.ElementAt(0).Exception;                }            }        }        runspace.Close();    }    return toReturn;}