How can I install .NET 8.0.12 on multiple VMs on azure?

Anonymous
2025-02-20T19:20:06.8066667+00:00

I need a script that will enable me to install .NET 8.0 on a multiple VMs in Azure.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Divyesh Govaerdhanan 10,310 Reputation points
    2025-02-21T22:20:56.8233333+00:00

    Hello,

    Welcome to Microsoft Q&A,

    You can use the below PowerShell script to install the .Net 8 on all the VMs.

    # Azure Login (if not already authenticated)
    Connect-AzAccount
    
    # Define variables
    $resourceGroup = "YourResourceGroup"
    $targetVMs = @("VM1", "VM2", "VM3")  # List of specific VM names
    
    
    # PowerShell script for remote execution
    $remoteScript = @"
    # Ensure winget is installed
    if (-not (Get-Command winget -ErrorAction SilentlyContinue)) {
        Write-Host "winget not found, skipping installation"
        exit 1
    }
    
    # Install .NET 8.0
    Write-Host "Installing .NET 8.0..."
    winget install Microsoft.DotNet.Runtime.8 --silent --accept-package-agreements --accept-source-agreements
    
    # Uninstall older .NET versions (1.x - 7.x)
    Write-Host "Checking for older .NET versions..."
    $installedVersions = Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name LIKE 'Microsoft .NET%'" | Select-Object Name
    foreach (\$version in \$installedVersions) {
        if (\$version.Name -match 'Microsoft .NET (Core|Runtime) [1-7]\.') {
            Write-Host "Uninstalling: \$version.Name"
            \$app = Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name='\$version.Name'"
            \$app.Uninstall()
        }
    }
    Write-Host "Update completed successfully!"
    "@
    # Iterate through only the specified VMs
    foreach ($vmName in $targetVMs) {
        $vm = Get-AzVM -ResourceGroupName $resourceGroup -Name $vmName -Status
        if ($vm -eq $null) {
            Write-Host "VM $vmName not found in resource group $resourceGroup. Skipping..."
            continue
        }
        if ($vm.PowerState -eq "VM running") {
            Write-Host "Processing VM: $vmName - Installing .NET 8 and removing old versions"
            Invoke-AzVMRunCommand -ResourceGroupName $resourceGroup -Name $vmName -CommandId "RunPowerShellScript" -ScriptString $remoteScript
        } else {
            Write-Host "Skipping $vmName: VM is not running"
        }
    }
    
    
    

    Please Upvote and Accept the answer if it helps!!


  2. Anonymous
    2025-02-25T19:24:58.18+00:00

    I got stuck with the following error:

    At line:11 char:30

    •     Write-Host "Skipping $vmName: VM is not running"
      
    •                          ~~~~~~~~
      

    Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to

    delimit the name.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    
    + FullyQualifiedErrorId : InvalidVariableReferenceWithDrive
    
    0 comments No comments

  3. Hendrik Schilt 0 Reputation points
    2025-12-10T08:30:20.8366667+00:00

    Hello,

    I was experiencing the same issue, but I managed to resolve it using the following powershell script. I'm sharing this for anyone else who, like me, stumbled upon this Q&A without finding a working solution.

    if ($null -eq (Get-AzContext)) {
        Connect-AzAccount
    }
    $AllWindowsVMs = Get-AzVM -Status | Where-Object { $_.StorageProfile.OsDisk.OsType -eq 'Windows' }
    $SelectedVMs = $AllWindowsVMs | Select-Object Name, ResourceGroupName, PowerState, Location | Out-GridView -PassThru -Title "Selecteer de VM's (Ctrl+Click voor meerdere)"
    if ($null -eq $SelectedVMs) {
        Write-Warning "No VM's selected"
        break
    }
    $LocalScriptPath = "$env:TEMP\InstallDotNetRunCommand.ps1"
    $ScriptContent = @'
    $Url = 'https://builds.dotnet.microsoft.com/dotnet/Runtime/8.0.22/dotnet-runtime-8.0.22-win-x64.exe'
    $Dest = "$env:TEMP\dotnet-runtime-8.0.22-win-x64.exe"
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    try {
        Invoke-WebRequest -Uri $Url -OutFile $Dest -UseBasicParsing -ErrorAction Stop
    }
    catch {
        Write-Error "Download failed: $_"
        return
    }
    if (Test-Path $Dest) {
        $Proc = Start-Process -FilePath $Dest -ArgumentList '/install', '/quiet', '/norestart' -PassThru -Wait
        Write-Output "Installatie op $env:COMPUTERNAME afgerond. ExitCode: $($Proc.ExitCode)"
        
        Remove-Item -Path $Dest -Force -ErrorAction SilentlyContinue
    }
    '@
    Set-Content -Path $LocalScriptPath -Value $ScriptContent -Force
    foreach ($VM in $SelectedVMs) {
        $IsRunning = $false
        
        try {
            $VMStatus = Get-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Status -ErrorAction Stop
            if ($VMStatus.Statuses) {
                foreach ($Status in $VMStatus.Statuses) {
                    if ($Status.Code -eq 'PowerState/running') {
                        $IsRunning = $true
                        break
                    }
                }
            }
            if ($IsRunning) {
                Write-Output "VM $($VM.Name) is active. Start installation..."
                $Result = Invoke-AzVMRunCommand -ResourceGroupName $VM.ResourceGroupName -VMName $VM.Name -CommandId 'RunPowerShellScript' -ScriptPath $LocalScriptPath -ErrorAction Stop
                Write-Output $Result.Value.Message
            }
            else {
                Write-Warning "VM $($VM.Name) is not turned on. Will be skipped."
            }
        }
        catch {
            Write-Error "Error executing on $($VM.Name): $_"
        }
    }
    if (Test-Path $LocalScriptPath) {
        Remove-Item -Path $LocalScriptPath -Force
    }
    
    0 comments No comments

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.