Home > Mobile >  Powershell If Statement not working - Posh-ACME
Powershell If Statement not working - Posh-ACME

Time:08-01

I am struggling to get the last if statement to work. I have a blob storage account which contains the directories mentioned and a certificate. I want to import that certificate to the keyvault.

When I run the pipeline (which contains the below script), it just runs to where I have put the Write-host 'everything..'

Can someone please assist why it won't work, I have tried to separate to 3 if statements, remove the if statement nothing has worked.

param (
        [string] $CertificateNames,
        [string] $KeyVaultResourceId
    )
    
    # Split certificate names by comma or semi-colon
    $certificateName = $CertificateNames.Replace(',', ';') -split ';' | ForEach-Object -Process { $_.Trim() } | Select-Object -First 1
    
    # For wildcard certificates, Posh-ACME replaces * with ! in the directory name
    $certificateName = $certificateName.Replace('*', '!')
    
    # Set working directory
    $workingDirectory = Join-Path -Path "." -ChildPath "pa"
    
    # Set Posh-ACME working directory
    $env:POSHACME_HOME = $workingDirectory
    Import-Module -Name Posh-ACME -Force
    
    # Resolve the details of the certificate
    $currentServerName = ((Get-PAServer).location) -split "/" | Where-Object -FilterScript { $_ } | Select-Object -Skip 1 -First 1
    $currentAccountName = (Get-PAAccount).id
    
    # Determine paths to resources
    $orderDirectoryPath = Join-Path -Path $workingDirectory -ChildPath $currentServerName | Join-Path -ChildPath $currentAccountName | Join-Path -ChildPath $certificateName
    $orderDataPath = Join-Path -Path $orderDirectoryPath -ChildPath "order.json"
    $pfxFilePath = Join-Path -Path $orderDirectoryPath -ChildPath "fullchain.pfx"
    
    Write-Host 'everything works up until here.. then breaks'
    
    # If we have a order and certificate available
    if ((Test-Path -Path $orderDirectoryPath) -and (Test-Path -Path $orderDataPath) -and (Test-Path -Path $pfxFilePath)) {
    
        Write-Host 'check paths are ok'
        
        $pfxPass = (Get-PAOrder $certificateName).PfxPass
    
        # Load PFX
        $certificate = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ArgumentList $pfxFilePath, $pfxPass, 'EphemeralKeySet'
        
        # Get the current certificate from key vault (if any)
        $azureKeyVaultCertificateName = $certificateName.Replace(".", "-").Replace("!", "wildcard")
        $keyVaultResource = Get-AzResource -ResourceId $KeyVaultResourceId
        $azureKeyVaultCertificate = Get-AzKeyVaultCertificate -VaultName $keyVaultResource.Name -Name $azureKeyVaultCertificateName -ErrorAction SilentlyContinue
        
        Write-Host 'check if certificate is in kv'
        
        # If we have a different certificate, import it
        If (-not $azureKeyVaultCertificate -or $azureKeyVaultCertificate.Thumbprint -ne $certificate.Thumbprint) {
            Import-AzKeyVaultCertificate -VaultName $keyVaultResource.Name -Name $azureKeyVaultCertificateName -FilePath $pfxFilePath -Password (ConvertTo-SecureString -String $pfxPass -AsPlainText -Force) | Out-Null
        }
        
        Write-Host 'check if upload is success'
    }

When the pipeline is run, it breaks and there is no errors: see screenshot here

CodePudding user response:

Resolved this, the issue was the file paths didn't exist so the if statement couldn't check against an invalid file path.

As there was no errors, this was a bit harder to find the reason, instead I removed the if statement and added Write-Host "test" to see where things were broken in the code.

  • Related