要检查模块是否存在,我尝试了以下方法:
try {
Import-Module SomeModule
Write-Host "Module exists"
}
catch {
Write-Host "Module does not exist"
}
Run Code Online (Sandbox Code Playgroud)
输出是:
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
Run Code Online (Sandbox Code Playgroud)
我确实得到了一个错误,但没有抛出任何异常,所以我们Module exists最终看到,虽然SomeModule不存在.
是否有一种好方法(最好不会产生错误)来检测系统上是否安装了PowerShell模块?
powershell ×1