Posted on Leave a comment

##[error]Script failed with error: Error: Unable to locate executable file: ‘pwsh’.

In AzureCLI@2 you may choose from a variety of options when it comes on how this task will be executed on the agent machine. I usually choose powershell for windows machines and powershell core for Unix based machines (pwsh).

Recently I got an error on a Windows machine when using Powershell core. The latest version of powershell which is currently on 7.* version can be used as pscore in the AzureCLI@2 task.

      - task: AzureCLI@2
        displayName: az cli task
        inputs:
          azureSubscription: 'SERVICE-CONNECTION'
          scriptType: 'pscore'
          scriptLocation: 'inlineScript'
          inlineScript: |
           script

Error message:

##[error]Script failed with error: Error: Unable to locate executable file: ‘pwsh’. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.

Solution:

In order to bypass this problem you should make sure that the latest version of powershell which is multiplatform should be installed on your system. At the time of this article this version is 7.*

After installing powershell you should make a restart also on the machine in order for the environmental variables to be added on PATH. Then you can execute your pwsh tasks on your agent machines.

Posted on Leave a comment

Initialize and format windows disk with powershell

Sometimes you may need to automatically create windows disk for a virtual server using an automation mechanism. With the below powershell you can initialize a new emtpy disk with GPT partition and format it according to your needs.

You can get your available disks using:

Get-Disk

You can use the below powershell and change the below settings:

DriveLetter : What your drive letter will be
AllocationUnitSize: Default is 4k, but in my case I define 64k
DiskNumber: number of disk from Get-Disk command
NewFileSystemLabel: name of the volume

 Initialize-Disk -number 4 -partitionstyle GPT ; New-Partition -DiskNumber 4 -UseMaximumSize ; Format-Volume -DriveLetter G -FileSystem NTFS -AllocationUnitSize 65536 -NewFileSystemLabel Volume-Name

After applying the volume will be created and initialized.

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

Install azure cli using Powershell – silent mode

Az cli is a very important tool that one devops engineer may need to install on systems. You can perform a silent install on a windows machine using the powershell below:

$msiFilePath = "azure-cli.msi"
Invoke-WebRequest https://aka.ms/installazurecliwindows -OutFile $msiFilePath
             
$MSIArguments = @(
    "/i"
    ('"{0}"' -f $msiFilePath)
    "/qn"
    "/norestart"
)               
Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow
Remove-Item -Path $msiFilePath  -Force

By running the powershell the download procedure will begin.

When the installation finishes you can locate it under installed programs:

https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?tabs=azure-cli