我一直在尝试创建一个脚本,用于在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: - asdasdasdasdasdadAny 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 SampleThis 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 -VerboseAfter 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)
更多推荐
发布评论