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

前端 未结 5 1415
小鲜肉
小鲜肉 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 01:49

    I tried @ScottSaad's code sample but it didn't work for me. I haven't found out exactly why but the check was unreliable, sometimes succeeding and sometimes not. I found that using a Where-Object filtering on the Name property worked better:

    if ((Get-PSSnapin | ? { $_.Name -eq $SnapinName }) -eq $null) {
        Add-PSSnapin $SnapinName 
    }
    

    Code courtesy of this.

提交回复
热议问题