Search code examples
powershellazure-devopsazure-pipelinesazure-powershellazure-keyvault

Get-AzKeyVaultSecret -VaultName..... | Name or service not known


I have a pipeline in which i calling a power-shell script which copy the azure keyvault secrets from one key-vault to another keyvault. Here's the powershell script:

    $SecretNames =   "api-gateway--jwt-public-key",
    "authentication-service--jwt-private-key",
    "user-management--pen-password",
    "user-management--stripe-secret-key"

    $sourceVaultName="fdevcuskv03"
    $destVaultName="fdevcuskv04"

    for (($i = 0); $i -lt $SecretNames.Count; $i++)
    {
        $sourceSecretName = "$($SecretNames[$i])"
        $destSecretName = "$($SecretNames[$i])"
        
        $Getvalue=(Get-AzKeyVaultSecret -VaultName $sourceVaultName -Name $sourceSecretName).SecretValue
        Write-Host "Copying $sourceSecretName Value To $destSecretName"

        
        Set-AzKeyVaultSecret -VaultName $destVaultName -Name $destSecretName `
            -SecretValue $Getvalue
    }

When I run the pipeline, I got this error but this works fine locally. Here's the error:

    Get-AzKeyVaultSecret: /home/vsts/work/1/s/Terraform/Terraform-Scripts/main.ps1:351
    Line |
    351 |  …  $Getvalue=(Get-AzKeyVaultSecret -VaultName $sourceVaultName -Name $s …
        |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        | Name or service not known

I'm bit confused, what i'm doing wrong.


Solution

  • Along with checking that please check also if the case maybe dns resolution issue or invalid dns cache causing the error .

    For that please try to give it sleep time and repeat the step.(Also check by dns flush ) Place check azure-powershell issues(github) comment by @placidseven ang set azure keyvault by first checking if dns Is resolved.

    foreach(($i = 0); $i -lt $SecretNames.Count; $i++)
        {
            $sourceSecretName = "$($SecretNames[$i])"
            $destSecretName = $sourceSecretName 
            
            $Getvalue=(Get-AzKeyVaultSecret -VaultName $sourceVaultName -Name $sourceSecretName).SecretValue
            Write-Host "Copying $sourceSecretName Value To $destSecretName"
    setSecret
    
    function setSecret{
        while (!$secret) {
    $DnsCheck = Resolve-DnsName $VaultURI -ErrorAction SilentlyContinue
    
         if (!$DnsCheck) {
            write-host "Resolve-DnsName taking time to resolve $vaultName. Keep trying!"
            Start-Sleep -Seconds 30
    Set-AzKeyVaultSecret -VaultName $destVaultName -Name  $destSecretName `
                -SecretValue $Getvalue -ErrorAction SilentlyContinue
        }
    }
    
    $secret = Set-AzKeyVaultSecret -VaultName $destVaultName -Name  $destSecretName `
                -SecretValue $Getvalue -ErrorAction SilentlyContinue
    setSecret
    }
    }
    

    Reference: Set-AzureKeyVaultSecret does not recognize vaultName · GitHub