我正在从以下格式的URL中获取API数据:
{
"printers" : [ {
"name" : "printsrv01\\printer01","status" : "OK","lastPrintJobSeconds" : 6495,"heldJobsCount" : 0,"physicalPrinterId" : "net://192.168.0.51"
},{
"name" : "printsrv01\\printer02","physicalPrinterId" : "net://192.168.0.52"
},{
"name" : "printsrv02\\printer03","physicalPrinterId" : "net://192.168.0.53"
},{
"name" : "printsrv01\\printer04","physicalPrinterId" : "net://192.168.0.54"
},{
"name" : "printsrv02\\printer05","physicalPrinterId" : "net://192.168.0.55"
},{
"name" : "printsrv01\\printer06","lastPrintJobSeconds" : 183162,"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数据源?
编辑:
我尝试使用@TheldesOfMark建议的解决方案:
$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
}
}