This script will call out to a defined URL to check the version of a plugin and download the update if needed.
To use:
# Download only (if web is newer) #powershell -file DownloadLatestWordPressPlugin.ps1 <WordPress URL> <WordPress Plugin Version Installed> <Download Path> powershell -file DownloadLatestWordPressPlugin.ps1 https://wordpress.org/plugins/akismet/ 3.1.3 c:usersdkittelldesktopdownload # Download and Extract #powershell -file DownloadLatestWordPressPlugin.ps1 <WordPress URL> <WordPress Plugin Version Installed> <Download Path> <1 to extract> <Extract path> powershell -file DownloadLatestWordPressPlugin.ps1 https://wordpress.org/plugins/akismet/ 3.1.2 c:usersdkittelldesktopdownload 1 c:usersdkittelldownloadextracted
<#
.SYNOPSIS
This script will scrap the given WordPress Plugin page for the download link and download it with the option to also extract the download
.DESCRIPTION
This script is designed to take read the given WordPress plugin URL and find the downloadUrl link download it to a defined directory on your computer.
The script will assume the file name part of the URL is the file name for the computer.
.PARAMETER Path
.PARAMETER LiteralPath
.Example
DownloadLatestWordPressPlugin.ps1 https://wordpress.org/plugins/akismet/ 3.1.3 c:usersdkittelldesktopdownload
Latest version so no need to download
.Example
DownloadLatestWordPressPlugin.ps1 https://wordpress.org/plugins/akismet/ 3.1.2 c:usersdkittelldesktopdownload
Only Download
.Example
DownloadLatestWordPressPlugin.ps1 https://wordpress.org/plugins/akismet/ 3.1.2 c:usersdkittelldesktopdownload 1 c:usersdkittelldownloadextracted
Download and Extract
.Inputs
[String]$WebURL
[String]$PluginVersion
[String]$FileDirectory
[String]$DoExtract
[String]$ExtractFileDirectory
.Link
PowerShell Download Latest WordPress Plugin
#>
$names = [Environment+SpecialFolder]::GetNames([Environment+SpecialFolder])
#$URI = "https://wordpress.org/plugins/akismet/"
$URI = $args[0]
# This is the plugin version to check against
$PluginVersion = $args[1]
#$PluginVersion = "3.1.3"
# Directory Parameter
$FileDirectory = $args[2]
# Extract Parameter
# If you want to extract the downloaded file pass a 1 followed by the extract folder path
$DoExtract = $args[3]
# Extract Folder
$ExtractFileDirectory = $args[4]
$Logfile = $FileDirectory + "DownloadLog_$(get-date -format `"yyyyMMdd`").txt"
#$Logfile
function Write-Log ($Message)
{
$Message | Out-File -filepath $Logfile -Append
}
Write-Log ($(get-date -format "yyyy-MM-dd hh:mm:ss tt").ToString() + " - Process Started")
#$HTML = Invoke-WebRequest -Uri $URI | gm -MemberType Property
$HTML = Invoke-WebRequest -Uri $URI
#$HTML.Links.Count
$PluginName = $HTML.AllElements | WHERE -Property itemprop -eq "name" | Select -First 1 -ExpandProperty innerText
$PluginName = $PluginName.Trim()
$downloadURL = $HTML.Links | WHERE -Property itemprop -eq "downloadUrl" | SELECT -Property href
$downloadURLText = $HTML.Links | WHERE -Property itemprop -eq "downloadUrl" | SELECT -Property innerText
$downloadURL = $downloadURL -replace "@{href=", ""
$downloadURL = $downloadURL -replace "}", ""
$downloadURL = $downloadURL.Trim()
$downloadURLText = $downloadURLText -replace "@{innerText=Download Version ", ""
$downloadURLText = $downloadURLText -replace "}", ""
$downloadURLText = $downloadURLText.Trim()
Write-Log $PluginName
Write-Host $PluginName
if($PluginVersion -eq $downloadURLText)
{
Write-Log "Lastest Version, move on to the next link"
Write-Host "Lastest Version, move on to the next link"
Write-Log ($(get-date -format "yyyy-MM-dd hh:mm:ss tt").ToString() + " - Process Ended")
}
else
{
Write-Log "Out Dated Version, will now download"
Write-Host "Out Dated Version, will now download"
# URL Parameter
$WebURL =$downloadURL
# If directory doesn't exist create the directory
if((Test-Path $FileDirectory) -eq 0)
{
mkdir $FileDirectory;
cd $FileDirectory;
}
# We assume the file you download is named what you want it to be on your computer
$FileName = [System.IO.Path]::GetFileName($WebURL)
# Concatenate the two values to prepare the download
$FullFilePath = "$($FileDirectory)$($FileName)"
# Give a basic message to the user to let them know what we are doing
Write-Log "Downloading '$WebURL' to '$FullFilePath'"
Write-Host "Downloading '$WebURL' to '$FullFilePath'"
$uri = New-Object "System.Uri" "$WebURL"
$request = [System.Net.HttpWebRequest]::Create($uri)
$request.set_Timeout(15000) #15 second timeout
$response = $request.GetResponse()
$totalLength = [System.Math]::Floor($response.get_ContentLength()/1024)
$responseStream = $response.GetResponseStream()
$targetStream = New-Object -TypeName System.IO.FileStream -ArgumentList $FullFilePath, Create
$buffer = new-object byte[] 10KB
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $count
while ($count -gt 0)
{
[System.Console]::Write("`r`nDownloaded {0}K of {1}K", [System.Math]::Floor($downloadedBytes/1024), $totalLength)
$targetStream.Write($buffer, 0, $count)
$count = $responseStream.Read($buffer,0,$buffer.length)
$downloadedBytes = $downloadedBytes + $count
}
$targetStream.Flush()
$targetStream.Close()
$targetStream.Dispose()
$responseStream.Dispose()
# Give a basic message to the user to let them know we are done
Write-Log "`r`nDownload complete"
Write-Host "`r`nDownload complete"
# If a 1 is passed we will extract the zip file
if($DoExtract -eq 1)
{
Write-Log "`r`nExtracting File"
Write-Host "`r`nExtracting File"
$Shell = new-object -com shell.application
# If directory doesn't exist create the directory
if((Test-Path $ExtractFileDirectory) -eq 0)
{
mkdir $ExtractFileDirectory;
cd $ExtractFileDirectory;
}
# Get the name of the Zip file
$Zip = $Shell.NameSpace($FullFilePath)
#Expand/Extract each file from the zip file
foreach($Item in $Zip.items())
{
$Shell.Namespace($ExtractFileDirectory).copyhere($Item)
}
Write-Log "`r`nFile Extracted"
Write-Host "`r`nFile Extracted"
}
Write-Log ($(get-date -format "yyyy-MM-dd hh:mm:ss tt").ToString() + " - Process Ended")
}
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.