安装Chocolatey软件包时在PowerShell中尝试/ catch(Try/catch in PowerShell when installing Chocolatey packages)

编程入门 行业动态 更新时间:2024-10-26 16:25:56
安装Chocolatey软件包时在PowerShell中尝试/ catch(Try/catch in PowerShell when installing Chocolatey packages)

我一直在尝试创建一个脚本,用于在PowerShell中安装Chocolatey包。 这工作正常,但我想解析Chocolatey的输出。 我正在考虑的是类似的东西

$package = "something I want" try { $installCommand = "choco install $package" iex $installCommand -ErrorAction Stop -WarningAction Stop } catch { #Print error messages, i.e. $_.Exception.Message }

我是PowerShell的新手,我试图弄清楚如何使用try / catch。 我试过了

try { $command = "choco install asdasdasdasdasdad" iex $command -ErrorAction Stop -WarningAction Stop } catch { $message = $_.Exception.Message Write-Host $message }

但这给了我

Installing the following packages: asdasdasdasdasdad By installing you accept licenses for the packages. asdasdasdasdasdad not installed. The package was not found with the source(s) listed. If you specified a particular version and are receiving this message, it is possible that the package name exists but the version does not. Version: "" Source(s): "https://chocolatey.org/api/v2/" Chocolatey installed 0/1 package(s). 1 package(s) failed. See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log). Failures: - asdasdasdasdasdad

有小费吗? 提前谢谢!


我发现了一个代码示例,可以帮助我:

function testInstall { Param ( [string] $package ) Begin { Write-Host "Now in Begin.. Going on!" } Process { Write-Host "Starting Process" Write-Host "" $chocoCommand = "choco install $package" iex $chocoCommand Write-Host "" Write-Host "Ending Process" } End { Write-Host "Now in End" } } Function Test-Demo { Param ($Param1) Begin { write-host "Starting" } Process { if($_ -eq " Use --force to reinstall, specify a version to install, or try upgrade.") { $forceDetected = $true; } write-host $_ } End { if($forceDetected -eq $true) { Write-Warning "Force detected" Write-Warning "Do you want to re-run the installer with --force?" # Promt the user Y/N option and re-run testInstall -package $package --force } write-host "Ending" } } testInstall -package ruby | Test-Demo Sample

这将使我有机会询问用户是否要使用--force参数重新运行脚本。 唯一的问题是choco的输出已经失去了颜色。 有什么建议么?

I've been trying to create a script for installing Chocolatey packages in PowerShell. This works fine, but I want to parse the output from Chocolatey. What I'm thinking about is something like

$package = "something I want" try { $installCommand = "choco install $package" iex $installCommand -ErrorAction Stop -WarningAction Stop } catch { #Print error messages, i.e. $_.Exception.Message }

I'm pretty new to PowerShell, and I've tried to figure out how to use try/catch. I've tried

try { $command = "choco install asdasdasdasdasdad" iex $command -ErrorAction Stop -WarningAction Stop } catch { $message = $_.Exception.Message Write-Host $message }

But this gives me

Installing the following packages: asdasdasdasdasdad By installing you accept licenses for the packages. asdasdasdasdasdad not installed. The package was not found with the source(s) listed. If you specified a particular version and are receiving this message, it is possible that the package name exists but the version does not. Version: "" Source(s): "https://chocolatey.org/api/v2/" Chocolatey installed 0/1 package(s). 1 package(s) failed. See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log). Failures: - asdasdasdasdasdad

Any tips? In advance, thank you!


I found a code sample that helps me on my way:

function testInstall { Param ( [string] $package ) Begin { Write-Host "Now in Begin.. Going on!" } Process { Write-Host "Starting Process" Write-Host "" $chocoCommand = "choco install $package" iex $chocoCommand Write-Host "" Write-Host "Ending Process" } End { Write-Host "Now in End" } } Function Test-Demo { Param ($Param1) Begin { write-host "Starting" } Process { if($_ -eq " Use --force to reinstall, specify a version to install, or try upgrade.") { $forceDetected = $true; } write-host $_ } End { if($forceDetected -eq $true) { Write-Warning "Force detected" Write-Warning "Do you want to re-run the installer with --force?" # Promt the user Y/N option and re-run testInstall -package $package --force } write-host "Ending" } } testInstall -package ruby | Test-Demo Sample

This will give me the opportunity to ask the user if he/she wants to re-run the script with the --force parameter. The only problem is that the output from choco has lost its colors. Any suggestions?

最满意答案

需要Windows Management Framework 5:

register-packagesource -Name chocolatey -Provider PSModule -Trusted -Location http://chocolatey.org/api/v2/ -Verbose

我在这里找到了这个: https : //serverfault.com/questions/633576/how-do-you-manually-set-powershells-oneget-repository-source-to-chocolatey

然后你应该能够运行这样的东西并得到详细的输出:

Get-Package Nodejs | Install-Package -Verbose

After some testing, I made this. It's in develop, but it parses the output and I can print an installation summary at the end (or whatever I end up doing).

Feel free to comment.

https://github.com/bpjohannessen/ChocoPower

(I will mark this as the answer tomorrow)

更多推荐

本文发布于:2023-07-26 16:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1278184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:软件包   PowerShell   Chocolatey   packages   installing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!