标签: invoke-webrequest

Invoke-WebRequest 和 Invoke-RestMethod 的不同结果

我正在尝试调用 Azure Rest API 并获取 DevTestLabs 的时间表。我尝试了 Invoke-RestMethod,但它没有给出“dailyRecurrence”键的。但 Invoke-WebRequest 可以。

原因是什么?

网址

$url = "https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourseGroup}/providers/Microsoft.DevTestLab/labs/{LabName}/schedules/LabVmsShutdown?api-version=2018-10-15-preview"
Run Code Online (Sandbox Code Playgroud)

带有 $expand 的 URL

$url = "https://management.azure.com/subscriptions/{subscriptionID}/resourceGroups/{resourseGroup}/providers/Microsoft.DevTestLab/labs/{LabName}/schedules/LabVmsShutdown?$expand=properties(dailyRecurrence)&api-version=2018-10-15-preview"
Run Code Online (Sandbox Code Playgroud)

调用 Invoke-RestMethod

$output = Invoke-RestMethod -Uri $url -Method "GET" -ContentType "application/json" -Headers $authHeaders

properties : @{status=Enabled; taskType=LabVmsShutdownTask; dailyRecurrence=; timeZoneId=AUS Eastern Standard Time;
         notificationSettings=; createdDate=26/03/2019 4:38:18 PM; provisioningState=Succeeded;
         uniqueIdentifier=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}
Run Code Online (Sandbox Code Playgroud)

调用Invoke-WebRequest

$output = Invoke-WebRequest -Uri $url -Method "GET" -Headers $authHeaders

