代码之家  ›  专栏  ›  技术社区  ›  Wiktor

添加变量以调用rest方法

  •  1
  • Wiktor  · 技术社区  · 7 年前

    我正在尝试编写一个脚本,该脚本从网站“$\.id-eq”s215”返回变量。

    我尝试了下面的函数,但我得到一个错误,变量$form找不到。。。。

    我在microsoft站点找到了此解决方案-> https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6 例5

    Function MyFunctionTemp{
        clear-host
            $Uri = "http://....../index.asp" 
            $Form = @{
            Name  = 'John'
                     }
            $Result = Invoke-RestMethod -Uri $Uri -Method Post -FOrm $Form
            $Result.AllElements | Where {$_.id -eq "s215"} | Select Name, innerText | ConvertTo-HTML -Head $headPath
    }
         $html_url = MyFunctionTemp
        Add-Type -AssemblyName System.Web
    
    [System.Web.HttpUtility]::HtmlDecode($html_url) | Out-File  "\\plwawprn01\xpbuild$\IT\Level0\scresult\test.htm" 
    

    enter image description here

    下面的错误

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   Robert Dyjas Joey    7 年前

    你的错误不是那样的 $Form 找不到。错误表明 Invoke-RestMethod 不知道是什么参数 -Form 是

    一定要跑 $PSVersionTable PSVersion 属性,然后通过文档网站上的下拉菜单选择正确的版本:

    Choosing PS version on the documentation

    此外,如评论中所述(归功于 @Matt @gms0ulman 注意到这一点)您可能想尝试使用 Invoke-WebRequest 相反

    推荐文章