# PowerShell script to upload a PEM file to Azure Key Vault
# Usage:
# pwsh -Command "& './UploadSecretToKeyVault.ps1' -vaultName 'YourKeyVaultName' -secretName 'YourSecretName' -pemFilePath 'path/to/your/private-key.pem'"
# Or from within powershell:
# .\UploadSecretToKeyVault.ps1 -vaultName "YourKeyVaultName" -secretName "YourSecretName" -pemFilePath "path\to\your\private-key.pem"



param (
    [string]$vaultName,
    [string]$secretName,
    [string]$pemFilePath
)

function Upload-SecretToKeyVault {
    param (
        [string]$vaultName,
        [string]$secretName,
        [string]$pemFilePath
    )

    try {
        # Authenticate to Azure
        Write-Host "Authenticating to Azure..."
        Connect-AzAccount

        # Read PEM file content
        Write-Host "Reading PEM file..."
        $pemContent = Get-Content $pemFilePath -Raw

        # Convert to SecureString
        $securePemContent = ConvertTo-SecureString -String $pemContent -AsPlainText -Force

        # Upload the secret to Azure Key Vault
        Write-Host "Uploading secret to Azure Key Vault..."
        Set-AzKeyVaultSecret -VaultName $vaultName -Name $secretName -SecretValue $securePemContent

        Write-Host "Secret uploaded successfully."
    }
    catch {
        Write-Error "An error occurred: $_"
    }
}

# Call the function
Upload-SecretToKeyVault -vaultName $vaultName -secretName $secretName -pemFilePath $pemFilePath
