Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 当类使用单独的文件时,从子级加载的管理单元不在父级范围内_Class_Powershell_Pssnapin - Fatal编程技术网

Class 当类使用单独的文件时,从子级加载的管理单元不在父级范围内

Class 当类使用单独的文件时,从子级加载的管理单元不在父级范围内,class,powershell,pssnapin,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 '

这是一个奇怪的例子,但它让我的问题得到了理解:

文件: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';
    }
}
控制台:

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
但是…

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


有什么建议吗?

管理单元很奇怪。。它们只是没有将其纳入PS的现代概念中。在显式导出某些函数的模块中加载管理单元时,也有类似的行为。我只想以不同的方式加载它,就像在您的
$Profile
中一样。管理单元很奇怪。。它们只是没有将其纳入PS的现代概念中。在显式导出某些函数的模块中加载管理单元时,也有类似的行为。我只想以不同的方式加载它,就像在您的
$Profile
中一样。