无法在 PowerShell 中使用 API "Import Certificate"
Unable to "Import Certificate" using API in PowerShell
我有一个自签名证书,我正在尝试将其导入 Azure Key Vault 证书,但我遇到了问题。我没有使用 Import-AzKeyVaultCertificate
命令,而是使用 API。
我能够通过 API 路线成功地创建 一个密钥,但是对于我来说,我无法导入证书。仅供参考,当我通过 Azure DevOps 管道执行此操作时它也有效,所以我知道服务原则不是问题。
以下是我创建证书的方式(通过本地 PowerShell - 这里的所有示例都是本地的,而不是针对 Azure DevOps):
$certroopath = "C:\cert"
$location = "eastus"
$vmName = "arsmpvm"
$certname = "$vmName"
$certpassword = "P@ssw0rd1234"
$cert = New-SelfSignedCertificate -DnsName "$certname" -CertStoreLocation cert:\LocalMachine\My
$pwd = ConvertTo-SecureString -String $certpassword -Force -AsPlainText
$certwithThumb = "cert:\localMachine\my\"+$cert.Thumbprint
$filepath = "$certroopath$certname.pfx"
Export-PfxCertificate -cert $certwithThumb -FilePath $filepath -Password $pwd
Remove-Item -Path $certwithThumb
以下是我通过 POST 操作导入证书的方式:
$pfxcontent = Get-Content 'C:\cert\arsmpvm.pfx' -Encoding Byte
$base64Stringpfxcontent = [System.Convert]::ToBase64String($pfxcontent)
#The payload
$json_new = '{
"value": "$base64Stringpfxcontent",
"pwd": "$pwd",
"policy": {
"key_props": {
"exportable": true,
"kty": "RSA",
"key_size": 2048,
"reuse_key": false
},
"secret_props": {
"contentType": "application/x-pem-file"
}
}
}'
$header = @{Authorization = "Bearer " + $token.access_token}
Invoke-RestMethod -Method Post -Uri "https://$kvname.vault.azure.net/certificates/$certname/import?api-version=7.0" -Body $json_new -Headers $header -ContentType "application/json"
这是我抛出的错误:
Invoke-RestMethod : {"error":{"code":"BadParameter","message":"The specified PEM X.509 certificate content is in an unexpected format. Please check if certificate is in valid PEM format."}}
即使我将 $base64Stringpfxcontent
(在 $json_new
中)的值替换为它的内容,就像一个 3500 字符的字符串,我也会遇到同样的问题。即使我将 $pwd
更改为其硬编码值,即 P@ssw0rd1234
,我也会抛出相同的错误。
引用自:https://docs.microsoft.com/en-us/rest/api/keyvault/importcertificate/importcertificate
错误截图:
如果在''
中使用$base64Stringpfxcontent
和$pwd
,它们将被识别为字符串而不是变量,并且从doc开始,pwd
是 string
,你不能使用 SecureString
代替它,你需要将 P@ssw0rd1234
直接传递给请求正文。
试试下面的脚本,它在我这边运行良好。
$kvname = "joykeyvault"
$certname = "testcer123"
$pfxcontent = Get-Content 'C:\Users\joyw\Desktop\arsmpvm.pfx' -Encoding Byte
$base64Stringpfxcontent = [System.Convert]::ToBase64String($pfxcontent)
$json_new = @{
value= $base64Stringpfxcontent
pwd= "P@ssw0rd1234"
policy= @{
secret_props= @{
contentType= "application/x-pkcs12"
}
}
}
$json = $json_new | ConvertTo-Json
$header = @{Authorization = "Bearer " + $token.access_token}
Invoke-RestMethod -Method Post -Uri "https://$kvname.vault.azure.net/certificates/$certname/import?api-version=7.0" -Body $json -Headers $header -ContentType "application/json"
登录门户:
我有一个自签名证书,我正在尝试将其导入 Azure Key Vault 证书,但我遇到了问题。我没有使用 Import-AzKeyVaultCertificate
命令,而是使用 API。
我能够通过 API 路线成功地创建 一个密钥,但是对于我来说,我无法导入证书。仅供参考,当我通过 Azure DevOps 管道执行此操作时它也有效,所以我知道服务原则不是问题。
以下是我创建证书的方式(通过本地 PowerShell - 这里的所有示例都是本地的,而不是针对 Azure DevOps):
$certroopath = "C:\cert"
$location = "eastus"
$vmName = "arsmpvm"
$certname = "$vmName"
$certpassword = "P@ssw0rd1234"
$cert = New-SelfSignedCertificate -DnsName "$certname" -CertStoreLocation cert:\LocalMachine\My
$pwd = ConvertTo-SecureString -String $certpassword -Force -AsPlainText
$certwithThumb = "cert:\localMachine\my\"+$cert.Thumbprint
$filepath = "$certroopath$certname.pfx"
Export-PfxCertificate -cert $certwithThumb -FilePath $filepath -Password $pwd
Remove-Item -Path $certwithThumb
以下是我通过 POST 操作导入证书的方式:
$pfxcontent = Get-Content 'C:\cert\arsmpvm.pfx' -Encoding Byte
$base64Stringpfxcontent = [System.Convert]::ToBase64String($pfxcontent)
#The payload
$json_new = '{
"value": "$base64Stringpfxcontent",
"pwd": "$pwd",
"policy": {
"key_props": {
"exportable": true,
"kty": "RSA",
"key_size": 2048,
"reuse_key": false
},
"secret_props": {
"contentType": "application/x-pem-file"
}
}
}'
$header = @{Authorization = "Bearer " + $token.access_token}
Invoke-RestMethod -Method Post -Uri "https://$kvname.vault.azure.net/certificates/$certname/import?api-version=7.0" -Body $json_new -Headers $header -ContentType "application/json"
这是我抛出的错误:
Invoke-RestMethod : {"error":{"code":"BadParameter","message":"The specified PEM X.509 certificate content is in an unexpected format. Please check if certificate is in valid PEM format."}}
即使我将 $base64Stringpfxcontent
(在 $json_new
中)的值替换为它的内容,就像一个 3500 字符的字符串,我也会遇到同样的问题。即使我将 $pwd
更改为其硬编码值,即 P@ssw0rd1234
,我也会抛出相同的错误。
引用自:https://docs.microsoft.com/en-us/rest/api/keyvault/importcertificate/importcertificate
错误截图:
如果在''
中使用$base64Stringpfxcontent
和$pwd
,它们将被识别为字符串而不是变量,并且从doc开始,pwd
是 string
,你不能使用 SecureString
代替它,你需要将 P@ssw0rd1234
直接传递给请求正文。
试试下面的脚本,它在我这边运行良好。
$kvname = "joykeyvault"
$certname = "testcer123"
$pfxcontent = Get-Content 'C:\Users\joyw\Desktop\arsmpvm.pfx' -Encoding Byte
$base64Stringpfxcontent = [System.Convert]::ToBase64String($pfxcontent)
$json_new = @{
value= $base64Stringpfxcontent
pwd= "P@ssw0rd1234"
policy= @{
secret_props= @{
contentType= "application/x-pkcs12"
}
}
}
$json = $json_new | ConvertTo-Json
$header = @{Authorization = "Bearer " + $token.access_token}
Invoke-RestMethod -Method Post -Uri "https://$kvname.vault.azure.net/certificates/$certname/import?api-version=7.0" -Body $json -Headers $header -ContentType "application/json"
登录门户: