Establish Remote PowerShell Session with Azure virtual machine

I had to create a new VM on the Windows Azure to configure a SharePoint 2013 development environment and there was also a requirement of remotely executing a PowerShell script (ConfigureSharePointFarm.ps1) on my Virtual machine. I found many blogs on, how to use an Invoke-command and how to execute the script on a remote machine. And after struggling a lot, I finally succeeded in running the PowerShell script remotely. The following script is in a little simpler form than the other scripts.

Collect all information to establish Connection

First, we must collect the connection path to Window Azure Virtual machine to establish a connection for the remote session. We can identify the appropriate connection path using the Get-AzureWinRMUri cmdlet and store it in a variable for a later use:

$myService = “MySPEnvVM”
$myVMName=“MySPEnvImg”
$VmUri = Get-AzureWinRMUri –Service $myService –Name $ myVMName

After the connection path, the second thing is that, we need a credential to access the VM on the Azure. Here we need to pass our username and password and create a credential object as the following way and secure the password by using the ConvertTo-SecureString cmdlet and create a credential.

$secPassword = ConvertTo-SecureString $pwd -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $secPassword)

There is another approach to collect the credential information from the user, by providing a pop up credential dialog box to the user, enabling the user to put their credential information. We can use the following powershell cmdlet.

$cred=Get-Credential



Establish a remote Connection to the Virtual Machine

Since we have all the required information to connect to our virtual Image now, simply start a new Powershell console on your machine and use the Enter-PSSession command.

Enter-PSSession –ConnectionUri $VmUri –Credential $cred

If you are able to successfully connect to your VM, then it will move you to a different context (virtual machine) similar to the following image.


 
Execute PowerShell Script remotely

Now we have successfully established a connection to the virtual machine on the Window Azure. The next step is to execute the powershell script on my virtual Image.

Generally, we use an Invoke-Command Windows PowerShell cmdlet. Because it permits us to execute a Windows PowerShell script block in a non-interactive form that turns out to be useful when you run the configuration commands or when you are running a larger script. To be very specific, I have used an invoke-expression inside the -ScriptBlock to execute a powershell from a specific location.

Invoke-Command -ConnectionUri $uri -credential $cred -ScriptBlock {invoke-expression C:\ConfigureDeveloperDesktop\Scripts\ConfigureSharePointFarm.ps1"}

I was new to this “Invoke-Command” cmdlet, so I tried a lot of its options, to work it out, but the Invoke-expression helped me to execute the powershell script.
Ebook Download
View all
Learn
View all