Powershell

Table of Contents

4.2 Web Requests

Invoke-rest

$rssfeedInvoke-RestMethod -Uri 'https://blogs.technet.microsoft.com/heyscriptingguy/feed/'
$rssfeed[0].InnerText | ForEach-Object {$_ -replace '<[^>]+>',''}



invoke-webrequest – Web Scrap

$parseURL = 'www.poshpython.com'
$webRequest = Invoke-WebRequest -Uri $parseURL
$webRequest.ParsedHTML.getElementsByTagName('p') | Where-Object {$_.ClassName -eq 'title'} | Select-Object -ExpandProperty OuterText



invoke-webrequest – Download file

$downloadURL = 'https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html'
$downloadRequest = Invoke-WebRequest -Uri $downloadURL
$downloadRequest.Links | Where-Object {$_ -like '*elv*' -and $_ -like '*download*'}
$elvLink = ($downloadRequest.Links | Where-Object {$_ -like '*elv*' -and $_ -like '*download*'}).href
$fileName = $elvLink.Substring($elvLink.LastIndexOf('/')+1)
$downloadRequest = Invoke-WebRequest -Uri $elvLink -OutFile "C:\download\$fileName" -PassThru



Invoke-webrequest - POST (Form Query [Search])

$uri = "www.google.com"
$webRequest = Invoke-WebRequest 'www.google.com'
$searchForm = $webRequest.Forms[0]
$searchForm.Fields.q = 'PowerShell'
$searchGoogle = Invoke-WebRequest -Uri "www.google.com$($form.action)" -Method $form.method -Body $form.fields
$searchGoogle.Links | Where-Object {$_.Class -eq 'search'} | Select-Object InnerText,Href



Invoke-webrequest - POST (Form logon)

$credential = Get-Credential
$uaString = [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox
$webRequest = Invoke-WebRequest -Uri 'www.Outlook.com' -SessionVariable webSession -UserAgent $uaString
$loginForm = $webRequest.Forms | Where-Object {$_.Id -eq 'login_login-main'}
$loginForm.Fields.user = $credential.UserName
$loginForm.Fields.passwd = $credential.GetNetworkCredential().Password
$webRequest = Invoke-WebRequest -Uri $loginForm.Action -Method $loginForm.Method -Body $loginForm.Fields -WebSession $webSession -UserAgent $uaString
if ($webRequest.Links | Where-Object {$_ -like ('*' + $credential.UserName + '*')}) { Write-Host "Login verified!"
}
else {
Write-Host 'Login unsuccessful!' -ForegroundColor yellow-BackgroundColor DarkBlue
}


Visit next page to learn about - 4.3 SSH