I'm trying to use a script like this:
$Server="remotepc"
$User="user"
$Password="password"
cmdkey /generic:$Server /user:$User /pass:$Password
mstsc /v:$Server /console
which works fine when running in powershell.
I'm trying to get this using runspace and pipeline in c#.
So this code works:
string server = "server";
string mstscScript = "mstsc /v:"+server;
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(mstscScript);
pipeline.Invoke();
runspace.Close();
However, if I add the script with the username and password it stops working and freezes.
So this code does not work.
string username = "user";
string password = "password";
string server = "server";
string cmdScript="cmd/genaric:"+server+" /user:$" + username" +
/pass:$" + password;
string mstscScript = "mstsc /v:" + server;
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(cmdScript);
pipeline.Commands.AddScript(mstscScript);
pipeline.Invoke();
runspace.Close();