如何在调用 Add-PSSnapin 之前检查 PowerShell 管理单元是否已加载 [英] How to check if PowerShell snap-in is already loaded before calling Add-PSSnapin

查看:41
本文介绍了如何在调用 Add-PSSnapin 之前检查 PowerShell 管理单元是否已加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组 PowerShell 脚本,它们有时一起运行,有时一次一个.每个脚本都需要加载特定的管理单元.

现在每个脚本都在开头调用 Add-PSSnapin XYZ.

现在,如果我背靠背运行多个脚本,后续脚本会抛出:

<块引用>

无法添加 Windows PowerShell 管理单元 XYZ,因为它已被添加.验证管理单元的名称,然后重试.

如何在调用 Add-PSSnapin 之前检查每个脚本以查看管理单元是否已加载?

解决方案

你应该可以用这样的方式来做,你可以查询 Snapin,但告诉 PowerShell 如果它找不到它就不要出错:

>

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

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 now each script is calling Add-PSSnapin XYZ at the beginning.

Now if I run multiple scripts back-to-back the subsequent scripts throw:

Cannot add Windows PowerShell snap-in XYZ because it is alerady added. Verify the name of the snap-in and try again.

How can I have each script check to see if the snap-in is already loaded before calling Add-PSSnapin?

解决方案

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
}

这篇关于如何在调用 Add-PSSnapin 之前检查 PowerShell 管理单元是否已加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