Skip to content

Tag: PowerShell

Connect Powershell with Azure VM

Posted in Microsoft Azure

You can connect a PowerShell with your Azure VM and use it as if you were sitting in front of the remote machine.
But why would you want that?

  • It uses less more resources, because the server doesn’t need to show a graphical user interface
  • There is no Terminal Server license required
  • Recurring tasks can be completed faster by scripts.

The connection between PowerShell and your Azure VM could not be established by not only typing a few commands, you also need to do a few preparations on the server and client side.

Endpoint for die Azure VM

An endpoint (Private Port 5986) must be defined for PowerShell on the server side. When you create a new VM from the gallery in the Azure Management PortalAzure Management Portal creates this directly for you.

Under “endpoints” of your VM you can check if everything is correct or make subsequent changes or additions.

With PowerShell itself you can of course also manage the endpoints for the VM. The corresponding cmdlets are:

As you can control Azure VMs with PowerShell, I have described here.

SSL certificate for the client

Since Azure VMs are backed up on a self-signed SSL certificate, you must install this before for your client.
Call the URL https://YOURVM.cloudapp.net:YOURPUBLICPORT, best with Chrome on. There is no website that appears, but Chrome offers you to save the certificate.

Click on the appearing lock in the address bar, and in the following menu on “Certificate information”.
In the opening Certificate window you will find the tab “Details” the opportunity to save the certificate locally.
Just leave this all to the default settings.

Now open the saved certificate, and select the tab “General” then “Install Certificate”.
In the following dialogue you stay on “current user”, but select “all certificates in the following store” and the location “Trusted Root Certification Authorities”.

Have you done everything right you do not see this error message when you perform the next step.

Enter-PSSession : Connecting to remote server EUREVM.cloudapp.net failed with the following error message : The SSL connection cannot be established. Verify that the service on the remote host is properly configured to listen for HTTPS requests. Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM service: “winrm quickconfig -transport:https”. For more information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ Enter-PSSession -ComputerName EUREVM.cloudapp.net -Port 5986 -Credential A …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : InvalidArgument: (EUREVM.cloudapp.net:String) [Enter-PSSession], PSRemotingTransportException
   + FullyQualifiedErrorId : CreateRemoteRunspaceFailed

Connect PowerShell with Azure VM

With the following command you can now connect to the Public Port of Endpoint your PowerShell with the Azure VM::

Enter-PSSession -ComputerName YOURVM.cloudapp.net -Port YOURPUBLICPORT -Credential USERNAME –UseSSL

Control Azure VMs with Powershell

Posted in Microsoft Azure

Like any physical or virtual server, Microsoft Azure VMs can be started, rebooted and shut down. You can do this through the Microsoft Windows Azure Management Portal, or through the new Microsoft Azure Portal.
But you can also control Azure VMs with your local PowerShell.

Requirements


Clearly, an Azure subscription and in addition you need the Azure Powershell Modul.
This then installed everything else is needed, for example the .NET framework.

Connect


Start your Powershell and import the Azure CmdLets.

Import-Module 'C:\Program Files (x86)\Microsoft SDKs\Windows Azure\PowerShell\Azure\Azure.psd1'

Download the Azure Abonnement Certficate if you haven’t already one.
pre>Get-AzurePublishSettingsFile
An Azure website opens an the download starts. Save the file localy and open that path with your PowerShell.

Import that file in Powershell. Tip: use autocomplete with tabulator.

Import-AzurePublishSettingsFile 'PATH\SUB_NAME-DOWNLOAD_DATE-credentials.publishsettings'

Control Azure VMs with Powershell


With this CmdLet you get a list of VMs of the subscription.

Get-AzureVM | FT

Dies Zeigt euch die VM-Namen und die Service-Namen an. Beide braucht ihr für die weiteren Kommandos.
This shows you the names and service-names of your VMs. You need both information for further commands.
Start VM

Start-AzureVM -ServiceName YOURSERVICENAME -Name YOURVMNAME

Reboot VM

Restart-AzureVM -ServiceName YOURSERVICENAME -Name YOURVMNAME

Shuot down VM

Stop-AzureVM -ServiceName YOURSERVICENAME -Name YOURVMNAME–Force

This does completly shut down the VW, so that it it will not produce any costs.
VM löschen

Remove-AzureVM -ServiceName YOURSERVICENAME -Name YOURVMNAME –DeleteVHD

Caution: The VM and the hard drive are gone after that.

Summary


If you save the functions shown here in a script from, you can relatively quickly control Azure VMs with PowerShell so the check the status or high accordingly, shutdown or restart.
With the Azure Abonnement Zertifikat someone without access to the management portal can control the Azure VMs with Powershell.

You find the complete Azure Cmdlet Referenz at the MSDN and also examples how to create Azure VMs with Powershell.