0

How would I call a URL and turn the page content into variables to use in PowerShell?

The HTML page looks like this:

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<span class="server1var1">8</span>
<span class="server1var2">2</span>

<span class="server2var1">5</span>
<span class="server2var2">1</span>

</body>
</html>

Then in PowerShell I want these variables:

$server1var1 = "8"
$server1var2 = "2"

$server2var1 = "5"
$server2var2 = "1"

Accepting the answer below with one modification to make it work for me. Changing $_.textContent to $_.innerhtml solved the issue.

$content = Invoke-WebRequest -Uri 'http://urltomysite'
$content.ParsedHtml.getElementsByTagName('span') | ForEach-Object { 
    New-Variable -Name $_.className -Value $_.innerhtml
    }
Damien
  • 157
  • 5
  • 12

2 Answers2

2

Use the Invoke-WebRequest cmdlet to retrieve the content of your website. Then use the getElementsByTagName of the ParsedHtml property to retrieve all span tags. Finally turn them into a variable using the New-Variable cmdlet:

$content = Invoke-WebRequest -Uri 'http://gameadmin.zerosurvival.com/servercodes.php'
$content.ParsedHtml.getElementsByTagName('span') | ForEach-Object { 
    New-Variable -Name $_.className -Value $_.textContent 
    }

After you run this, if you type $server1var1 you will get 8.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • Running that exact code (changing only the Uri to a page of mine), I get no output for $server1var1. `$content = Invoke-WebRequest -Uri 'http://urltoyoursite' $content.ParsedHtml.getElementsByTagName('span') | ForEach-Object { New-Variable -Name $_.className -Value $_.textcontent } echo "$server1var1"` I get no errors either. – Damien Nov 24 '16 at 07:28
  • This code is working since I hosted your html in IIS and testet it. Is the URL public - can you share it? Does the HTML contains a span tag with class name `server1var1`? Does it have a value? – Martin Brandl Nov 24 '16 at 07:31
  • This is working as expected. after I run this, the `$server1var1` contains `8`. – Martin Brandl Nov 24 '16 at 07:34
  • doesnt work in my pc : error : New-Variable : Impossible de lier l'argument au paramètre « Name », car il a la valeur Null. – Esperento57 Nov 24 '16 at 07:35
  • What do you get if you run `(Invoke-WebRequest -Uri 'http://gameadmin.zerosurvival.com/servercodes.php').ParsedHtml.getElementsByTagName('span') | select className, textContent` – Martin Brandl Nov 24 '16 at 07:46
  • Changing `textContent` to `innerhtml` I get http://i.imgur.com/M6QQo2J.png Updating your original code from $_.textcontent to $_.innerhtml works!! Is there a reason why this would work for me, but textContent would not? – Damien Nov 24 '16 at 08:00
1

other solution

$template=@'
<span class="{servername*:server1var1}">{value:8}</span>
<span class="{servername*:server1var2}">{value:2}</span>
'@

(Invoke-WebRequest -Uri 'http://www.google.fr').ParsedHtml.body.innerHTML | ConvertFrom-String -TemplateContent $template
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • While this may work in the provided example, this is not the way you want to parse a html file. See: http://stackoverflow.com/a/1732454/1163423. Also it doesn't set variables... – Martin Brandl Nov 24 '16 at 07:36
  • 1
    this solution is a answer which work, he can choice to use or not (but your respect condition but your solution doesnt work in my pc...) – Esperento57 Nov 24 '16 at 07:40
  • On the link you gave the opinions are shared – Esperento57 Nov 24 '16 at 07:42