如何检查是否安装了 PowerShell 模块? [英] How do I check if a PowerShell module is installed?

查看:101
本文介绍了如何检查是否安装了 PowerShell 模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要检查模块是否存在,我尝试了以下操作:

To check if a module exists I have tried the following:

try {
    Import-Module SomeModule
    Write-Host "Module exists"
} 
catch {
    Write-Host "Module does not exist"
}

输出为:

Import-Module : The specified module 'SomeModule' was not loaded because no valid module file was found in any module directory.
At D:\keytalk\Software\Client\TestProjects\Export\test.ps1:2 char:5
+     Import-Module SomeModule
+     ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (SomeModule:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

Module exists

我确实得到了一个错误,但是没有抛出异常,所以我们看到最后Module exists,虽然SomeModule 不存在.

I do get an error, but no exception is thrown, so we see Module exists in the end, although SomeModule does not exist.

是否有一种好方法(最好不会产生错误)来检测系统上是否安装了 PowerShell 模块?

Is there a good way (preferably without generating an error) to detect if a PowerShell module is installed on the system?

推荐答案

你可以使用Get-ModuleListAvailable选项:

if (Get-Module -ListAvailable -Name SomeModule) {
    Write-Host "Module exists"
} 
else {
    Write-Host "Module does not exist"
}

这篇关于如何检查是否安装了 PowerShell 模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