Try/catch in PowerShell when installing Chocolatey packages -


i've been trying create script installing chocolatey packages in powershell. works fine, want parse output chocolatey. i'm thinking like

$package = "something want" try          {     $installcommand = "choco install $package"     iex $installcommand -erroraction stop -warningaction stop } catch {     #print error messages, i.e. $_.exception.message } 

i'm pretty new powershell, , i've tried figure out how 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 gives me

installing following packages: asdasdasdasdasdad installing accept licenses packages. asdasdasdasdasdad not installed. package not found source(s) listed.  if specified particular version , receiving message, possible package name exists version not. version: ""  source(s): "https://chocolatey.org/api/v2/"  chocolatey installed 0/1 package(s). 1 package(s) failed.  see log details (c:\programdata\chocolatey\logs\chocolatey.log). failures:   - asdasdasdasdasdad 

any tips? in advance, thank you!


i found code sample helps me on 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 reinstall, specify version install, or try upgrade.")         {          $forcedetected = $true;         }         write-host $_     }     end {          if($forcedetected -eq $true)         {             write-warning "force detected"             write-warning "do want re-run installer --force?"             # promt user y/n option , re-run testinstall -package $package --force          }         write-host "ending"     } }  testinstall -package ruby | test-demo sample 

this give me opportunity ask user if he/she wants re-run script --force parameter. problem output choco has lost colors. suggestions?

requires windows management framework 5:

register-packagesource -name chocolatey -provider psmodule -trusted -location http://chocolatey.org/api/v2/ -verbose 

i found here: https://serverfault.com/questions/633576/how-do-you-manually-set-powershells-oneget-repository-source-to-chocolatey

you should able run , verbose output:

get-package nodejs | install-package -verbose 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -