如何检查是否安装了PowerShell模块?

时间:2022-06-01 20:42:31

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"
}

The output is:

输出是:

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

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

我确实收到错误,但没有抛出任何异常,所以我们看到Module最终存在,尽管SomeModule不存在。

Does anyone know a good way (preferably without generating an error) to detect if a powershell module is installed on the system?

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

5 个解决方案

#1


62  

Found a solution. You can use the ListAvailable option of Get-Module:

找到了解决方案。您可以使用Get-Module的ListAvailable选项:

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

#2


18  

The ListAvailable option doesn't work for me. Instead this does:

ListAvailable选项对我不起作用。相反,这样做:

if (-not (Get-Module -Name "<moduleNameHere>")) {
    # module is not loaded
}

Or, to be more succinct:

或者,更简洁:

if (!(Get-Module "<moduleNameHere>")) {
    # module is not loaded
}

#3


6  

Just revisiting this as its somthing I just faced and there is some incorrect stuff in the answers (though its mentioned in the comments).

只是重新审视这个我刚才面对的事情,答案中有一些不正确的东西(虽然在评论中提到)。

First thing though, the original questions asks how to tell if a Powershell module is installed. We need to talk about the word installed! You don't install Powershell modules (not in the traditional way you install software anyway). Powershell modules are either available (i.e. they are on the powershell module path), or they are imported (they are imported into your session and you can call the functions contained). This is how to check your module path, in case you want to know where to store a module:

首先,原始问题询问如何判断是否安装了Powershell模块。我们需要谈谈安装这个词!您不安装Powershell模块(不是以传统的方式安装软件)。 Powershell模块可用(即它们位于powershell模块路径上),或者它们被导入(它们被导入到您的会话中,您可以调用包含的函数)。这是检查模块路径的方法,以防您想知道模块的存储位置:

$env:psmodulepath

I'd argue that its becoming common to use C:\Program Files\WindowsPowerShell\Modules; more often due to it being available to all users but if you want to lock down your modules to your own session, include them in your profile. C:\Users\%username%\Documents\WindowsPowerShell\Modules;

我认为使用C:\ Program Files \ WindowsPowerShell \ Modules正变得越来越普遍;更常见的原因是它可供所有用户使用,但如果您想将模块锁定到自己的会话中,请将它们包含在您的个人资料中。 C:\ Users \用户%的用户名%\文件\ WindowsPowerShell \模块;

Alright, back to the two states.

好吧,回到两个州。

Is the module available (using available to mean installed in the original question)?

该模块是否可用(使用可用于表示安装在原始问题中)?

Get-Module -Listavailable -Name <modulename>

This tells you if a module is available for import.

这会告诉您模块是否可用于导入。

Is the module imported? (im using this as the answer for the word 'exists' in the original question).

模块是否已导入? (我用这个作为原始问题中'存在'这个词的答案)。

Get-module -Name <modulename>

This will either return an empty load of nothing if the module is not imported, or a one line description of the module if it is. As ever on SO, try the commands above on your own modules.

如果未导入模块,这将返回空载空载,如果是,则返回模块的单行描述。与SO一样,在您自己的模块上尝试上面的命令。

#4


0  

IMHO, there is difference between checking if a module is: 1) installed, or 2) imported:

恕我直言,检查模块是否:1)安装,或2)导入:

To check if installed:

检查是否已安装:

$error.clear()
Import-Module "<ModuleName>" -ErrorAction SilentlyContinue
If($error){Write-Host 'Module is NOT installed'} 
Else{Write-Host 'Module is installed'}

To check if imported:

要检查是否已导入:

if ((Get-Module -Name "<ModuleName>")) {
   Write-Host "Module is already imported (i.e. its cmdlets are available to be used.)"
} else {
   Write-Warning "Module is NOT imported (must be installed before importing)."
}

#5


0  

A module could be in following states:

模块可以处于以下状态:

  • imported
  • 进口
  • available on disk (or local network)
  • 可在磁盘(或本地网络)上使用
  • available in online gallery
  • 可在在线图库中找到

If you just want to have the darn thing available in PowerShell session for use here is a function that will do that or exit out if it cannot get it done:

如果你只是想在PowerShell会话中使用这些可用的东西,那么这是一个可以执行此操作的函数,如果无法完成,则退出:

function Load-Module ($m) {
    # If module is imported say that and do nothing
    if (Get-Module | Where-Object {$_.Name -eq $m}) {
        write-host "Module $m is already imported."
    } else {
        # If module is not imported, but available on disk then import
        if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
            Import-Module $m -Verbose
        } else {
            # If module is not imported, not available on disk, but is in online gallery then install and import
            if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
                Install-Module -Name $m -Force -Verbose -Scope CurrentUser
                Import-Module $m -Verbose
            } else {
                # If module is not imported, not available and not in online gallery then abort
                write-host "Module $m not imported, not available and not in online gallery, exiting."
                EXIT 1
            }
        }
    }
}

