Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Invoke-Webrequest to download and save all files?

Tags:

powershell

In an attempt to utilize PowerShell to automate a process of pulling down files, doing something with them, and then copying them to somewhere else, I have most of the process working. My only issue I am encountering is I cannot get invoke webrequest to download multiple files.

# Specify variables

$SVN_BASE = "website ommitted" 
$SCCM_PATH = "path omitted"
$LOKI_PATH = "path omitted"
$INSTALLER_NAME = "Firefox Setup 58.0.1.exe"
$PROJECT_FOLDER = "mozilla_firefox_rr"

# Set an alias for the executable 7zip to be called to extract files

set-alias 7z "$env:ProgramFiles\7-Zip\7z.exe"

# Create the working directory for the application

new-item -path "$($env:userprofile)\Desktop" -name $PROJECT_FOLDER -itemtype 
directory -Force

# Change the directory to the working directory 

set-location "$($env:userprofile)\Desktop\$PROJECT_FOLDER"

# Invoke-WebRequest is aka wget. Here, we are downloading the required file
# and placing it into our working directory

Invoke-WebRequest "$SVN_BASE" -outfile ".\"
Invoke-WebRequest "$LOKI_PATH/$INSTALLER_NAME" -outfile 
"$($env:userprofile)\Desktop\$PROJECT_FOLDER\$INSTALLER_NAME"

# Extract contents of executable

7z x Firefox*.exe

# Remove contents that aren't needed

Remove-item .\$INSTALLER_NAME
Remove-item "$SCCM_PATH\core" -recurse
Remove-item "$SCCM_PATH\setup.exe" -recurse

# The final step is copying the newly extracted files to the corresponding SCCM directory

copy-item ".\*" -Destination $SCCM_PATH -recurse

The line that I am hoping to utilize to do this is

Invoke-WebRequest "$SVN_BASE" -outfile ".\"

Any suggestions?

Thanks

like image 972
xerxes2985 Avatar asked Oct 29 '25 11:10

xerxes2985


1 Answers

Invoke-WebRequest performs HTTP operation with Powershell.

Invoke-WebRequest can perform all HTTP methods. You can accomplish every of them using Method parameter (most popular ones are: GET, POST, PUT, DELETE).

In HTTP protocol there is no option to download all files under particular link. If you want to do it you need to 'crawl' through the page. First list the content of the given link and after some kind of parsing website perform foreach loop through links to download each file.

enter image description here

like image 151
pawel wujczyk Avatar answered Oct 31 '25 01:10

pawel wujczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!