Get-InstalledModule と Get-Module の違い
TL;DR
-
Get-InstalledModuleとGet-Moduleの違いを理解し隊 - インストールされているモジュール (
Get-InstalledModule) が動的にインポート (Import-Module) されるので、Get-Moduleはあんまり大事じゃない - まぁ、コマンド名そのままではあるんですが、メモまでに
はじめに
完全に自分向けのメモであり、まぁ言われてみれば当たり前の話を書いているのですが、意外と、あ、そういう動きをするのね、というのがわかりやすく紹介できると思ったので書いておきます。
結論としては Get-Module はあんまり大事じゃなくて、Get-InstalledModule が大事、という話を書いてます。
Get-InstalledModule と Get-Module の違い
Get-InstalledModule はシステムに install されている module を列挙します。(当たり前の話を書いてるんですが、はい)
以下は自分のマシンでの例で、一部出力を省略していますが、79 個の module が install されていることがわかります。
> Get-InstalledModule
Version Name Repository Description
------- ---- ---------- -----------
9.2.0 Az PSGallery Microsoft Azure PowerShell - Cmdlets to …
2.10.4 Az.Accounts PSGallery Microsoft Azure PowerShell - Accounts cr…
2.0.0 Az.Advisor PSGallery Microsoft Azure PowerShell: Advisor cmdl…
<snip>
> (Get-InstalledModule).Count
79
次に Get-Module ですが、PowerShell を起動した時点で読み込まれている module を列挙します。
> Get-Module
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Manifest 7.0.0.0 Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-Item, Clear-It…
Script 2.2.6 PSReadLine {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …
ここで、適当に Get-Module に入っていなさそうな cmdlet を実行してみます。
> Get-AzContext
Name Account SubscriptionName Environment TenantId
---- ------- ---------------- ----------- --------
xxxxxxxxxxxx (xxxxxxxx-xxxx-xxxx-... xxxxxxxx@microso... xxxxxxxx AzureCloud xxxxxxxx-xxxx-x...
で、再度 Get-Module を実行してみると、先ほどの cmdlet が含まれている module が自動的に追加されています。
つまり、Get-Module は該当の PSSession で読み込まれている module を列挙するので、Get-InstalledModule とは別物ということですね。
> Get-Module
ModuleType Version PreRelease Name ExportedCommands
---------- ------- ---------- ---- ----------------
Script 2.10.4 Az.Accounts {Add-AzEnvironment, Clear-AzConfig, Clear-AzConte…
Manifest 7.0.0.0 Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-Item, Clear-It…
Manifest 7.0.0.0 Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Ob…
Script 1.4.8.1 PackageManagement {Find-Package, Find-PackageProvider, Get-Package,…
Script 2.2.5 PowerShellGet {Find-Command, Find-DscResource, Find-Module, Fin…
Script 2.2.6 PSReadLine {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …
なのでまぁ、Get-Module がどうであるかはどうでもよい話であって、Get-InstalledModule を見て module が install されているかどうかを確認すればよいということになるかと思います。
Install-Module してない module は Import-Module できない
これも当たり前の話なのですが、ちょうどいい log が取れたので参考までに張り付けておきます。
ThreadJob という module があるのですが、既定では入っていません。
で、それをおもむろに Import-Module してみると、エラーを吐きます。
> Import-Module ThreadJob
Import-Module : The specified module 'ThreadJob' was not loaded because no valid module file was found in any module
directory.
At line:1 char:1
+ Import-Module ThreadJob
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (ThreadJob:String) [Import-Module], FileNotFoundException
+ FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand
無いなら install すればいいだけなので Install-Module してみます。
-Scope CurrentUser を付ければ、管理者権限無しでも動くはずです。
> Install-Module -Name ThreadJob -Scope CurrentUser -Repository PSGallery
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): y
ちなみにこの時点ではまだ Get-Module の一覧には出てこないみたいですね。
> Get-Module
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 2.0.0 PSReadLine {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...
で、install が終わっているはずなので Import-Module してから Get-Module すればちゃんと ThreadJob が表示されています、よかったね!
> Import-Module ThreadJob
> Get-Module
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Script 2.0.0 PSReadLine {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...
Binary 2.0.3 ThreadJob Start-ThreadJob
参考
-
PowerShell で Linux の shell っぽいキーバインドを利用する
PSReadLineはおすすめの module の一つですのでぜひ使ってみてください!
Update log
- 全体的に
# (見出し1)から## (見出し2)に変更 - 2024/07/14 - TL;DR とかの追加 - 2024/07/14
Discussion