只有累積,沒有奇蹟

2019年2月10日 星期日

[Chocolatey] Windows 套件管理工具 - Chocolatey 初體驗

前言 
之前在外面上課時就聽過講師介紹 chocolatey 套件管理工具,但當時趕專案上線就沒有再深入研究,直到最近要在公司筆電安裝軟體才想起 Windows 有此神奇的套件管理神器,今天就針對此神器筆記安裝與功能介紹,避免初老症狀病發還要再次請教 google 大神。 

Chocolatey 是什麼 可以吃嗎 ?
在過去在 Windows 安裝軟體應用程式時,需要下載安裝檔與使用其軟體安裝流程步驟等各種 GUI 介面,如果需要安裝多個軟體時使用上很繁瑣,Chocolatey 是在 Windows 上的套件管理工具,它結合 Nuget 基礎服務與 powershell 指令,提供快速安裝應用程式與需要工具的服務,主要是將安裝軟體的內容封裝到一個安裝包中,在 Chocolatey 中只需要一條簡單的 command line,就可以完成搜尋、安裝、更新、解安裝等操作,詳細更多說明可以參考 官方文章 What is Chocolatey?

安裝 Chocolatey
安裝方式很簡單,可以透過下列兩種方式安裝 chocolatey
使用 cmd.exe
在命令提示字元執行以下指令
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
使用 Powershell
透過 powershell 安裝前,須先透過 Get-ExecutionPolicy 得知目前系統的指令檔執行限制
Get-ExecutionPolicy
如果顯示的是 Restricted,則需要執行下列指令 變更 Windows PowerShell 執行原則的使用者喜好設定
會跳出提示是否要變更執行原則提示,如下圖所示
Set-ExecutionPolicy AllSigned
or
Set-ExecutionPolicy Bypass -Scope Process

執行原則變更
執行原則有助於防範您不信任的指令碼。如果變更執行原則,可能會使您接觸到 about_Execution_Policies 說明主題 (網址為
https:/go.microsoft.com/fwlink/?LinkID=135170) 中所述的安全性風險。您要變更執行原則嗎?
[Y] 是(Y)  [A] 全部皆是(A)  [N] 否(N)  [L] 全部皆否(L)  [S] 暫停(S)  [?] 說明 (預設值為 "N"):
設定完畢執行原則後,接著輸入下列指令安裝 chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
確認安裝完成
安裝完畢之後,可以透過 choco 指令確認是否安裝完成 
PS D:\> choco
Chocolatey v0.10.11
Please run 'choco -?' or 'choco <command> -?' for help menu.

下載 Package 
截至目前為止(2019/1) Chocolatey 有提供 6千多種 community package 提供快速安裝
詳細清單傳送門 : Package list
安裝完成之後,我們來試著透過 chocolatey 在筆電安裝 winrar 軟體,步驟如下
Step 1 : 在 powershell 輸入下列指令
choco install winrar
Step 2 : chocolatey 會自動下載 winrar 軟體,完成下載後會詢問是否要 run install script,選擇 yes 即會自動安裝 winrar 軟體,以下為安裝時的過程
PS D:\> choco install winrar
Chocolatey v0.10.11
Installing the following packages:
winrar
By installing you accept licenses for the packages.
Progress: Downloading winrar 5.61... 100%

winrar v5.61 [Approved]
winrar package files install completed. Performing other installation steps.
The package winrar wants to run 'chocolateyInstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint): y

Downloading winrar 64 bit
  from 'https://www.rarlab.com/rar/winrar-x64-561tc.exe'
Progress: 100% - Completed download of C:\Users\marcustung\AppData\Local\Temp\chocolatey\winrar\5.61\winrar-x64-561tc.exe (3.29 MB).
Download of winrar-x64-561tc.exe (3.29 MB) completed.
Hashes match.
Installing winrar...
winrar has been installed.
 The install of winrar was successful.
  Software installed as 'exe', install location is likely default.

Chocolatey installed 1/1 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

心得
透過一行指令就迅速地完成安裝 winrar 軟體,過程中不需要像一般軟體安裝一樣無止境地按下一步,如果連是否同意 y/n 都不想按下的話,直接在 install appName -y 就可以省去此步驟,實在是太神奇拉!

參考
chocolatey.org
Windows下的包管理器Chocolatey

0 意見:

張貼留言

Copyright © m@rcus 學習筆記 | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com