Invoke-WebRequest无法识别为cmdlet Windows 7 Powershell的名称

时间:2018-10-28 20:15:42

标签: powershell windows-7-x64 invoke-webrequest

如何在Windows 7 PowerShell上使用此脚本?

null

运行此脚本后,出现此错误:

$IE = new-object -com internetexplorer.application
$go = (Invoke-WebRequest –Uri ‘c:\link.html’).Links.href  
$IE.navigate($go)
$IE.visible=$true
start-sleep 5
$word=$go = (Invoke-WebRequest –Uri ‘c:\word.html’).Links.href 
$Link = $IE.Document.getElementsByTagName("span") | ? {$_.InnerHTML -eq "$word"}
$word2=$go = (Invoke-WebRequest –Uri ‘c:\word2.html’).Links.href 
$ie.Document.getElementsByTagName("$word2").item(0).click()

我认为错误是说无法在Windows 7上使用The term 'Invoke-WebRequest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:7 char:29 + $go = (Invoke-WebRequest <<<< –Uri ‘http://lapfix.ir/link.html’).Links.href + CategoryInfo : ObjectNotFound: (Invoke-WebRequest:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException The term 'Invoke-WebRequest' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify t hat the path is correct and try again. At line:12 char:31 + $word=$go = (Invoke-WebRequest <<<< –Uri ‘http://lapfix.ir/word.html’).Links.href + CategoryInfo : ObjectNotFound: (Invoke-WebRequest:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Cannot find an overload for "getElementsByTagName" and the argument count: "1". At line:13 char:42 + $Link = $IE.Document.getElementsByTagName <<<< ("span") | ? {$_.InnerHTML -eq "$word"} + CategoryInfo : NotSpecified: (:) [], MethodException + FullyQualifiedErrorId : MethodCountCouldNotFindBest You cannot call a method on a null-valued expression. At line:14 char:12 + $Link.click <<<< () + CategoryInfo : InvalidOperation: (click:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull 。为什么?

2 个答案:

答案 0 :(得分:5)

默认情况下,Windows 7装有PowerShell版本2.0。 Invoke-WebRequest cmdlet在PowerShell 3.0版中引入。

最简单的解决方案是将PowerShell版本升级到3或更高版本(我建议仅安装最新版本:5.1)。您可以通过下载Windows Management Framework来做到这一点:

https://www.microsoft.com/en-us/download/details.aspx?id=54616

答案 1 :(得分:0)

我发现了这一点,并使用了Powershell版本2

$req = [System.Net.WebRequest]::Create("http://sample.com/link.html")
$resp = $req.GetResponse()
$reqstream = $resp.GetResponseStream()
$stream = new-object System.IO.StreamReader $reqstream
$result = $stream.ReadToEnd()
This is for test result : #Write-Host -Object $result

您知道在Powershell版本2中执行此操作的任何其他命令吗?

$Link = $IE.Document.getElementsByTagName("span") | ? {$_.InnerHTML -eq "https://sample.com/"}
$Link.click()

这不适用于Powershell版本2!