我试图制作一个PowerShell脚本,从中获取一个json文件并放入另一个。获得部分已完成。我有一个像这样的代码:
*$sourceFolder = 'my folder'
$token = 'my token'
$encodedToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($token))
$authValue = "AfasToken $encodedToken"
$Headers = @{
Authorization = $authValue
}
$url = "https://my.url.nl/Restservices/budgethouders?skip=0&take=1000"
$file = 'my file'
Invoke-WebRequest -Uri $url -OutFile $file -Headers $Headers*
这有效,我得到一个文件。像这样:
{ "skip" : 0,
"take" : 1000,
"rows" : [
{
"CmId": "1",
"CrId": 100,
"CrDs": "Directie",
"U266FAB0A4D908F09592CD7A18E24B1DD": "506",
"U624D2D6B4FFA8407904125BBCB35A80E": "11186",
"U7A831591420B12B7871EFF8C4AB4E554": "11186"
},
{
"CmId": "1",
"CrId": 101,
"CrDs": "Dimensie",
"U266FAB0A4D908F09592CD7A18E24B1DD": "506",
"U624D2D6B4FFA8407904125BBCB35A80E": "11186",
"U7A831591420B12B7871EFF8C4AB4E554": "11186"
}
]}
现在我想使用我必须将该文件返回给服务的代码,该代码从我刚刚保存的文件中获取信息。我知道我如何制作代码manualy。但我似乎无法为每个州的权利而努力。我自己的代码看起来像这样:
$sourceFolder = 'my folder'
$token = 'my token'
$encodedToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($token))
$authValue = "AfasToken $encodedToken"
$Headers = @{
Authorization = $authValue
}
$url = "https://my.url.nl/Restservices/connectors/CostCentre"
$file = '{
"HrCostCentre": {
"Element": {
"Fields": {
"CmId": "1",
"CrId": "100",
"CrDs": "Directie",
"U266FAB0A4D908F09592CD7A18E24B1DD": "506",
"U624D2D6B4FFA8407904125BBCB35A80E": "11186",
"U7A831591420B12B7871EFF8C4AB4E554": "11186"
}
}
}
}'
Invoke-WebRequest -Uri $url -ContentType application/json -Method PUT -Headers $Headers -Body $file
$url = "https://my.url.nl/Restservices/connectors/HrCostCentre"
$file = '{
"HrCostCentre": {
"Element": {
"Fields": {
"CmId": "1",
"CrId": 101,
"CrDs": "Dimensie",
"U266FAB0A4D908F09592CD7A18E24B1DD": "506",
"U624D2D6B4FFA8407904125BBCB35A80E": "11186",
"U7A831591420B12B7871EFF8C4AB4E554": "11186"
}
}
}
}'
Invoke-WebRequest -Uri $url -ContentType application/json -Method PUT -Headers $Headers -Body $file
这有效,但我会在更新中使用我的文件中的信息,但不知道如何正确获取代码。我真的是一个初学者,所以我很高兴我已经走到了这一步。我会帮助我获得帮助。
答案 0 :(得分:0)
这适合您:
$budgethouders = ConvertFrom-JSON '
{ "skip" : 0,
"take" : 1000,
"rows" : [
{
"CmId": "1",
"CrId": 100,
"CrDs": "Directie",
"U266FAB0A4D908F09592CD7A18E24B1DD": "506",
"U624D2D6B4FFA8407904125BBCB35A80E": "11186",
"U7A831591420B12B7871EFF8C4AB4E554": "11186"
},
{
"CmId": "1",
"CrId": 101,
"CrDs": "Dimensie",
"U266FAB0A4D908F09592CD7A18E24B1DD": "506",
"U624D2D6B4FFA8407904125BBCB35A80E": "11186",
"U7A831591420B12B7871EFF8C4AB4E554": "11186"
}
]}
'
$file = ConvertTo-JSON @{
"HrCostCentre" = @{
"Element" = @{
"Fields" = New-Object PSObject $budgethouders.rows[0] # or .rows[1]
}
}
}
但它引出了一个新问题:结果中的 "value": ""
来自哪里?(我还没有找到一个简单的逻辑解决方案压制这个):
{
"HrCostCentre": {
"Element": {
"Fields": {
"value": "",
"CmId": "1",
"CrId": 100,
"CrDs": "Directie",
"U266FAB0A4D908F09592CD7A18E24B1DD": "506",
"U624D2D6B4FFA8407904125BBCB35A80E": "11186",
"U7A831591420B12B7871EFF8C4AB4E554": "11186"
}
}
}
}
答案 1 :(得分:0)
以某种方式对你的评论我发现了。不是最bueatifull代码,但它的工作原理:))
我获取文件并将其编辑为可用的脚本,感谢您的输入。
$sourceFolder = 'C:\my folder\'
$token = 'my token'
$encodedToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($token))
$authValue = "AfasToken $encodedToken"
$Headers = @{
Authorization = $authValue
}
$url = "https://My.url.nl/Restservices/connectors/budgethouders?skip=0&take=1"
$file = $Sourcefolder + 'Budgethouders.json'
Invoke-WebRequest -Uri $url -OutFile $file -Headers $Headers
$Budgethouders = $file
(Get-Content $Budgethouders| Select-Object -Skip 3) |Select -SkipLast 1| Set-Content $Budgethouders
$Orgineel1 = "{"
$Orgineel2 = "},"
$Dollar = "$"
$Url = "url = "
$link = [char]34 + "https://my.url.nl/Restservices/connectors/HrCostCentre"+ [char]34
$enter = "
"
$F = "File ="
$haakjeopen = "'{"
$tekst1 = [char]34 + "HrCostCentre" + [char]34 +": {"
$tekst2 = [char]34 + "Element" +[char]34 + ": {"
$tekst3 = [char]34 + "Fields" + [char]34 + ": {"
$Sluiten = "}
}
}
}'
"
$sluiten2 ="}
}
}'
"
$invoke1 = "Invoke-WebRequest -Uri " + $dollar + "url -ContentType application/json -Method PUT -Headers "+ $dollar +"Headers -Body " + $dollar + "file"
$vervang1 = $Dollar+ $Url+ $link + $enter + $dollar + $F + $haakjeopen + $enter + $tekst1 + $enter + $tekst2 + $enter + $tekst3
$vervang2 = $Sluiten + $enter + $invoke1
$invoketotaal = $invoke1
(Get-Content $Budgethouders) -replace $Orgineel1,$vervang1 | out-file $Budgethouders
(Get-Content $Budgethouders) -replace $Orgineel2,$vervang2| out-file $Budgethouders
Add-Content $Budgethouders $sluiten2
Add-Content $Budgethouders $enter
Add-Content $Budgethouders $invoketotaal
$Jsonfile = $file
$tekstfile = $sourcefolder + "Temp text file.txt"
$tempfile = $sourcefolder +"auto.txt"
$scriptfile = $sourcefolder + "script.ps1"
Add-Content -Path $tempfile -Value (Get-Content $tekstfile)
Add-Content -Path $tempfile -Value (Get-Content $Jsonfile)
(Get-Content $tempfile) | Out-File $scriptfile
Invoke-Expression $Scriptfile
Remove-Item $Scriptfile
Remove-item $tempfile
remove-item $jsonfile