为了检查一个模块是否存在,我尝试了以下方法:
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”不存在。
是否有一种好的方法(最好不会生成错误)来检测PowerShell模块是否已在系统上安装?