JudedeDude
JudedeDude

Reputation: 27

Can I run powershell commands from Azure DevOps to vm's in Azure?

How am I able to run PowerShell commands against a Windows 2016 vm in Azure without using winrm or pssession where I have to install\configure a self signed cert?

Does anyone know

Upvotes: 0

Views: 4611

Answers (4)

LoLance
LoLance

Reputation: 28216

Can I run powershell commands from Azure DevOps to vm's in Azure?

Run PowerShell scripts in your Windows VM by using Run Command provides two possible directions:

1.Azure CLI way:

You can use Azure CLI task in azure devops pipeline to run az vm start and az vm run-command invoke commands. The az vm start will start the VM, the second command will then run the PS commands.

2.Powershell way:

You can use Azure Powershell task in azure devops pipeline to run Start-AzVM and Invoke-AzVMRunCommand commands.

Note: It requires several seconds even when starting VM in web portal, so it's recommended to have 10~15 seconds as interval between these two commands(Start VM and Run PS). eg: Try Start-Sleep -s 15.

Upvotes: 1

Monika Reddy
Monika Reddy

Reputation: 973

If I understand your query correctly, you want to run PowerShell commands on Azure VM

  • In such a case, Azure DevOps service will not know where to direct the request, hence out of the box this is not possible.
  • However what you can do is, you can setup a Self hosted agent on this machine(as long it is connected to the internet).
  • Then you can run the task on a self hosted agent, and choose to run a powershell task. (This might need you to whitelist some devops ranges on your firewall).

Other than that, there are several ways to run commands and scripts on Azure VMs depending on the design you need to implement.

You can use Azure portal to execute predefined, custom PowerShell commands on a VM using Virtual Machine blade in Azure portal.

VM Blade

Also please take a look at these docs for more reference :

Run Powershell Cmds

Run Cmd

Upvotes: 1

Oleh Tarasenko
Oleh Tarasenko

Reputation: 612

If you are not required to run only one command frequently, you can simply run any PowerShell command just from the portal using Run command, please take a look at the screenshot.

enter image description here

Upvotes: 1

Hassan Raza
Hassan Raza

Reputation: 442

Better to use Azure Automation account than Azure DevOps if you want to do PowerShell.

Read this on how to create an automation account.

See here how to create a Runbook in Azure Automation using PowerShell.

Upvotes: 1

Related Questions