2

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

$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()

运行此脚本后,我收到此错误:

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

我认为错误是说不能Invoke-WebRequest在 Windows 7 上使用。为什么会这样?

4

2 回答 2

8

默认情况下,Windows 7 安装了 PowerShell 2.0 版。Invoke-WebRequestcmdlet 是在 PowerShell 版本 3.0 中引入的。

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

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

于 2018-10-28T20:21:11.743 回答
1

我发现了这个并使用了 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 !

于 2018-10-30T06:22:15.240 回答