我是PowerShell的新手,并没有太多的编程背景,只是尝试使用下面的power shell脚本来安装很少的软件。执行WinSCP软件时脚本会抛出错误。
错误消息
Program 'WinSCP-5.13.1-Setup.exe' failed to run: The file or directory is corrupted and unreadableAt line:1 char:1
+ D:\softwares\WinSCP-5.13.1-Setup.exe /
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
At line:1 char:1
+ D:\softwares\WinSCP-5.13.1-Setup.exe /SP
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
脚本:
$source = 'D:\softwares'
If (!(Test-Path -Path $source -PathType Container)) {New-Item -Path $source -ItemType Directory | Out-Null}
$packages = @(
@{title='7zip Extractor';url='http://downloads.sourceforge.net/sevenzip/7z920-x64.msi';Arguments=' /qn';Destination=$source},
@{title='Notepad++ 7.5.6';url='https://notepad-plus-plus.org/repository/7.x/7.5.6/npp.7.5.6.Installer.exe';Arguments=' /Q /S';Destination=$source}
@{title='WinScp 5.13.1';url='https://winscp.net/download/WinSCP-5.13.1-Setup.exe';Arguments=' /';Destination=$source}
)
foreach ($package in $packages) {
$packageName = $package.title
$fileName = Split-Path $package.url -Leaf
$destinationPath = Join-Path $package.Destination $fileName
If (!(Test-Path -Path $destinationPath -PathType Leaf)) {
Write-Host "Downloading $packageName"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($package.url,$destinationPath)
#Start-Sleep -s 10
}
#Once we've downloaded all our files lets install them.
foreach ($package in $packages) {
$packageName = $package.title
$fileName = Split-Path $package.url -Leaf
$destinationPath = Join-Path $package.Destination $fileName
$Arguments = $package.Arguments
Write-Output "Installing $packageName"
Invoke-Expression -Command "$destinationPath $Arguments"
}
}
答案 0 :(得分:2)
https://winscp.net/download/WinSCP-5.13.1-Setup.exe
不是下载网址。这是一个下载页面。您的代码下载HTML文档,而不是可执行的二进制文件。
您实际上无法从WinSCP站点自动下载二进制文件。它旨在防止滥用其带宽,用于像您这样的用途。
上面的URL只会获取可执行文件,因为它返回的HTML包含一个JavaScript代码,可将浏览器重定向到一次性下载URL。当然,您可以实现相同的功能(使用它返回的数据获取HTML并查找/生成下载URL)。但这是一种虐待。