Posted on 1 Comment

Install python silently on Windows container and add to PATH

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

In this article we will examine how we can silently install python on a container running windows and add it to path. By doing so we can run python scripts inside our container and perform various tests, or even create webservices that will be hosted as containers inside a Cloud provider.

In order to host python inside a windows system I am using the below Dockerfile. A file named configuration.ps1 could exist inside your building directory and contain other instructions like modules installation. If you do not want this step you can remove the last two lines of the Dockerfile.

FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-windowsservercore-ltsc2019
 RUN Invoke-WebRequest -UseBasicParsing https://www.python.org/ftp/python/3.10.7/python-3.10.7-amd64.exe -OutFile python.exe; `
    Start-Process python.exe -Wait -ArgumentList /PrependPath=1, /quiet; `
    Remove-Item -Force python.exe
COPY ./configuration.ps1 .
CMD .\configuration.ps1

When python is installed inside your container you will need to add it on PATH in order to use the verb python. This is done by appending the installation directory on windows PATH.

In order to do you will need to get an interactive session with your container.

docker exec -it container_name powershell

With the below command you can find where is the python installation located. You will need to change python version to the one that you installed. If you had installed the version 3.5 then you would need to change 3.10 to 3.5.

py -3.10 -c "import sys; print(sys.executable[:-10])"

The last step would be to set this location in the PATH. You can do that using the command below.

setx path "%path%;C:\Users\ContainerAdministrator\AppData\Local\Programs\Python\Python310\"

Then you can just press python and voilà.

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

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