Home > Net >  Ask questions about Powershell call c #
Ask questions about Powershell call c #

Time:11-22

Excuse me, I want to use the button event input
The following command in Powershell, how should I do, consult everybody, thank you,


 
$the Credential=New - Object System. Management Automation. PSCredential 'administrator', (convertto - securestring $(" qwe123! @ # ") - asplaintext - force)
Enter - PSSession 192.168.200.128 - the credential $the credential
Netsh advfirewall firewall set rule name='RDP' new action=allow



Code above excuse me, how should I in c # desktop button_click events do the above code, thank you

CodePudding user response:



Have a great god can help,,,

CodePudding user response:

Close test available
 private static string command="$the Credential=New - Object System. Management Automation. PSCredential 'administrator', (convertto - securestring $(' qwe123! @ # ') - asplaintext - force); + "
"Enter - PSSession 192.168.200.128 - the credential $the credential;" +
"The netsh advfirewall firewall set rule name='RDP' new action=allow";

The static void Main (string [] args)
{
Using (PowerShell ps=PowerShell. Create (RunspaceMode. NewRunspace)) {
Using (var PIP=ps. Runspace. CreatePipeline ())
{
Pip.Com mands. AddScript (command);
Foreach (var res in PIP. The Invoke ())
{
Console. WriteLine (res);
}
Foreach (var STR in PIP. Error. ReadToEnd ())
{
Console. WriteLine (STR);
}
}
}
The Console. ReadKey ();

}
  •  Tags:  
  • C#
  • Related