0
如何编写应检查Add-PSSnapin的代码,如果该代码不存在,则检查Import-Module,如果该代码也不存在,则退出该脚本。我编写了下面的代码,但在使用它时出现内存溢出问题。PowerShell中的导入模块和Add-PSSnapin
cls
Function GetModule {
$ErrorActionPreference = 'Stop'
if(-not(Get-Module -Name VMware.VimAutomation.Core))
{
Import-Module VMware.VimAutomation.Core
}
Elseif (-not(Get-PSSnapin -Name VMware.VimAutomation.Core))
{
Add-PSSnapin VMware.VimAutomation.Core
}
Else {
Write-Host "VMware PowerCLI Modules are NOT INSTALLED on this machine !"
Exit
}
}
GetModule
谢谢@Avshalom一起! 如果我想退出脚本,如果没有找到/安装在机器上的2个模块?那么在第二个catch中添加“EXIT”就可以解决这个问题? –
是的,但它会自动退出,您可以在最后一个catch块的末尾添加'return',更新我的答案。 – Avshalom