在 PowerShell 中进行 Http post 调用时,将 JSON 正文中的字段设置为文件中的内容
setting the field in the JSON body to contents in a file while making a Http post call in PowerShell
我正在尝试安装由 digicert CA 颁发的证书。首先,我使用 Powershell 脚本生成 CSR(证书签名请求)。我将生成的 CSR 放在一个 txt 文件中,并删除所有换行符、空格和回车 returns。我正在使用以下脚本来执行此操作:
$CSR > C:\Scripts\CSR.txt
$path = "C:\Scripts\CSR.txt"
(Get-Content $path -Raw).Replace("`r`n","") | Set-Content $path -Force
(Get-Content $path -Raw).Replace(" ","") | Set-Content $path -Force
(Get-Content $path -Raw).Replace("`r`n","") | Set-Content $path -Force -NoNewline
之后,我想对 digiCert 进行 http post 调用以获取证书 ID。为此,我正在使用以下脚本:
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
$body = @"
{
"profile_name": "XXX",
"common_name": "XX",
"additional_dns_names": [
"XX",
"XX"
],
"csr":$CSRWithNoCRLF,
"signature_hash": "sha256",
"validity": {
"months": 36
},
"organization": {
"name": "XXX",
"units": [
]
}
}
"@
$header = @{
"Accept"="application/json"
"KEY"="XXXX"
"Content-Type"="application/json"
}
$response = Invoke-WebRequest -Uri "https://www.digicert.comXXXX" -Method Post -Body $body -Headers $header
$Content = $response.Content
对于JSON正文中的csr字段,我想直接复制C:\Scripts\CSR.txt文件的内容。我尝试了以下方法,但它不起作用。
$CSRWithNoCRLF = Get-Content $path
并使用 $CSRWithNoCRLF 作为 csr 的值。但是 Get-Content 正在添加换行符和回车 returns 并使 CSR 值变形。所以这是行不通的。关于如何解决这个问题的任何想法?
Get-Content is adding line breaks
多种方式,每一种都是完整的解决方案:
$CSRWithNoCRLF = Get-Content $path -Raw # requires PSv3+
Get-Content $path | Out-String
(Get-Content $path) -join "`n" # use "`r`n" for Windows-style line-endings
Edit
检查您发送的内容。试试这个:
[regex]::Escape($Body)
如果您使用 (Get-Content $path) -join "
n"`,那么 CSR 部分将不会有 \r\n 行结尾。但是,您为什么要手工制作 JSON?如果您这样做windows 框,您的 JSON 字符串中会有 Windows 行结尾。
试试这个:
$Body = [pscustomobject]@{
profile_name = "XXX"
common_name = "XX"
additional_dns_names = @(
"XX",
"XX"
)
csr = (Get-Content $Path) -join "`n"
signature_hash = "sha256"
validity = [pscustomobject]@{
months = 36
}
organization = [pscustomobject]@{
name = "XXX"
units = @()
}
} | ConvertTo-Json -Compress
请注意,我使用的是 Compress
。
> [regex]::Escape($Body) -match '\r'
False
试一试。
试试这个:
$CSRWithNoCRLF = (Get-Content $path -Raw).Replace("`r`n","").Replace(" ","")
$body = @"
{
"profile_name": "XXX",
"common_name": "XX",
"additional_dns_names": [
"XX",
"XX"
],
"csr":"$CSRWithNoCRLF",
"signature_hash": "sha256",
"validity": {
"months": 36
},
"organization": {
"name": "XXX",
"units": [
]
}
}
"@
$header = @{
"Accept"="application/json"
"KEY"="XXXX"
"Content-Type"="application/json"
}
$response = Invoke-WebRequest -Uri "https://www.digicert.comXXXX" -Method Post -Body $body -Headers $header
$Content = $response.Content
我看修改后不需要将证书存回文件。
我正在尝试安装由 digicert CA 颁发的证书。首先,我使用 Powershell 脚本生成 CSR(证书签名请求)。我将生成的 CSR 放在一个 txt 文件中,并删除所有换行符、空格和回车 returns。我正在使用以下脚本来执行此操作:
$CSR > C:\Scripts\CSR.txt
$path = "C:\Scripts\CSR.txt"
(Get-Content $path -Raw).Replace("`r`n","") | Set-Content $path -Force
(Get-Content $path -Raw).Replace(" ","") | Set-Content $path -Force
(Get-Content $path -Raw).Replace("`r`n","") | Set-Content $path -Force -NoNewline
之后,我想对 digiCert 进行 http post 调用以获取证书 ID。为此,我正在使用以下脚本:
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
$body = @"
{
"profile_name": "XXX",
"common_name": "XX",
"additional_dns_names": [
"XX",
"XX"
],
"csr":$CSRWithNoCRLF,
"signature_hash": "sha256",
"validity": {
"months": 36
},
"organization": {
"name": "XXX",
"units": [
]
}
}
"@
$header = @{
"Accept"="application/json"
"KEY"="XXXX"
"Content-Type"="application/json"
}
$response = Invoke-WebRequest -Uri "https://www.digicert.comXXXX" -Method Post -Body $body -Headers $header
$Content = $response.Content
对于JSON正文中的csr字段,我想直接复制C:\Scripts\CSR.txt文件的内容。我尝试了以下方法,但它不起作用。
$CSRWithNoCRLF = Get-Content $path
并使用 $CSRWithNoCRLF 作为 csr 的值。但是 Get-Content 正在添加换行符和回车 returns 并使 CSR 值变形。所以这是行不通的。关于如何解决这个问题的任何想法?
Get-Content is adding line breaks
多种方式,每一种都是完整的解决方案:
$CSRWithNoCRLF = Get-Content $path -Raw # requires PSv3+
Get-Content $path | Out-String
(Get-Content $path) -join "`n" # use "`r`n" for Windows-style line-endings
Edit
检查您发送的内容。试试这个:
[regex]::Escape($Body)
如果您使用 (Get-Content $path) -join "
n"`,那么 CSR 部分将不会有 \r\n 行结尾。但是,您为什么要手工制作 JSON?如果您这样做windows 框,您的 JSON 字符串中会有 Windows 行结尾。
试试这个:
$Body = [pscustomobject]@{
profile_name = "XXX"
common_name = "XX"
additional_dns_names = @(
"XX",
"XX"
)
csr = (Get-Content $Path) -join "`n"
signature_hash = "sha256"
validity = [pscustomobject]@{
months = 36
}
organization = [pscustomobject]@{
name = "XXX"
units = @()
}
} | ConvertTo-Json -Compress
请注意,我使用的是 Compress
。
> [regex]::Escape($Body) -match '\r'
False
试一试。
试试这个:
$CSRWithNoCRLF = (Get-Content $path -Raw).Replace("`r`n","").Replace(" ","")
$body = @"
{
"profile_name": "XXX",
"common_name": "XX",
"additional_dns_names": [
"XX",
"XX"
],
"csr":"$CSRWithNoCRLF",
"signature_hash": "sha256",
"validity": {
"months": 36
},
"organization": {
"name": "XXX",
"units": [
]
}
}
"@
$header = @{
"Accept"="application/json"
"KEY"="XXXX"
"Content-Type"="application/json"
}
$response = Invoke-WebRequest -Uri "https://www.digicert.comXXXX" -Method Post -Body $body -Headers $header
$Content = $response.Content
我看修改后不需要将证书存回文件。