【发布时间】:2018-02-02 20:34:50
【问题描述】:
为了尝试利用 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 ".\"
有什么建议吗?
谢谢
【问题讨论】:
-
当你说下载多个文件时;您可以多次运行
Invoke-WebRequest(即每个文件的 URL 一次);或者您是否希望从某个远程目录/给定页面中的所有链接项目/类似的东西拉回所有子项目? -
@JohnLBevan 我希望拉回远程目录下的子项。我以为我可以将它指向每个文件,但是当我有一个包含 50 个文件的目录时,这会很麻烦。
-
我不相信这在一个请求中是可能的;我对 HTTP 的理解是每个资源都需要自己的请求(这可能是为什么很多人在网站上有多个图像时使用 sprite,而不是浏览器简单地在一个请求中下载所有图像文件的集合)。
-
您可以从远程目录拉回链接列表,然后拉回每个项目...该解决方案在此处给出(即答案的第一部分):stackoverflow.com/a/27945081/361842
-
ps。您可以避免使用 7zip,而是使用 PowerShell(v5 及更高版本)或 .net(PS v5 之前)进行压缩。 7-Zip 确实提供了更好的压缩;但是,如果您想避免任何努力找出相关路径,或者有任何超出您控制范围的依赖项,则此选项可能会更好。 stackoverflow.com/a/40347498/361842
标签: powershell