Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

PowerShell Download Latest WordPress Plugin

Posted on September 17, 2015 By David Kittell

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.

Related

Code PowerShell

Post navigation

Previous post
Next post

Related Posts

PowerShell – Mass Rename Files / Directories

Posted on March 25, 2019

Recently I needed to rename files and directories in a repository so I came up with the PowerShell script. NOTE: Use caution using this. cd "C:\Users\dkittell\Repos\TestRepo" Get-ChildItem -File -Recurse | Rename-Item –NewName { $_.name –replace " ","_" } # Replace space with underscore – Files Get-ChildItem -File -Recurse | Rename-Item…

Read More

Find Table Differences And Sync Tables

Posted on May 26, 2015October 26, 2015

Have two databases with the same table schema and need to keep both up to date? First, let us look at the differences between the two databases. The query below will show the differences between the development database table tbl_zip_code and the production database table tbl_zip_code. Ideally the differences should…

Read More

Oracle Clean Date Format

Posted on September 18, 2013October 26, 2015

Sometimes you have to pull a date from Oracle in a cleaner format, this is one way to achieve the format you want. // Convert the Oracle date 19000101 to 01-01-1900 public string OracleCleanDate(string strDate) { if (strDate.Length == 8) { string strYear, strMonth, strDay; strYear = strDate.Substring(0, 4); strMonth…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes