ConvertFrom-JSON 到 POST 的变量进一步到 PowerBI
ConvertFrom-JSON to variables for POST further on to PowerBI
我正在从 URL 中获取 API 数据,格式如下:
{
"printers" : [ {
"name" : "printsrv01\printer01",
"status" : "OK",
"lastPrintJobSeconds" : 6495,
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.51"
}, {
"name" : "printsrv01\printer02",
"status" : "OK",
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.52"
}, {
"name" : "printsrv02\printer03",
"status" : "OK",
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.53"
}, {
"name" : "printsrv01\printer04",
"status" : "OK",
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.54"
}, {
"name" : "printsrv02\printer05",
"status" : "OK",
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.55"
}, {
"name" : "printsrv01\printer06",
"status" : "OK",
"lastPrintJobSeconds" : 183162,
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.56"
} ],
"heldJobCountTotal" : 0,
"heldJobsCountMax" : 0
}
我已经成功地使用以下代码从 JSON 转换数据:
while ($true) {
$request = "http://serveradress:80/api/health/printers?Authorization=GHSKAskas0a5as5FFDA22asD"
Invoke-WebRequest $request |
ConvertFrom-Json |
select -Expand printers |
}
我正在使用 PowerBI 上传数据。
如果我的值存储在变量中,则可以使用以下代码完成此操作。
$payload = @{
"name" = $name
"status" = $status
"lastPrintJobSeconds" = $lastPrintJobSeconds
"heldJobsCount" = $heldJobsCount
"physicalPrinterId" = $physicalPrinterId
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))
如何将每个打印机信息放入每个变量中以将其发送到用于显示的 PowerBI 数据源?
编辑:
我尝试使用 :
建议的解决方案
$endpoint = "https://api.powerbi.com/beta/aaa555v22-66888-ccc6-95aa-0dfb5dc31330/datasets/aaa555a666-6547-4250-950c-25s5sd5s5s5s5/rows?key=65asd55asd45asd45asd5as5d4!%54dsa5f45fc4zd56fc4"
while ($true) {
$request = "http://server:80/api/health/printers?Authorization=asd54asas5dSSd55sd4as65d4ASDA"
Invoke-WebRequest $request |
ConvertFrom-Json |
select -Expand printers |
foreach ($printer in $printers) {
$payload = @{
"name" = $printer.name
"status" = $printer.status
"lastPrintJobSeconds" = $printer.lastPrintJobSeconds
"heldJobsCount" = $printer.heldJobsCount
"physicalPrinterId" = $printer.physicalPrinterId
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))
Write-Host "Name: " $printer.name " Status: " $printer.status " lastPrintJobSeconds: " $printer.lastPrintJobSeconds " heldJobsCount: " $printer.heldJobsCount " physicalPrinterId: " $printer.physicalPrinterId
}
sleep 2
}
}
但它会产生以下错误:
At line:10 char:22
+ foreach ($printer in $printers) {
+ ~~
Unexpected token 'in' in expression or statement.
At line:10 char:21
+ foreach ($printer in $printers) {
+ ~
Missing closing ')' in expression.
At line:4 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
At line:10 char:34
+ foreach ($printer in $printers) {
+ ~
Unexpected token ')' in expression or statement.
At line:22 char:5
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
似乎 foreach
循环是问题所在?
如果我不从 JSON 转换,它不会给出任何错误,但如果我不先转换就尝试,我不会得到任何数据。
编辑 2
将 Invoke-WebRequest 更改为以下内容,Write Output 可以正确打印数据。
但是我在 Invoke-RestMethod:
上出错
$printers = Invoke-WebRequest 'http://server:80/api/health/printers?Authorization=asd54asas5dSSd55sd4as65d4ASDA' | ConvertFrom-Json | select -Expand printers
错误码如下:
Invoke-RestMethod : {"error":{"message":"The request was blocked by KeyBlocker "}}
At line:14 char:9
+ Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
编辑 3
$endpoint = "PowerBI_URL"
Invoke-WebRequest 'API_URL' |
ConvertFrom-Json |
Select-Object -Expand printers |
ForEach-Object {
Invoke-RestMethod -Method Post -Uri "$endpoint" (ConvertTo-Json @($_))
}
错误代码:
Invoke-RestMethod : A positional parameter cannot be found that accepts argument '[
{
"name": "printsrv01\printer01",
"status": "OK",
"heldJobsCount": 0,
"physicalPrinterId": "net://192.168.0.51"
}
]'.
At line:6 char:9
+ Invoke-RestMethod -Method Post -Uri "$endpoint" (ConvertTo-Json @($_))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
似乎并未打印所有值。少了一些。
编辑 4 - 工作脚本
$endpoint = "https://api.powerbi.com/beta/nnf5d2dsf1-4588-88gh-b6a4-0dfb5dc31330/datasets/55562ee92-9c7d-8850-854g-1a39asdd8a4b/rows?key=ljrethjkreRDFG545REWFGGDF0DFGDGF"
while ($true) {
$printers = Invoke-WebRequest 'http://server/api/health/printers?Authorization=sdfkjdsf044s21sDSFsdf54sdf' | ConvertFrom-Json | select -Expand printers
foreach ($printer in $printers) {
$payload = @{
"name" = $printer.name
"status" = $printer.status
"lastPrintJobSeconds" = $printer.lastPrintJobSeconds
"heldJobsCount" = $printer.heldJobsCount
"physicalPrinterId" = $printer.physicalPrinterId
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))
#Write-Host "Name: " $printer.name " Status: " $printer.status " lastPrintJobSeconds: " $printer.lastPrintJobSeconds " heldJobsCount: " $printer.heldJobsCount " physicalPrinterId: " $printer.physicalPrinterId
sleep 1
}
}
AFAICS 您不需要将各个值提取到不同的变量中。只需将 ForEach-Object
添加到管道中,将当前对象转换为 JSON 并使用该对象调用 POST 请求。
Invoke-WebRequest $request |
ConvertFrom-Json |
Select-Object -Expand printers |
ForEach-Object {
$data = ConvertTo-Json $_
Invoke-RestMethod -Method Post -Uri $endpoint -Body $data
}
Ansgar 的回答是我认为的最佳答案。但如果出于某种原因,您需要将此信息提供给其他 PS 进程。您可以通过循环遍历您构建的数组并分配变量来转换当前脚本中的变量。
ForEach($printer in $printers){
$payload = @{
"name" = $printer.name
"status" = $printer.status
"lastPrintJobSeconds" = $printer.lastPrintJobSeconds
"heldJobsCount" = $printer.heldJobsCount
"physicalPrinterId" = $printer.physicalPrinterId
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))
}
我正在从 URL 中获取 API 数据,格式如下:
{
"printers" : [ {
"name" : "printsrv01\printer01",
"status" : "OK",
"lastPrintJobSeconds" : 6495,
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.51"
}, {
"name" : "printsrv01\printer02",
"status" : "OK",
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.52"
}, {
"name" : "printsrv02\printer03",
"status" : "OK",
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.53"
}, {
"name" : "printsrv01\printer04",
"status" : "OK",
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.54"
}, {
"name" : "printsrv02\printer05",
"status" : "OK",
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.55"
}, {
"name" : "printsrv01\printer06",
"status" : "OK",
"lastPrintJobSeconds" : 183162,
"heldJobsCount" : 0,
"physicalPrinterId" : "net://192.168.0.56"
} ],
"heldJobCountTotal" : 0,
"heldJobsCountMax" : 0
}
我已经成功地使用以下代码从 JSON 转换数据:
while ($true) {
$request = "http://serveradress:80/api/health/printers?Authorization=GHSKAskas0a5as5FFDA22asD"
Invoke-WebRequest $request |
ConvertFrom-Json |
select -Expand printers |
}
我正在使用 PowerBI 上传数据。 如果我的值存储在变量中,则可以使用以下代码完成此操作。
$payload = @{
"name" = $name
"status" = $status
"lastPrintJobSeconds" = $lastPrintJobSeconds
"heldJobsCount" = $heldJobsCount
"physicalPrinterId" = $physicalPrinterId
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))
如何将每个打印机信息放入每个变量中以将其发送到用于显示的 PowerBI 数据源?
编辑:
我尝试使用
$endpoint = "https://api.powerbi.com/beta/aaa555v22-66888-ccc6-95aa-0dfb5dc31330/datasets/aaa555a666-6547-4250-950c-25s5sd5s5s5s5/rows?key=65asd55asd45asd45asd5as5d4!%54dsa5f45fc4zd56fc4"
while ($true) {
$request = "http://server:80/api/health/printers?Authorization=asd54asas5dSSd55sd4as65d4ASDA"
Invoke-WebRequest $request |
ConvertFrom-Json |
select -Expand printers |
foreach ($printer in $printers) {
$payload = @{
"name" = $printer.name
"status" = $printer.status
"lastPrintJobSeconds" = $printer.lastPrintJobSeconds
"heldJobsCount" = $printer.heldJobsCount
"physicalPrinterId" = $printer.physicalPrinterId
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))
Write-Host "Name: " $printer.name " Status: " $printer.status " lastPrintJobSeconds: " $printer.lastPrintJobSeconds " heldJobsCount: " $printer.heldJobsCount " physicalPrinterId: " $printer.physicalPrinterId
}
sleep 2
}
}
但它会产生以下错误:
At line:10 char:22 + foreach ($printer in $printers) { + ~~ Unexpected token 'in' in expression or statement. At line:10 char:21 + foreach ($printer in $printers) { + ~ Missing closing ')' in expression. At line:4 char:1 + { + ~ Missing closing '}' in statement block or type definition. At line:10 char:34 + foreach ($printer in $printers) { + ~ Unexpected token ')' in expression or statement. At line:22 char:5 + } + ~ Unexpected token '}' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken
似乎 foreach
循环是问题所在?
如果我不从 JSON 转换,它不会给出任何错误,但如果我不先转换就尝试,我不会得到任何数据。
编辑 2
将 Invoke-WebRequest 更改为以下内容,Write Output 可以正确打印数据。 但是我在 Invoke-RestMethod:
上出错$printers = Invoke-WebRequest 'http://server:80/api/health/printers?Authorization=asd54asas5dSSd55sd4as65d4ASDA' | ConvertFrom-Json | select -Expand printers
错误码如下:
Invoke-RestMethod : {"error":{"message":"The request was blocked by KeyBlocker "}}
At line:14 char:9
+ Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @( ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
编辑 3
$endpoint = "PowerBI_URL"
Invoke-WebRequest 'API_URL' |
ConvertFrom-Json |
Select-Object -Expand printers |
ForEach-Object {
Invoke-RestMethod -Method Post -Uri "$endpoint" (ConvertTo-Json @($_))
}
错误代码:
Invoke-RestMethod : A positional parameter cannot be found that accepts argument '[
{
"name": "printsrv01\printer01",
"status": "OK",
"heldJobsCount": 0,
"physicalPrinterId": "net://192.168.0.51"
}
]'.
At line:6 char:9
+ Invoke-RestMethod -Method Post -Uri "$endpoint" (ConvertTo-Json @($_))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-RestMethod], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
似乎并未打印所有值。少了一些。
编辑 4 - 工作脚本
$endpoint = "https://api.powerbi.com/beta/nnf5d2dsf1-4588-88gh-b6a4-0dfb5dc31330/datasets/55562ee92-9c7d-8850-854g-1a39asdd8a4b/rows?key=ljrethjkreRDFG545REWFGGDF0DFGDGF"
while ($true) {
$printers = Invoke-WebRequest 'http://server/api/health/printers?Authorization=sdfkjdsf044s21sDSFsdf54sdf' | ConvertFrom-Json | select -Expand printers
foreach ($printer in $printers) {
$payload = @{
"name" = $printer.name
"status" = $printer.status
"lastPrintJobSeconds" = $printer.lastPrintJobSeconds
"heldJobsCount" = $printer.heldJobsCount
"physicalPrinterId" = $printer.physicalPrinterId
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))
#Write-Host "Name: " $printer.name " Status: " $printer.status " lastPrintJobSeconds: " $printer.lastPrintJobSeconds " heldJobsCount: " $printer.heldJobsCount " physicalPrinterId: " $printer.physicalPrinterId
sleep 1
}
}
AFAICS 您不需要将各个值提取到不同的变量中。只需将 ForEach-Object
添加到管道中,将当前对象转换为 JSON 并使用该对象调用 POST 请求。
Invoke-WebRequest $request |
ConvertFrom-Json |
Select-Object -Expand printers |
ForEach-Object {
$data = ConvertTo-Json $_
Invoke-RestMethod -Method Post -Uri $endpoint -Body $data
}
Ansgar 的回答是我认为的最佳答案。但如果出于某种原因,您需要将此信息提供给其他 PS 进程。您可以通过循环遍历您构建的数组并分配变量来转换当前脚本中的变量。
ForEach($printer in $printers){
$payload = @{
"name" = $printer.name
"status" = $printer.status
"lastPrintJobSeconds" = $printer.lastPrintJobSeconds
"heldJobsCount" = $printer.heldJobsCount
"physicalPrinterId" = $printer.physicalPrinterId
}
Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))
}