How to check if PowerShell snap-in is already loaded before calling Add-PSSnapin

前端 未结 5 1455
小鲜肉
小鲜肉 2020-12-13 01:15

I have a group of PowerShell scripts that sometimes get run together, sometimes one at a time. Each of the scripts requires that a certain snap-in be loaded.

Right n

5条回答
  •  醉酒成梦
    2020-12-13 02:05

    You should be able to do it with something like this, where you query for the Snapin but tell PowerShell not to error out if it cannot find it:

    if ( (Get-PSSnapin -Name MySnapin -ErrorAction SilentlyContinue) -eq $null )
    {
        Add-PsSnapin MySnapin
    }
    

提交回复
热议问题