How to echo if invoke-webrequest failed due to an invalid/expired url else continue download in Powershell CLI?

huangapple go评论59阅读模式
英文:

How to echo if invoke-webrequest failed due to an invalid/expired url else continue download in Powershell CLI?

问题

你可以尝试以下方式来实现你的需求:

powershell "$progresspreference='silentlycontinue'; $response = Invoke-WebRequest -Uri 'https://thisismyurl.com/123456' -Method Head; if ($response.StatusCode -ne 200) { echo 'Download failed! URL is invalid or expired.'; pause; goto label; } else { wget -uri 'https://thisismyurl.com/123456' -outfile '%userprofile%\Downloads\file.zip' }"

这个命令将首先使用Invoke-WebRequest来检查 URL 是否有效,如果状态码不是 200,则会输出错误信息并跳转到 label。如果 URL 有效,它将继续下载文件。

至于你提到的将 PowerShell 命令分开的问题,你可以使用分号 ; 来分隔多个命令,或者将它们放在多行上,如上面的示例所示。不过,请确保在适当的位置添加分号或换行,以保持语法正确性。

英文:

I want to use a powershell command inside a batch file to download a file.

My code looks like that and works for just downloading the file from that specific url:

powershell "$progresspreference='silentlycontinue'; wget -uri "https://thisismyurl.com/123456" -outfile '%userprofile%\Downloads\file.zip'"

Now I want to implement echo download failed! url is invalid. & pause & goto label if the invoke-webrequest failed due to an invalid or expired url.

Also, since the powershell commands within the batch file get pretty long, is there a way to break up the commands?

I tried

powershell "$progresspreference='silentlycontinue' `
wget -uri "https://thisismyurl.com/123456" -outfile '%userprofile%\Downloads\file.zip'"

but that didn't work.

答案1

得分: 2

* 调用 [`powershell.exe`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe) 时,使用隐含的 `-Command` 参数,表示命令字符串中 *最后一条语句* 决定了 `powershell.exe` 的退出代码:成功时为 `0`,否则为 `1`
  * 在 Windows PowerShell 中,`wget`[`Invoke-WebRequest`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest) cmdlet 的别名,与任何 cmdlet 一样,如果在执行过程中发生 *任何* 错误,其成功状态被视为 `$false`,因此转换为退出代码 `1`
  * 因此,可以简单地使用 `cmd.exe``||` 运算符处理 `powershell.exe` 退出代码为 _非零_ 的情况。

* 对于来自批处理文件的 *多行* PowerShell CLI 调用,请参阅 [此答案](https://stackoverflow.com/a/69439031/45375)。简而言之:无法使用总体 `"..."` 封闭,因此必须使用 `^` 转义选定字符,并且必须在每个内部行末尾使用 `^`
在代码的上下文中综合起来:

@echo off & setlocal

powershell $progresspreference='silentlycontinue'; ^
wget -uri 'https://thisismyurl.com/123456' ^
-outfile '%userprofile%\Downloads\file.zip' ^
|| (echo download failed! url is invalid. & pause & goto label)

exit /b 0

:label
echo failure-handling branch...
exit /b 1


<details>
<summary>英文:</summary>

&lt;!-- language-all: sh --&gt;

* You&#39;re calling [`powershell.exe`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe), the Windows PowerShell CLI, with the implied `-Command` parameter, which means that the success status of the *last statement* in the command string determines `powershell.exe`&#39;s exit code: `0` in the success case, `1` otherwise.

  * In Windows PowerShell, `wget` is an alias for the [`Invoke-WebRequest`](https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-WebRequest) cmdlet, and, as with any cmdlet, if *any* error occurs during its execution, its success status is considered `$false` and therefore translates to exit code `1`.

  * Therefore, you can simply use `cmd.exe`&#39;s `||` operator to act on the case where `powershell.exe`&#39;s exit code is _non-zero_.

* As for *multiline* PowerShell CLI calls from a batch file, see [this answer](https://stackoverflow.com/a/69439031/45375). In short: you cannot use overall `&quot;...&quot;` enclosure and must therefore `^`-escape select characters, and you must end each interior line with `^`

To put it all together in the context of your code:

@echo off & setlocal

powershell $progresspreference='silentlycontinue'; ^
wget -uri 'https://thisismyurl.com/123456' ^
-outfile '%userprofile%\Downloads\file.zip' ^
|| (echo download failed! url is invalid. & pause & goto label)

exit /b 0

:label
echo failure-handling branch...
exit /b 1


</details>



huangapple
  • 本文由 发表于 2023年5月6日 23:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定