Load-Module "ModuleName" # Use "PoshRSJob" to test it out

#1


62  

Found a solution. You can use the ListAvailable option of Get-Module:

找到了解决方案。您可以使用Get-Module的ListAvailable选项:

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

#2


18  

The ListAvailable option doesn't work for me. Instead this does:

ListAvailable选项对我不起作用。相反,这样做:

if (-not (Get-Module -Name "<moduleNameHere>")) {
    # module is not loaded
}

Or, to be more succinct:

或者,更简洁:

if (!(Get-Module "<moduleNameHere>")) {
    # module is not loaded
}

#3


6  

Just revisiting this as its somthing I just faced and there is some incorrect stuff in the answers (though its mentioned in the comments).

只是重新审视这个我刚才面对的事情,答案中有一些不正确的东西(虽然在评论中提到)。

First thing though, the original questions asks how to tell if a Powershell module is installed. We need to talk about the word installed! You don't install Powershell modules (not in the traditional way you install software anyway). Powershell modules are either available (i.e. they are on the powershell module path), or they are imported (they are imported into your session and you can call the functions contained). This is how to check your module path, in case you want to know where to store a module:

首先,原始问题询问如何判断是否安装了Powershell模块。我们需要谈谈安装这个词!您不安装Powershell模块(不是以传统的方式安装软件)。 Powershell模块可用(即它们位于powershell模块路径上),或者它们被导入(它们被导入到您的会话中,您可以调用包含的函数)。这是检查模块路径的方法,以防您想知道模块的存储位置:

$env:psmodulepath

I'd argue that its becoming common to use C:\Program Files\WindowsPowerShell\Modules; more often due to it being available to all users but if you want to lock down your modules to your own session, include them in your profile. C:\Users\%username%\Documents\WindowsPowerShell\Modules;

我认为使用C:\ Program Files \ WindowsPowerShell \ Modules正变得越来越普遍;更常见的原因是它可供所有用户使用,但如果您想将模块锁定到自己的会话中,请将它们包含在您的个人资料中。 C:\ Users \用户%的用户名%\文件\ WindowsPowerShell \模块;

Alright, back to the two states.

好吧,回到两个州。

Is the module available (using available to mean installed in the original question)?

该模块是否可用(使用可用于表示安装在原始问题中)?

Get-Module -Listavailable -Name <modulename>

This tells you if a module is available for import.

这会告诉您模块是否可用于导入。

Is the module imported? (im using this as the answer for the word 'exists' in the original question).

模块是否已导入? (我用这个作为原始问题中'存在'这个词的答案)。

Get-module -Name <modulename>

This will either return an empty load of nothing if the module is not imported, or a one line description of the module if it is. As ever on SO, try the commands above on your own modules.

如果未导入模块,这将返回空载空载,如果是,则返回模块的单行描述。与SO一样,在您自己的模块上尝试上面的命令。

#4


0  

IMHO, there is difference between checking if a module is: 1) installed, or 2) imported:

恕我直言,检查模块是否:1)安装,或2)导入:

To check if installed:

检查是否已安装:

$error.clear()
Import-Module "<ModuleName>" -ErrorAction SilentlyContinue
If($error){Write-Host 'Module is NOT installed'} 
Else{Write-Host 'Module is installed'}

To check if imported:

要检查是否已导入:

if ((Get-Module -Name "<ModuleName>")) {
   Write-Host "Module is already imported (i.e. its cmdlets are available to be used.)"
} else {
   Write-Warning "Module is NOT imported (must be installed before importing)."
}

#5


0  

A module could be in following states:

模块可以处于以下状态:

  • imported
  • 进口
  • available on disk (or local network)
  • 可在磁盘(或本地网络)上使用
  • available in online gallery
  • 可在在线图库中找到

If you just want to have the darn thing available in PowerShell session for use here is a function that will do that or exit out if it cannot get it done:

如果你只是想在PowerShell会话中使用这些可用的东西,那么这是一个可以执行此操作的函数,如果无法完成,则退出:

function Load-Module ($m) {
    # If module is imported say that and do nothing
    if (Get-Module | Where-Object {$_.Name -eq $m}) {
        write-host "Module $m is already imported."
    } else {
        # If module is not imported, but available on disk then import
        if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
            Import-Module $m -Verbose
        } else {
            # If module is not imported, not available on disk, but is in online gallery then install and import
            if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
                Install-Module -Name $m -Force -Verbose -Scope CurrentUser
                Import-Module $m -Verbose
            } else {
                # If module is not imported, not available and not in online gallery then abort
                write-host "Module $m not imported, not available and not in online gallery, exiting."
                EXIT 1
            }
        }
    }
}

Load-Module "ModuleName" # Use "PoshRSJob" to test it out