Powershell : downloading up-to-date software to network share

I recently created an article about automatically updating certain applications, that article is linked here

Software inevitably gets updated by the manufacturer, therefore, this particular script Goes to the download website and download the latest version from the website and then put them in a predesignated network share with a specified naming convention.

This means when the script runs it will download the latest copy of the software and add it to the network share as specified in the script, then when the installation script calls that installer it will automatically install with the latest version.

Then once downloaded before being copied to the network location we can modify the script to rename the downloaded file to the desired names.

Note : This only downloads PuTTY as there is a direct download link, not protected by hotlink protection which means, whereas WinSCP requires you to click the download button and the downloads are behind hotlink protection, so that is not something that can be easily scripted.

# Function to download file from URL
function Download-File {
    param(
        [string]$url,
        [string]$outputPath
    )

    try {
        $webClient = New-Object System.Net.WebClient
        $webClient.DownloadFile($url, $outputPath)
        Write-Host "Downloaded: $outputPath"
    }
    catch {
        Write-Host "Error downloading file from $url : $_" -ForegroundColor Red
    }
}

# URL for PuTTY download page
$puttyUrl = "https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html"

# Function to extract download URL from PuTTY download page
function Get-PuTTYDownloadUrl {
    $pageContent = Invoke-WebRequest -Uri $puttyUrl
    $puTTYDownloadUrl = $pageContent.ParsedHtml.getElementsByTagName("a") | Where-Object { $_.href -like "*.msi" } | Select-Object -First 1 -ExpandProperty href
    return $puTTYDownloadUrl
}

# Network shared folder path
$networkSharedFolder = "\\distribution.bear\software"

# Ensure network shared folder exists
if (-not (Test-Path -Path $networkSharedFolder)) {
    New-Item -Path $networkSharedFolder -ItemType Directory | Out-Null
}

# Download PuTTY installer to the network shared folder
$puttyOutputPath = Join-Path -Path $networkSharedFolder -ChildPath "PuTTY.msi"
Download-File -url (Get-PuTTYDownloadUrl) -outputPath $puttyOutputPath

Write-Host "PuTTY download process completed."

This shows the original install from the previous script, as you can see the files are named as the default download with the wrong name:

Now lets run the script and we should see a file called "PuTTY.exe"


Excellent, that means you can keep this software up-to-date by running a script.
Previous Post Next Post

نموذج الاتصال