我是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"
}
}发布于 2018-05-15 17:07:36
https://winscp.net/download/WinSCP-5.13.1-Setup.exe不是下载地址。这是一个下载页面。您的代码下载的是HTML文档,而不是可执行的二进制文件。
实际上,您不能从WinSCP站点自动下载二进制文件。它的设计是为了防止它的带宽被滥用,用于你这样的用途。
上面的网址为您提供了一个可执行文件,因为它返回的超文本标记语言包含一个将您的浏览器重定向到一次性下载URL的JavaScript代码。当然,您也可以实现相同的功能(获取HTML并使用其返回的数据查找/生成下载URL )。但这是一种虐待。
https://stackoverflow.com/questions/50344959
复制相似问题