Search code examples
.netpowershellazure-devops

PowerShell issue when trying to install .Net SDK in Azure Pipelines


I'm trying to run pipeline job in Azure Devops using PowerShell to install .Net SDK on my servers.

This works

    # Install sdk on each server
    Invoke-Command -ComputerName $buildServers -Credential $credential {
        c:\temp\dotnet-sdk-8.0.303-win-x64.exe /install /quiet /norestart" | Out-Null
    }

But when I try to make the exe a variable $sdkVersion, I don't get an error but it certainly doesn't install it.

    # Install sdk on each server
    $sdkVersion = 'dotnet-sdk-8.0.303-win-x64.exe'
    Invoke-Command -ComputerName $buildServers -Credential $credential {
        "c:\temp\$sdkVersion /install /quiet /norestart" | Out-Null
    }

I am also open to easier ways to do this.


Solution

  • "c:\temp\$sdkVersion /install /quiet /norestart" is just creating a string which get's then sent to $null via Out-Null. In addition, $sdkVersion is also $null as it doesn't exist in the context of the parallel invocation. You can use the $using: scope modifier to pass-in the variable value to this scope. What you need to do is first construct the path to your executable "c:\temp\$using:sdkVersion" then you can invoke it using & or . and add the required arguments as needed.

    # Install sdk on each server
    $sdkVersion = 'dotnet-sdk-8.0.303-win-x64.exe'
    Invoke-Command -ComputerName $buildServers -Credential $credential {
        & "c:\temp\$using:sdkVersion" /install /quiet /norestart | Out-Null
    }