【问题标题】:Powershell Downloading and Executing Files (File or Directory is corrupted and unreadable)Powershell 下载和执行文件(文件或目录已损坏且无法读取)
【发布时间】:2020-08-13 11:17:52
【问题描述】:

我一直在尝试使用 powershell 下载和执行文件。一切正常,Powershell 从“Workupload.com”下载文件并将其放在我的 /user 文件夹中。 这与图片完美搭配,下载后我也可以打开它们。

但是当我尝试使用 .exe 或 .txt 文件时它不起作用.. 这是我的代码:

$WebClient = New-Object System.Net.WebClient
$url = 'link to my .exe on workupload'
$dst = 'FireFox-Installer.exe' #Not adding a Path just puts the File in the User Folder
$WebClient.DownloadFile($url, $dst)
Start-Process $dst 'FireFox-Installer.exe'

现在,当我尝试打开文件时,我收到文件已损坏且无法读取的错误消息。我想知道如何解决这个问题.. Windows-Defender 会阻止它吗?我的代码错了吗?还是导致错误的 File-Hoster?

这是我的错误代码:

Start-Process : This command cannot be run due to the error: The file or directory is corrupted and unreadable.
At line:1 char:1
+ Start-Process $dst 'FireFox-Installer.exe'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

非常感谢一些帮助,因为我是 Powershell 的新手,谢谢大家! :)

【问题讨论】:

  • 你用来为变量 $dst 编写 cmets 的那些是什么?您必须使用 # 在 Powershell 中编写 cmets。此外,Start-Process 不会那样工作,因为第一个参数需要 $dst,第二个参数需要 'FireFox-Installer.exe'。如果要加入路径,请使用 join-path Start-Process (Join-Path $dst 'FireFox-Installer.exe')
  • Opps,这不是我的代码的一部分,我只是写了这个,以便人们了解文件目标。谢谢你
  • 不用担心。我编辑了我的第一条评论。尝试使用 join-path 创建一个正确的文件名,作为 Start-Process 的参数。
  • 我现在使用此代码“Start-Process (Join-Path $dst 'FireFox-Installer.exe')”,但现在我得到一个不同的错误,即“Start-Process:此命令不能由于错误而被运行:系统找不到指定的文件。”
  • 那么路径可能是错误的。将 Start-Process 行放在注释中并在其下方写入:Write-Host (Join-Path $dst 'FireFox-Installer.exe') 并检查路径是否正确。如果没有,你可能在 $dst 中构建路径时做错了

标签: powershell download execute


【解决方案1】:

好的,我只是尝试了这个,它按预期工作:

$WebClient = New-Object System.Net.WebClient
$url = 'https://download-installer.cdn.mozilla.net/pub/firefox/releases/79.0/win32/en-US/Firefox%20Installer.exe'
$dst = 'C:\Temp'
$WebClient.DownloadFile($url, (Join-Path $dst 'FireFox-Installer.exe'))
Start-Process (Join-Path $dst 'FireFox-Installer.exe')

我刚刚为DownloadFile 的目标参数添加了另一个Join-Path 如果它仍然不起作用,我建议您检查您的链接并测试您的 Firefox-Installer.exe 文件。

【讨论】:

    【解决方案2】:

    如果您将目标根文件夹(例如 \\myserver\myshare)添加到 IE 中的受信任站点,然后关闭并重新启动您的 PS 会话,原来的 $dst 是否有效?

    【讨论】:

    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 2020-04-30
    • 2019-05-04
    • 2011-10-09
    • 2018-04-10
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多