Home > front end >  Issue with downloading different file from same website
Issue with downloading different file from same website

Time:04-04

So I manage to create below script, which will download the .exe file from McAfee website and execute it. However, the name of .exe file in website keeps on changing daily basis. For e.g. file is V3_4756dat.exe, next day version will be changed to V3_4757dat.exe and keeps on incrementing by 1. Any idea what changes I can make in below code to achieve this.

# URL and Destination
$url = "https://download.nai.com/products/datfiles/V3DAT/V3_4756dat.exe"
$dest = "C:\Users\user\Desktop\V3_4756dat.exe"
# Download file
Start-BitsTransfer -Source $url -Destination $dest
#file execution
Start-Process -FilePath “V3_4756dat.exe” -WorkingDirectory “C:\Users\user\Desktop\”

CodePudding user response:

Fortunately, there is access to directory services with belowlocation: https://download.nai.com/products/datfiles/v3dat/

We can use invoke-webrequest to download the directory content and parse the output file to know the latest name of exe file. After this, you can download the latest file and run the command.

Hope this helps.

With few minor corrections, I can propose below:

# Download the current directory output 
$outfile="c:\temp\currentfile.txt"

Invoke-WebRequest "https://download.nai.com/products/datfiles/v3dat/" -OutFile $outfile

$ExeFilename=(gc "c:\temp\abc.log" -tail 3 |where {$_ -match ".exe" }).split("=")[3].split(">")[0].replace("""", "") 

Remove-Item $outfile

 
# URL and Destination
$url = "https://download.nai.com/products/datfiles/v3dat/$ExeFilename"
$dest = "C:\temp\$ExeFilename"

echo $url

# Download file
Start-BitsTransfer -Source $url -Destination "c:\temp\" 

#file execution
Start-Process -FilePath "$ExeFilename" -WorkingDirectory "c:\temp\"

CodePudding user response:

Thanks GMaster9 for help in this. I also wanted to run this script on different users, hence using generic folder. Final script turned out to be like this.

#Create a new temp folder
New-Item -Path 'C:\Temp' -ItemType Directory
# Download the current directory output 
$outfile="C:\Temp\CurrentDatfiles.txt"
Invoke-WebRequest "https://download.nai.com/products/datfiles/v3dat/" -OutFile $outfile
$ExeFilename=(gc "C:\Temp\CurrentDatfiles.txt" -tail 3 |where {$_ -match ".exe" }).split("=")[3].split(">")[0].replace("""", "") 
Remove-Item $outfile
# URL and Destination
$url = "https://download.nai.com/products/datfiles/v3dat/$ExeFilename"
$dest = "C:\Temp\$ExeFilename"
echo $url
# Download file
Start-BitsTransfer -Source $url -Destination "C:\Temp" 
#file execution
Start-Process -FilePath "$ExeFilename" -WorkingDirectory "C:\Temp"
  • Related