## Define the tools and URLs to them $URLS = @( 'https://emberkom.s3.amazonaws.com/tools/LiquidFilesCLI.exe', 'https://emberkom.s3.amazonaws.com/tools/7za.exe' ) ## Test for write access to tools directory, redirect to AppData If denied $TOOLS = "${Env:ProgramData}\Emberkom\Tools" $TEST_FILE = "${TOOLS}\testfile.txt" ## Test to see if $TOOLS exists If (!( Test-Path $TOOLS )) { ## If $TOOLS does not exist, create it. Try { New-Item -ItemType Directory -Path $TOOLS } ## If we get an error, switch to the logged on user's AppData directory Catch { $TOOLS = "${Env:AppData}\Emberkom\Tools" If ( !(Test-Path $TOOLS) ) { New-Item -ItemType Directory -Path $TOOLS } } } ## If $TOOLS already exists... Else { ## Try to create a file to make sure we have permission to modify the files in there Try { [IO.File]::OpenWrite($TEST_FILE).Close() If ( Test-Path $TEST_FILE ) { Remove-Item $TEST_FILE -Force } } ## If we get an error, switch to the logged on user's AppData directory Catch { $TOOLS = "${Env:AppData}\Emberkom\Tools" If ( !(Test-Path $TOOLS) ) { New-Item -ItemType Directory -Path $TOOLS } } } ## Loop through each URL ForEach ($url in $URLS) { ## Set the tool name based on the URL path $TOOL = $($url.Split('/') | Select-Object -Last 1) ## Delete any previous version of the tool If ( Test-Path "${TOOLS}\${TOOL}" ) { Try { Remove-Item -Path "${TOOLS}\${TOOL}" } Catch { Write-Host "Could not delete ${TOOLS}\${TOOL}"; exit } } ## Download latest version of tool If ( !( Test-Path "${TOOLS}\${TOOL}" )) { Try { (New-Object System.Net.WebClient).DownloadFile($url,"${TOOLS}\${TOOL}") } Catch { Write-Host "Failed to download ${url}" } } }