Share via

Powershell script to detect ado agent installed virtual machine

Sreenu Sanam 60 Reputation points
2024-01-05T17:26:14.0866667+00:00

I have multiple azure virtual machine (both linux and window) . Using powershell script I want to loop through multiple virtual machine (environment as well as deployment group in azure devops) and check if ado agent is missing then install ado agent on virtual machine one by one. Could you Please share example of powershell script to achieve this?

Azure Virtual Machines
Azure Virtual Machines

An Azure service that is used to provision Windows and Linux virtual machines.


Answer accepted by question author

v-vvellanki-MSFT 4,920 Reputation points Microsoft External Staff
2024-01-07T15:39:52.2+00:00

Hi @Sreenu Sanam,

Thanks for contacting Microsoft Q&A platform.
Certainly! Below is an example PowerShell script that demonstrates how to loop through multiple Azure virtual machines, check if the Azure DevOps agent is missing, and install it if necessary:

# Install Azure PowerShell module if not already installed

if (-not (Get-Module -Name Az -ListAvailable)) {

    Install-Module -Name Az -AllowClobber -Force -Scope CurrentUser

}

# Connect to Azure

Connect-AzAccount

# Set the target resource group and Azure DevOps organization

$resourceGroupName = "YourResourceGroupName"

$devOpsOrganization = "YourDevOpsOrganization"

# Get all Azure virtual machines in the specified resource group

$vms = Get-AzVM -ResourceGroupName $resourceGroupName

# Loop through each virtual machine

foreach ($vm in $vms) {

    $vmName = $vm.Name

    $vmStatus = $vm.PowerState

    # Check if the virtual machine is running

    if ($vmStatus -eq "VM running") {

        # Check if Azure DevOps agent is installed on the virtual machine

        $agentInstalled = Invoke-AzVMRunCommand -ResourceGroupName $resourceGroupName -Name $vmName -CommandId 'RunPowerShellScript' -ScriptPath 'C:\path\to\check-agent.ps1' | Out-String

        # If the agent is not installed, install it

        if ($agentInstalled -notmatch "Azure Pipelines Agent") {

            Write-Host "Azure DevOps agent is missing on VM '$vmName'. Installing..."

            # Run command to install Azure DevOps agent on the virtual machine

            Invoke-AzVMRunCommand -ResourceGroupName $resourceGroupName -Name $vmName -CommandId 'RunPowerShellScript' -ScriptPath 'C:\path\to\install-agent.ps1'

            Write-Host "Azure DevOps agent installed on VM '$vmName'."

        }

        else {

            Write-Host "Azure DevOps agent is already installed on VM '$vmName'."

        }

    }

    else {

        Write-Host "Virtual machine '$vmName' is not running."

    }

}

In the script:

  1. Make sure you have the Azure PowerShell module installed. If not, the script will install it using Install-Module.
  2. Replace "YourResourceGroupName" with the name of your target resource group.
  3. Replace "YourDevOpsOrganization" with the name of your Azure DevOps organization.
  4. Modify the paths in the Invoke-AzVMRunCommand commands to reference your own PowerShell scripts (check-agent.ps1 and install-agent.ps1) that handle checking and installing the Azure DevOps agent.
  5. The script uses Invoke-AzVMRunCommand to execute commands remotely on each virtual machine. Make sure you have the necessary permissions and network connectivity for running commands on the VMs.
  6. Customize the output and any additional logic as per your requirements.

Remember to replace the placeholders and customize the script according to your environment and specific needs.

Hope this helps you.

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.