azure - 如何在rest api收集Azure Key Vault中的所有 secret 后获取所有 key

标签 azure azure-powershell azure-keyvault azure-rest-api

祝大家有美好的一天! 我有一个 VMware Windows,它在 key 保管库中有权限,我想收集所有 key ,但完成后的下面的代码只有 ID + 属性,不包含 key 值。任何人都可以帮我完成下面的代码以获取所有关键 secret 。

非常感谢您的帮助!

$RresourceUrl = 'dddd.vault.azure.net'

# Compose REST request.
$response = Invoke-WebRequest -Uri 'http://169.254.111.211/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net' -Method GET -Headers @{Metadata="true"}

$OAuth = $response.Content | ConvertFrom-Json


# Check if authentication was successfull.
if ($OAuth.access_token) {
    #Format headers.
    $HeaderParams = @{
            'Content-Type'  = "application\json"

            'Authorization' = "Bearer $($OAuth.access_token)"
    }

    # Create an empty array to store the result.
    $QueryResults = @()
    
    $Uri = "https://$RresourceUrl/secrets?api-version=2016-10-01"
    # Invoke REST method and fetch data until there are no pages left.
    do {
        
        $Results = Invoke-WebRequest -Uri $Uri -Method GET -Headers $HeaderParams | ConvertFrom-Json
        $Results.nextLink
        if ($Results.value) {
            $QueryResults += $Results.value
        }
        else {
            $QueryResults += $Results
        }
        $Uri = $Results.nextLink
    } until (!($Uri))

    # Return the result.
    $QueryResults | Export-Csv -NoTypeInformatio *\Documents\Tesst.csv    
}  
else {
    Write-Error "No Access Token"
}


最佳答案

这是我的最终代码,可能尚未优化,但它有效。

$RresourceUrl = 'devakv01.vault.azure.net'

# Compose REST request.
$response = Invoke-WebRequest -Uri 'http://169.254.111.211/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net' -Method GET -Headers @{Metadata="true"}

$OAuth = $response.Content | ConvertFrom-Json


# Check if authentication was successfull.
if ($OAuth.access_token) {
    #Format headers.
    $HeaderParams = @{
            'Content-Type'  = "application\json"

            'Authorization' = "Bearer $($OAuth.access_token)"
    }

    # Create an empty array to store the result.
    $QueryResults = @()
    
    $Uri = "https://$RresourceUrl/secrets?api-version=2016-10-01"
    # Invoke REST method and fetch data until there are no pages left.
    do {
        
        $Results = Invoke-WebRequest -Uri $Uri -Method GET -Headers $HeaderParams | ConvertFrom-Json
        $Results.nextLink
        if ($Results.value) {
            $QueryResults += $Results.value
        }
        else {
            $QueryResults += $Results
        }
        $Uri = $Results.nextLink
    } until (!($Uri))

    # Return the result.
    $QueryResults    
}  
else {
    Write-Error "No Access Token"
}

# get Key after to have secrets name            
              
$output = ForEach ($nameSecret in $QueryResults.id)
{ 
    $Resultskey = Invoke-WebRequest -Uri $($nameSecret+'?api-version=2016-10-01') -Method GET  -Headers $HeaderParams | ConvertFrom-Json
    $Resultskey 
}
$output  | Export-Csv -NoTypeInformatio *\$RresourceUrl.csv

关于azure - 如何在rest api收集Azure Key Vault中的所有 secret 后获取所有 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73273892/

相关文章:

azure - 在 Bash 中访问传入的 WebHook JSON 负载 - Azure Pipelines

azure - 选择-AzureRm订阅 : Please provide a valid tenant or a valid subscription

用于从 Azure Key Vault 读取 secret 的 Python 脚本

azure - 如何将 KeyVault secret 传递到 Azure Pipelines 中的模板或脚本文件?

python - 如何在 Azure 上创建环境变量以访问 Key Vault

powershell - 在从 VSTS 进行新部署之前删除 Azure 上的文件和文件夹

c# - CloudConfigurationManager.GetSetting 在单元测试中返回 Null

azure - 如何使用 bicep 为我的 azure 函数创建访问策略?

azure - 如何将现有的 Block Blob 转换为 PageBlob

django - 如何使我的 azure django 项目成为 https?