Content           : {"properties":{"status":"Enabled","taskType":"LabVmsShutdownTask","dailyRecurrence":{"time":"1900"}
                ,"timeZoneId":"AUS Eastern Standard Time","notificationSettings":{"status":"Disabled","timeInMinute
                s":30},"createdDate":"2019-03-26T03:38:18.0726376+00:00","provisioningState":"Succeeded","uniqueIde
                ntifier":"XXXXXXXXXXXXXXXXXXXXXXXXX"},"id":"/subscriptions/XXXXXXXXXXXXXXXXXXX/resourcegroups/XXXXXXXXXXXXX/providers/microsoft.devtestlab/labs/XXXXXXXX/schedules/labvmsshutdown","name":"LabVmsShutdown","type":"microsoft.devtestlab/labs/schedules","location":"australiasoutheast"}
Run Code Online (Sandbox Code Playgroud)

powershell invoke-webrequest azure-rest-api invoke-restmethod

8
推荐指数
1
解决办法
5089
查看次数

Invoke-RestMethod 挂在长时间运行的端点上

我们Invoke-RestMethod在 PowerShell 脚本中使用来调用GET具有可变长度运行时的方法端点。有些电话可能会在几秒钟后返回,有些可能需要长达 20 分钟。我们通过-TimeoutSec参数设置了 50 分钟的通话超时。

只需几秒钟的调用即可正常返回并输出预期的响应。更长的调用(例如 5 分钟)永远不会返回并且Invoke-RestMethod命令用完整个 50 分钟超时,尽管我们在 Web 服务器日志中确认服务器早已返回200 OK.

try 
{
    $Url = "https://example.com/task"   # GET
    $Timeout = 3000                     # 50 minute timout
    $Response = Invoke-RestMethod $Url -TimeoutSec $Timeout
        
    Write-Host $Response
}
catch 
{
    Write-Host $_.Exception
}
Run Code Online (Sandbox Code Playgroud)

端点上没有身份验证。PowerShell 版本为 7。该脚本在托管被调用的 Web 服务器的同一台机器上运行。

这是Invoke-RestMethod我们不知道的配置问题吗?我们在Invoke-WebRequest使用基本相同的脚本时遇到了类似的问题。

powershell invoke-webrequest invoke-restmethod

7
推荐指数
1
解决办法
208
查看次数

如何使用包含文件数据的 JSON 正文发送 Invoke-WebRequest

问题:
如何将文件内容放入 Invoke-WebRequest 的 JSON 正文中,而不包含不需要的文件元数据?

我的目标是发送一个 HTTP 请求,如下所示:

Invoke-WebRequest -Uri http://localhost:4321/updatefile `
    -ContentType 'application/json' `
    -Method POST `
    -Body $Body
Run Code Online (Sandbox Code Playgroud)

在哪里:

PS C:\Users\User1234> $Body = ConvertTo-Json @(
    @{filename='file1.txt';filecontent=$file1},
    @{filename='file2.txt';filecontent=$file2}
)

PS C:\Users\User1234> $file1 = Get-Content "C:\path\to\file1.txt"
PS C:\Users\User1234> $file2 = Get-Content "C:\path\to\file2.txt"
Run Code Online (Sandbox Code Playgroud)

当我打印变量时:

PS C:\Users\User1234> echo $file1
aaaaa
PS C:\Users\User1234> echo $file2
bbbbb
Run Code Online (Sandbox Code Playgroud)

...它按照我的预期打印文件的内容。
但是打印文件内容显示$Body了更多我不需要的信息:

PS C:\Users\User1234> echo $Body
{
    "filename":  "file1.txt",
    "filecontent":  {
                        "value":  "aaaaa",
                        "PSPath":  "C:\\path\\to\\file1.txt",
                        "PSParentPath":  "C:\\path\\to",
                        "PSChildName":  "file1.txt",
                        "PSDrive":  {
                                        "CurrentLocation":  "Users\\User1234",
                                        "Name": …
Run Code Online (Sandbox Code Playgroud)

powershell invoke-webrequest

6
推荐指数
1
解决办法
1万
查看次数

curl --data-binary 在 PowerShell 中

如果我有一个curl命令,例如:

curl <url> \
  -H 'Content-Type: application/json' \
  -H 'API-Key: <key>' \
  --data-binary '{"blah":"blah {\n  blah2(accountId: id, integrations: {int1: {Vms: [{Id: id2, resourceGroups: [\"test1\", \"test2\", \"test3\"]}]}}) {\n    integrations {\n      id\n      name\n      service {\n        id\n        slug\n      }\n    }\n  }\n}\n", "variables":""}'
Run Code Online (Sandbox Code Playgroud)

--data-binary在 Powershell 中相当于什么?一些答案说只是 running curl.exe,其他人提到要更改内容类型。不过,这确实可以作为 shell 脚本正常工作。只是想知道是否可以将其转换为Invoke-WebRequest在 Powershell 中使用。

powershell curl invoke-webrequest

5
推荐指数
1
解决办法
2463
查看次数

如何从 PowerShell 中的请求中获取 StatusCode

我需要使用 Power Shell 从请求中获取成功和/或错误状态代码。我总是得到一个空白状态。

我尝试过 Invoke-WebRequest 和 Invoke-RestMethod。我已成功通话,但找不到获取状态代码的方法。

这里是如何写的:

$resource = "some url"
$Logfile = "C:/path/log.log"

function LogWrite
{
    Param([string]$logstring)

    Add-content $logfile -value $logstring
}

Try
{
    $Response = Invoke-WebRequest -Method Post -Uri $resource 
    Write-Output("Success.")
    LogWrite $Date
    LogWrite SuccessOnCall
    LogWrite  $Response.StatusCode
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    Write-Output($ErrorMessage)
    $FailedItem = $_.Exception
    Write-Output($FailedItem)
    LogWrite $Date
    LogWrite ErrorOnCall
    LogWrite $ErrorMessage
    Break
}
Run Code Online (Sandbox Code Playgroud)

我也试过:

LogWrite "StatusCode:" $Response.Exception.Response.StatusCode.value__ 

Run Code Online (Sandbox Code Playgroud)

我使用了这个问题(和其他链接):Invoke-Restmethod: how to get the return code?

试图解决这个问题,我的日志确实写了“SuccessOnCall”,但 StatusCode 为空。

谢谢你。

rest powershell logging invoke-webrequest

5
推荐指数
1
解决办法
9530
查看次数

我可以通过 Windows 10 命令行将文件上传到 onedrive 吗?

我需要通过命令行将文件上传到 OneDrive。这将通过分发给最终用户的批处理文件来完成。

\n

通过在 Stack Overflow 上搜索,我发现了类似这样的问题,其中提到您需要使用 Azure 注册应用程序并创建应用程序密码。我没有在我工作的组织中执行此操作所需的权限,也无法执行任何需要管理员帐户的操作。所以我无法安装任何软件 - 我必须使用 Windows 10 附带的软件。我也无法使用 VBA,因为它被阻止了。

\n

我已经成功地从 OneDrive 下载文件,没有任何类似的东西,使用此处描述的过程:

\n
\n
    \n
  • 在任一浏览器中打开 URL。
  • \n
  • 使用 Ctrl+Shift+I 打开开发人员选项。
  • \n
  • 转到网络选项卡。
  • \n
  • 现在点击下载。不需要保存文件\xe2\x80\x99。我们只需要浏览器从服务器请求文件时的网络活动。
  • \n
  • 将出现一个新条目,类似于 \xe2\x80\x9cdownload.aspx?\xe2\x80\xa6\xe2\x80\x9d。
  • \n
  • 右键单击该 和Copy \xe2\x86\x92 Copy as cURL
  • \n
  • 将复制的内容直接粘贴到终端中,并追加 \xe2\x80\x98--output file.extension\xe2\x80\x99 将内容保存在 file.extension 中,因为\n终端无法显示二进制文件数据。
  • \n
\n

例子:

\n
curl https://xyz.sharepoint.com/personal/someting/_layouts/15/download.aspx?UniqueId=cefb6082%2D696e%2D4f23%2D8c7a%2\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa6。一些长文本 \xe2\x80\xa6.\ncCtHR3NuTy82bWFtN1JBRXNlV2ZmekZOdWp3cFRsNTdJdjE2c2syZmxQamhGWnMwdkFBeXZlNWx2UkxDTkJic2hycGNGazVSTnJGUnY1Y1d0WjF5SDJMWHBqTjRmcUNUU WJxVnZYb1JjRG1WbEtjK0VIVWx2clBDQWNyZldid1R3PT08L1NQPg==;\ncucg=1\xe2\x80\x99 --压缩--输出文件.扩展名

\n
\n

在浏览器上单击“上传”后,我尝试执行类似的操作,但在尝试过滤请求时没有找到任何有用的东西。

\n

我找到了 两个问题,但没有上传键盘快捷键,AFAICT。此外,最终用户还将文件上传到我从 OneDrive 与他们共享的文件夹中。将 Chrome 或 Edge 作为最小化窗口打开很好,但我不能只是将一个窗口推到他们面前,自动点击一些东西 …

powershell curl file-upload onedrive invoke-webrequest

5
推荐指数
1
解决办法
8859
查看次数

在没有 OutFile 的情况下调用 WebRequest?

我在 Powershell 中使用 Invoke-WebRequest 下载文件而不使用-OutFile参数,并且从文档here 中,该文件应该已经在我所在的目录中结束。但是,什么也没有。响应正常,没有显示错误。

那个文件可能发生了什么?我是否误解了 Invoke-WebRequest 在没有 Out 参数的情况下应该如何工作?

谢谢!

注意:我知道我可以使用参数轻松下载文件,但它非常大,我想确保它不会在我不需要的地方堵塞磁盘空间

powershell default file-location invoke-webrequest

4
推荐指数
2
解决办法
3850
查看次数

Invoke-WebRequest 冻结/挂起

为什么 cmdlet“Invoke-WebRequest”在某些 URL 上冻结/挂起?任何可能的解决方法?我想访问给定网页的“跨度”对象,如果它没有像那样挂起,这个 cmdlet 将非常有用。

例如,这挂起:

Invoke-WebRequest -Uri "https://cloud.google.com/chrome-enterprise/browser/download/"
Run Code Online (Sandbox Code Playgroud)

这不会:

Invoke-WebRequest -Uri "https://www.microsoft.com/fr-ca/"
Run Code Online (Sandbox Code Playgroud)

-UseBasicParsing 使其运行,但我想使用 Invoke-WebRequest 返回的功能而无需基本解析,因为使用基本解析,我尝试提取的跨度字段未填充。

powershell freeze invoke-webrequest

3
推荐指数
1
解决办法
3005
查看次数

如何在 powershell 中使用 Invoke-WebRequest 重定向 URL 时获取 Location 标头

  • 调用永久移动或重定向的 URL,不会返回 Location http 标头或正确的位置。
  • 看来 URL 重定向正在 URL 调用期间执行(http 状态代码:302、301 等)。

我该如何克服这个问题?

powershell redirect location header invoke-webrequest

3
推荐指数
1
解决办法
3265
查看次数

Powershell ConvertFrom-Json 编码特殊字符问题

我的 powershell 脚本中有这段代码,它在特殊字符部分表现不佳。

 $request = 'http://151.80.109.18:8082/vrageremote/v1/session/players'
 $a = Invoke-WebRequest -ContentType "application/json; charset=utf-8" $request |
 ConvertFrom-Json    |
 Select -expand Data |
 Select -expand players |
 Select displayName, factionTag | Out-file "$scriptPath\getFactionTag.txt"
Run Code Online (Sandbox Code Playgroud)

在我的输出文件中,我只得到 '????' 对于任何特殊字符。有谁知道如何让它在我的输出文件中显示特殊字符?

powershell character-encoding invoke-webrequest

2
推荐指数
1
解决办法
1万
查看次数