Posted on Leave a comment

Execute powershell command without username password on Azure virtual machine

You can use az cli task on Azure devops in order to execute a powershell command inside a virtual machine without having to connect on it with username and password. In order to do that you have to use AzureCLI task and invoke a RunPowerShellScript.

    - task: AzureCLI@2
      displayName: execute command inside vm
      inputs:
        azureSubscription: 'subscription'
        scriptType: 'ps'
        scriptLocation: 'inlineScript'
        inlineScript: 'az vm run-command invoke --command-id RunPowerShellScript --name $(vm_name) -g $(vnet_rg_name) --scripts "hostname"'

You should use your own variables regarding the vm_name and rg_name. This task will execute on a windows-latest machine and will output the hostname of the machine that is given as input with the variables.

Posted on Leave a comment

Enable debug logs on Azure DevOps pipelines

In some cases you may need to troubleshoot your azure devops tasks inside a job and get a more detailed output than the default. You can enable a detailed log output using predefined variables. In more detail you can use System.Debug and set it to true.

variables:
  - name: System.Debug
    value: true

By doing so you will get more debug messages as shown in the below screenshot.

Posted on Leave a comment

remote: TF401019: The Git repository with name or identifier does not exist or you do not have permissions for the operation you are attempting.

Recently from a new devops project I tried to checkout an external repository from another devops project and I got a failure. In more detail I was working on a new pipeline on devops project B and I was trying to fetch a repository from azure devops project A.

remote: TF401019: The Git repository with name or identifier does not exist or you do not have permissions for the operation you are attempting.

Searching it further I realized that the error was the job scope. In order to resolve I disabled the job scope for the current project (the one from which I run the pipeline – project B).

After disabling “Limit job authorization scope to current project for non release pipelines” then I was able to get a successful checkout for the external repository.

Posted on Leave a comment

Install Azure DevOps agent using powershell

With Azure devops you can create incredible automations. But in many cases you will need first to install the azure devops agent in order to run your tasks. Using the below powershell you can automate the installation of an azure devops agent.

New-Item "C:\agent" -itemType Directory
cd "C:\agent"
$url = "https://dev.azure.com/YOUR_ORG"
$token = "PAT_TOKEN"
$auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$token"))

$package = Invoke-RestMethod "$url/_apis/distributedtask/packages/agent?platform=win-x64&$`top=1" -Headers @{Authorization = "Basic $auth"}

$fileName = $package.value[0].fileName;
$downloadUrl = $package.value[0].downloadUrl;
    

Invoke-WebRequest -UseBasicParsing $downloadUrl -OutFile agent.zip
Expand-Archive -Force agent.zip -DestinationPath .
Remove-Item -Force agent.zip


.\config.cmd --unattended --replace --acceptTeeEula --work work --url https://dev.azure.com/YOUR_ORG --pool YOUR_POOL_NAME --auth path --token $token --runAsService --runAsAutoLogon --windowsLogonAccount USER --windowsLogonPassword USER_PASSWORD
.\run.cmd

During the installation you may notice a problem for the powershell task to write on the log file.

Nevertheless the agent will be installed successfully.