Private/_ReplaceWithPSCred.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function _ReplaceWithPSCred {
    [cmdletbinding()]
    param(
        [string]$SecretName,
        $SecretObject,
        [string]$VaultName = $Script:KeyVault.VaultName
    )
    $Properties = $SecretObject.PSObject.Properties | Where-Object {$_.IsSettable -and $_.Value -eq $SecretName}
    if (!$Properties) {
        $NestedProperties = $SecretObject.PSObject.Properties | Where-Object {$_.TypeNameOfValue -eq 'System.Management.Automation.PSCustomObject'}
        if (!$NestedProperties) {
            Write-Error "Unable to find property with value, $($SecretName)"
        }
        foreach($NestedProperty in $NestedProperties){
            _ReplaceWithPSCred -SecretName $SecretName -SecretObject $NestedProperty.Value -VaultName $VaultName
        }
    }
    foreach($Property in $Properties){
        $Property.Value = Get-AzKeyVaultSecret -SecretName $SecretName -Auto -Expand
    }
}