2016-07-25 9 views
0

ChocolateyパッケージをPowerShellにインストールするためのスクリプトを作成しようとしていました。これは正常に動作しますが、私はChocolateyからの出力を解析したいと思います。私が考えているのは、何かのようなものです。ChocolateyパッケージをインストールするときにPowerShellで試してみる

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

私は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パラメータ唯一の問題は、チョコからの出力が色を失っていることです。助言がありますか?

答えて

0

後いくつかのテスト、私はこれを作った。それは開発中ですが、出力を解析して、最後にインストールの概要を印刷できます。

気軽にコメントしてください。

https://github.com/bpjohannessen/ChocoPower

(私は明日の答えとしてこれをマークします)

1

は、Windows管理フレームワーク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 
+0

これは確かに私が考える何かです。助けてくれてありがとう! –

関連する問題