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
    }

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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?
This is working as expected. after I run this, the $server1var1 contains 8.
doesnt work in my pc : error : New-Variable : Impossible de lier l'argument au paramètre « Name », car il a la valeur Null.
What do you get if you run (Invoke-WebRequest -Uri 'http://gameadmin.zerosurvival.com/servercodes.php').ParsedHtml.getElementsByTagName('span') | select className, textContent
|
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

3 Comments

While this may work in the provided example, this is not the way you want to parse a html file. See: stackoverflow.com/a/1732454/1163423. Also it doesn't set variables...
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...)
On the link you gave the opinions are shared

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.