2011-06-03 23 views
4

我有这样的功能:从功能添加,pssnapin一个模块不能正常工作(范围问题?)

function start-sqlsnap 
{ 
       add-pssnapin SqlServerCmdletSnapin100 
} 

无论用于加载功能的方法,得到-pssnapin将显示该管理单元加载。但是:

  1. 如果壳粘贴,则各功能(如调用-SQLCMD)被识别
  2. 如果点从文件来源,所述功能被识别
  3. 如果放置在一个PSM1文件(内模块文件夹,在它自己的文件夹具有相同的名称作为PSM1文件),并装有进口模块,该管理单元的具体功能并不认可 - 但运行Get-pssnapin将显示模块加载。

错误:

The term 'invoke-sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check t 
he spelling of the name, or if a path was included, verify that the path is correct and try again. 
At line:1 char:14 
+ invoke-sqlcmd <<<< 
    + CategoryInfo   : ObjectNotFound: (invoke-sqlcmd:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 

我试着用不同的管理单元同样的事情:Microsoft.EnterpriseManagement.OperationsManager.Client并有完全相同的结果。 我在2008 R2上运行PowerShell 2。

这是一个已知的bug或功能?我能不能解决这个问题?

+0

我只是想这和它的工作对我来说是什么你得到'Get-Module'吗?我得到: ModuleType名称ExportedCommands ---------- ---- ---------------- 脚本SQLPS {调用-PolicyEvaluation,调用,SQLCMD } – PeskyGnat 2011-06-03 18:08:09

回答

2

,而不是添加功能模块文件,如果你只需要添加的单行:

add-pssnapin SqlServerCmdletSnapin100 

我试过了,它似乎工作。

+0

这对我来说是诀窍。谢谢。 – Winfred 2011-06-03 19:36:38

0

只添加一行到你的脚本可以解决,如果你的功能确实只有一行。但是如果你的函数有很多行,你需要检查一些需求或者根据安装的SQL版本加载不同版本的PsSnapin,它可能会有问题。

在这种情况下,该解决方案是创建一个PowerShell脚本文件(名为.ps1),并在该文件中移动你的功能。当你调用一个函数时,所有包含的模块/ pssnapins将被加载。

充电pssnapins和模块不能从外面:

Import-Module "MyCustomModule" #(.psm1) 

充电pssnapins和模块可以从外面:

Import-Module "MyCustomModule.ps1