要检查模块是否存在,我尝试了以下方法:
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模块?
您可以使用以下ListAvailable
选项Get-Module
:
if (Get-Module -ListAvailable -Name SomeModule) {
Write-Host "Module exists"
}
else {
Write-Host "Module does not exist"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句