Snapins从子级加载,而不是在用于类的单独文件时从父级作用域加载

时间:2017-08-25 13:02:13

标签: class powershell pssnapin

这是一个奇怪的例子,但它解决了我的问题:

文件:Foo.psm1

class Foo
{
    [OBJECT] GetSite ()
    {
        return Get-SPWeb -Identity 'http://mysharepointsite.com';
    }
}

文件:Bar.psm1

Using MODULE ".\Foo.psm1";

class Bar : Foo
{
    Bar ()
    {
        Add-PSSnapin 'Microsoft.SharePoint.PowerShell';
    }
}

CONSOLE:

PS C:\test> Using MODULE "C:\Test\Bar.psm1";

PS C:\test> $myBar = [Bar]::new();

PS C:\test> $myBar.GetSite();
Get-SPWeb : The term 'Get-SPWeb' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
At C:\Test\Foo.psm1:5 char:16
+         return Get-SPWeb -Identity 'http://mysharepointsite.com ...
+                ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-SPWeb:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

HOWEVER ......

当这些类在同一个文件中时,它可以正常工作。

有什么建议吗?

0 个答案:

没有答案