## Define the scripts and URLs to them $URLS = @( 'https://emberkom.s3.amazonaws.com/management/scripts/Upload-Contents.ps1' ) ## Define and prepare the main script directory $SCRIPT_DIRECTORY = "${Env:ProgramData}\Emberkom\Scripts" if (!(Test-Path $SCRIPT_DIRECTORY)) { New-Item -ItemType Directory -Path $SCRIPT_DIRECTORY } ## Loop through each URL ForEach ($url in $URLS) { ## Set the script name based on the URL path $SCRIPT_FILE = $($url.Split('/') | Select-Object -Last 1) ## Delete any previous version of the script If ( Test-Path "${SCRIPT_DIRECTORY}\${SCRIPT_FILE}" ) { try { Remove-Item -Path "${SCRIPT_DIRECTORY}\${SCRIPT_FILE}" } catch { Write-Host "Could not delete ${SCRIPT_DIRECTORY}\${SCRIPT_FILE}"; exit } } ## Download latest version of script If ( !( Test-Path "${SCRIPT_DIRECTORY}\${SCRIPT_FILE}" )) { try { (New-Object System.Net.WebClient).DownloadFile($url,"${SCRIPT_DIRECTORY}\${SCRIPT_FILE}") } catch { Write-Host "Failed to download ${url}" } } }