I m new in PS stuff, I develop st in c#, that connect a remote machine and start a few exe for me.
I tried some ps code for connect and start an exe in remote server. It s worked ok.
But i couldn’t get same result in c#.
My ps code in c# worked ok in local operation.
this is my c# code
thanks for help!
PowerShell ps = PowerShell.Create();
SecureString pass = new System.Net.NetworkCredential("", "12345").SecurePassword;
PSCredential credential = new PSCredential(@"user1", pass);
ps.AddCommand("Enter-PSSession");
ps.AddParameter("ComputerName", @"RemoteIP");
ps.AddParameter("Credential", credential);
ps.Invoke();
HLTECH is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3
As Mathias stated in the comments u can use invoke-command. Now there are two ways to do so: by using the -computername parameter for one time use or by creating a session and using the -session parameter:
$result= invoke-command -computername “nameOfClientToConnectTo” -scriptblock{
“Do something on this computer”}
$session= new-pssession -computername “nameOfClientToConnectTo”
$result= invoke-command -session $session -scriptblock{
“Do something on this computer”}
The scriptblock doesn’t know the variables u declare outside of it. So if u wanna work with them u Aamust hand them over via the -argumentlist parameter. U can also declare the scriptblock up front as variable which makes it very flexible to use especially in the combination with an argument list. I can provide another example for that if needed.
1