Powershell Invoke-Webrequest下载并保存所有文件?

时间:2018-02-02 20:34:51

标签: powershell

尝试利用PowerShell自动化下拉文件,对它们执行某些操作,然后将它们复制到其他位置的过程,我的大部分过程都在工作。我遇到的唯一问题是我无法调用webrequest来下载多个文件。

# 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

我希望利用这条线来做

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

有什么建议吗?

由于

2 个答案:

答案 0 :(得分:2)

Invoke-WebRequest使用Powershell执行HTTP操作。

Invoke-WebRequest可以执行所有HTTP方法。您可以使用 Method 参数完成所有操作(最受欢迎的是:GET,POST,PUT,DELETE)。

在HTTP协议中,没有选项可以下载特定链接下的所有文件。如果您想这样做,则需要“爬行”整个页面。首先列出给定链接的内容,然后在某种解析网站上通过链接执行foreach循环以下载每个文件。

enter image description here

答案 1 :(得分:0)

使用invoke-webrequest查找链接。您可以使用正则表达式将其拉出。然后,您可以使用start-bitstransfer下载每个查找。

相关问题