PowerShell Run Exe from Web

  • Welcome to ITBible, we're your #1 resource for enterprise or homelab IT problems (or just a place to show off your stuff).
So there are times that you may need to run an exe across many devices, and maybe those devices are across several organizations or not on a Windows Domain. With this script you can upload the file to a central web server and the script will download the file and run it on devices.

Note: This script was created for our previous RMM though could be modified to be ran in an organization on remote machines using PSRemoting.

PowerShell:
param(
    [Parameter(Mandatory=$true)]
    [string]$packageUri, # url to downlaod the exe from
    [Parameter(Mandatory=$true)]
    [string]$packageName, # what to name the exe
    [string]$packageSwitches #any package switches
) #end param

$path = "C:\"
$folder = "Tools"
$fullPath = -join($path,$folder,"\",$packageName)

function Test-Folder {
    if (-Not (Test-Path -Path $folder)) {
        New-Item -Path $path -Name $folder -ItemType "directory" -Force
    }
} #end Test-Folder

function Get-Installer {
    if (Test-Path -Path $fullPath) {
        Remove-Item -Path $fullPath -Force
    }
    Invoke-WebRequest -Uri $packageUri -OutFile $fullPath
} #end Get-Installer

Test-Folder #verify the folder exists
Get-Installer #download a new installer
#run the install
Start-Process $fullPath -ArgumentList $packageSwitches -NoNewWindow -Wait

exit 1

It's a pretty simple script. Not flashy but it works.