Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, November 14, 2012

Use PowerShell to Download HTML code from a website

Here is a little code to help you download the HTML code of a website to a file on your client.

Function Download-HTML
{
[CmdletBinding(HelpUri
="http://get-help-jason-yoder.blogspot.com/2012/10/download-html.html")]
Param(
[Parameter(Mandatory
=$True)]$Source = "www.MCTExpert.com",
[Parameter(Mandatory
=$True)]$SaveFile = "C:\Users\Jason\Documents\Temp\Download1.HTML",
[
Switch]$Quiet

)

#Create an object to hold the web content.
$DataObj = New-Object System.Net.WebClient


Try
{
# Use the System.Net.WebClient method: Download to attempt to
# download the data. If it dows not exists, then error out.
$DataObj.DownloadFile("Http://$source", $SaveFile)

# Once the file is found, notify the user if $Quite is $False.
If ($Quiet-eq $False)
{
Write-Host "Website text downloaded to $SaveFile" -foreground Green -background DarkGreen
}
}
Catch
{
# If the file is not available and the code errors, let the user
# know the file is not there unless $Quest is $True.
If ($Quiet-eq $False)
{
Write-Host "File not Available" -ForegroundColor Red -BackgroundColor DarkRed
}
}
<#
.SYNOPSIS
Saves HTML source code from the web to a file on your hard drive.

.DESCRIPTION
Allows you to specify a website and download the source code.

.PARAMETER Source
The website that you want to download the source code from.

.PARAMETER SaveFile
The destination path and file name that you want to save the source code to.

.PARAMETER Quiet
Suppresses on screen messages.

.EXAMPLE
Download-HTML -Source "www.MCTExpert.com" -SaveFile "C:\Data\Code.html"

Downloads the code from the web page www.MCTExpert.com and saves it to the
local hard drive in the Data folder as Code.html.

#>
}

No comments: