I am trying to access Azure VM's Serial console to execute some powershell script. How can I do it using C# code?
Asked
Active
Viewed 447 times
2
-
The answer to this question would be "Visual Studio has nothing to do with remote access". You can start Azure CLI from VS but you can also do that from the command line. To start a remote desktop session, use Remote Desktop. – Panagiotis Kanavos May 07 '19 at 10:08
-
I suspect the question is how to execute a remote Powershell script, not how to start a console (there's no "Serial" console by the way). Powershell already offers remote sessions and remote execution. You'll find *many* SO questions that describe how to use Powershell from C# and how to create a remote session in code, [eg this one](https://stackoverflow.com/questions/44519600/execute-powershell-script-on-a-remote-computer-using-c-sharp). An Azure VM may have special security requirements though. – Panagiotis Kanavos May 07 '19 at 10:11
-
Why do you want to execute a Powershell script remotely though? Are you trying to run a setup script? A scheduled job? A maintenance script? There are probably better ways to do what you want – Panagiotis Kanavos May 07 '19 at 10:13
1 Answers
2
You could use the command to run the powershell command in VM, make sure you have the Az module.
Connect-AzAccount
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -Name 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter @{param1 = "var1"; param2 = "var2"}
To run the powershell with c#, you could follow the links:
1.Executing PowerShell scripts from C#
2.Execute PowerShell Script from C# with Commandline Arguments

Joy Wang
- 39,905
- 3
- 30
- 54