tech.guitarrapc.cóm

Technical updates

PowerShell で Chocolatey をリモートコンピュータにインストールしたい

chocolatey 便利ですよね、使っていらっしゃる方も多いかと思います。

私も見つけてからずっと愛用しています。

今回は、Invoke-Commandvalentiaなど、リモートPCでのchocolatey インストールについて考えてみたいと思います。

目次

chocolatey って何

一応簡単に説明しておきましょう。

公式HPより

Let's get Chocolatey!

Chocolatey NuGet is a Machine Package Manager, somewhat like apt-get, but built with Windows in mind.

また、公式wiki では以下のように紹介されています。

Chocolatey is a global PowerShell execution engine using the NuGet packaging infrastructure. Think of it as the ultimate automation tool for Windows.

Chocolatey is like apt-get, but built with Windows in mind (there are differences and limitations). For those unfamiliar with apt/debian, think about chocolatey as a global silent installer for applications and tools. It can also do configuration tasks and anything that you can do with PowerShell. The power you hold with a tool like chocolatey is only limited by your imagination!

You can develop your tools and applications with NuGet, and release them with chocolatey!

But chocolatey is not just for .NET tools. It's for nearly any windows application/tool!

さらに、FAQs では

What is chocolatey?

Chocolatey is kind of like apt-get, but for Windows (with windows comes limitations). It is a machine level package manager that is built on top of nuget command line and the nuget infrastructure. More behind the name

"Okay, machine package manager, that's nice. What does that mean though?" It means you can simply install software with a few keystrokes and go get coffee while your co-workers are downloading and running an install manually (and I do mean something like an MSI).

How about updates? Wouldn't it be nice to update nearly everything on your machine with a few simple keystrokes? We think so, too. Chocolatey does that.

日本語でいって

あ、はい。

Chocolatey は、 YumPortsapt-getなど、Linux/Unixにあるパッケージ管理システムのWindows版です。

Visual Studioなどでおなじみの .NET Frameworkのライブラリ、コンポーネント向けパッケージ管理ツールNugetを基盤として、PowerShellでアプリケーションのインストールや依存解決をします。

マウスでファイルを探してインストール作業を眺める簡単なお仕事はおしまいです。同僚がそんなことをしている間に、少しのコマンド入力をしてコーヒーを取りに行ったらインストールが終わってる。素敵じゃないですか?

まさにPowerShellが目指すところの Automationの一つのあり方を示すモノ、それがchocolatey です。

Chocolatey 開発 に協力したい方

プロジェクトはGitHubで開発されており、誰でもソースを閲覧、Pull Req 可能です。

GitHub - chocolatey / chocolatey

某人の Pull Reqとかありますし

本題

本題です。

chocolatey は、インストールに以下のコマンドを cmd で入力してねと言っています。

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin

内容は至って簡単で、ダウンロードしたinstall.ps1の内容を Invoke-Expressionで実行しているだけです。で、環境変数PATHにchocolateyフォルダを差し込みしています。

そう、cmdで。

cmdだと困ることもある

できるはできますが、まぁなんというかアレです。

PowerShellで書き直してしまいます。

# installation
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')
    
# Environment Set up
$envChocolatey = "%systemdrive%\Chocolatey\bin;"
$envChocolateyRoot = Join-Path (Get-Item ([System.Environment]::SystemDirectory)).Root "Chocolatey\bin"
if (-not($env:Path -like "*$envChocolatey*") -or -not($env:Path -like "*$envChocolateyRoot*"))
{
    $currentEnvPath = $env:Path
    $newEnvPath = $env:Path + "$envChocolatey"
    [System.Environment]::SetEnvironmentVariable("Path",$newEnvPath,"user")
}

ワンライナーもできますが、ほげ

Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1');$envChocolatey = "%systemdrive%\Chocolatey\bin;";$envChocolateyRoot = Join-Path (Get-Item ([System.Environment]::SystemDirectory)).Root "Chocolatey\bin";if (-not($env:Path -like "*$envChocolatey*") -or -not($env:Path -like "*$envChocolateyRoot*")){$currentEnvPath = $env:Path;$newEnvPath = $env:Path + "$envChocolatey";[System.Environment]::SetEnvironmentVariable("Path",$newEnvPath,"user")}

正直、chocolatey インストール時にPATHには追加されています。なので不要と思いましたが、まぁ一応従っておきましょう。

PATH environment variable does not have 'C:\Chocolatey\bin' in it. Adding.

リモートにインストール

これで、 PowerShell Remoting でchocolatey をまとめてインストールできます。

もし、環境変数が不要でInvoke-Expressionだけなら、本当に楽ですが、一応です。

Invoke-Command ならこのようになるでしょう。 (ローカルコンピュータに上記のスクリプトをinstallchocolatey.ps1として保存した状態です。)*1

Invoke-Command -ComputerName hogehoge -Credential $Credential -FilePath .\installchocolatey.ps1

valentiaならもっと簡単です。

valea hogehoge .\installchocolatey.ps1

valentia で 100台ほどに一斉実行してみたところ 1分で終わりました。コーヒーもとってこれないほどあっという間じゃないですかー、やーだーー。

*1:リモートコンピュータにはスクリプトは不要です