Reputation: 157
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
}
Upvotes: 0
Views: 6706
Reputation: 58931
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
.
Upvotes: 2
Reputation: 17472
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
Upvotes: 1