How to run powershell command on remote computer using azure release pipeline

run powershell command on remote computer using azure pipeline

Overview:

This post describes the way to run PowerShell command on a remote computer or azure virtual machine. Here, we will discuss two ways to achieve this remote execution of the command and the limitations :

  1. Creating PSSession and using Invoke-Command
  2. Using deployment group

Let’s begin!

1. Creating PSSession and using Invoke-Command :

In the first way, we create a PSSession to the remote machine. Upon a successful session creation, we execute a set of PowerShell command over it.

These sessions are persistent in nature. The session will be available and we can execute multiple commands over it.

We can use Power-Shell tasks in the azure release pipeline and write scripts to create PSSession and execute any commands. We can perform all possible Powershell operation over PSSession. i.e, Operating System management command and Application management command.

Limitation :

The major disadvantage of this way of run PowerShell command on the remote computer is that we need to enable and configure WinRM[window remote management] into the remote machine. also, we need to enable the port for HTTP and HTTPS to listen to remote request.

The WinRM configuration is a manual process and major drawback or limitation of this mechanism. The remote machine should be properly configured to accept remote request well in advance for creating the PSSession. You can find steps to configure the winrm in the remote machine in my previous post: https://techdiksha.com/configuring-winrm-with-https-in-azure-virtual-machine/

Please refer this link for more details on create PSSession : link

Below command can be used to create session to remote machine and run commands:

Create session :

$username = ‘<user_name of remote machine>‘
$password = ‘<password of remote machine>‘
$pso = New-PSSessionOption -SkipCACheck
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($username, $secpasswd)

$session = New-PSSession -ComputerName ‘IP or DNS name‘ -UseSSL -SessionOption $pso -Credential $credentials

Run commands over session :

We can execute any command over the session established.

Example: using below command we create a directory to the remote machine :

Invoke-Command -Session $session -ScriptBlock {New-Item -ItemType directory -Path C:\TestPackage -force}

 

2. Using a deployment group

In the second approach, We can use the deployment group for remote machine connection establishment.

A deployment group is used in azure DevOps for grouping the target machine for the deployment. We can register one or more remote system to the deployment group.

In release pipeline, we use deployment group task Job and specify a set of operation/tasks under it. It executes all set of operation to each of the remote machines separately.

Specify tags to the registered system helps us to identify the remote machine uniquely and we can set of tasks only for the tagged machine by specifying tag in the release pipeline.

Advantage:

The main benefit of using the deployment group is that connection management is done automatically by the azure agent. Manual creation of session and removing session will be avoided here.

Other benefits of using the deployment group are that we can write clean code/script.

Conclusion :

I would recommend using the deployment group approach for release pipeline as it has automatic connection management, automatic availability checks, parallel execution etc.

admin

Software professional, Blogger, Tech enthusiast.