4

How can I use this script on 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()

After I run this script I get this error:

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

I think the error is saying can't use Invoke-WebRequest on Windows 7. Why would that be?

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
mo.khorami
  • 79
  • 1
  • 1
  • 7

2 Answers2

9

By default Windows 7 comes with PowerShell version 2.0 installed. The Invoke-WebRequest cmdlet was introduced in PowerShell version 3.0.

The simplest solution is to upgrade your version of PowerShell to 3 or greater (I recommend just installing the latest version: 5.1). You can do that by downloading the Windows Management Framework:

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

Mark Wragg
  • 22,105
  • 7
  • 39
  • 68
1

i found this and working with powershell version 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

do you know any other command to do this for powershell version 2 ?

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

this is not working with powershell version 2 !

mo.khorami
  • 79
  • 1
  • 1
  • 7
  • 1
    Thanks for posting this. You'll need to ask your new question separately though. Q&A sites work differently from forums - each Question page is focused on asking/answering a single question. – mwfearnley Oct 27 '20 at 11:46