【问题标题】:In windows powershell what is the difference between $objGroup.add($objUser.Path)) and $objGroup.PSBase.Invoke('Add',$objUser.PSBase.Path)在 windows powershell 中 $objGroup.add($objUser.Path)) 和 $objGroup.PSBase.Invoke('Add',$objUser.PSBase.Path) 有什么区别
【发布时间】:2014-01-28 08:32:46
【问题描述】:

我想将用户添加到组中:

我发现有两种可能。 第一种方法:

$objGroup.add($objUser.Path))

其中$objGroup 是组的目录条目,$objUser 是要添加的用户的目录条目。

第二种方法:

$objGroup.PSBase.Invoke('Add',$objUser.PSBase.Path)

其中$objGroup 是组的目录条目,$objUser 是要添加的用户的目录条目。

这两种方法有什么区别?首选哪种方法?

【问题讨论】:

    标签: powershell active-directory invoke directoryservices active-directory-group


    【解决方案1】:

    PSBase 通常用于调用隐藏方法,如果我可以这么说的话。比如我用[ADSI]连接获取本地管理员组的一个实例,然后查看对象的成员,我看到的就是这个。

    PS H:\> $admGroup = [ADSI]("WinNT://./Administrators")
    PS H:\> $admGroup | gm
    
    
       TypeName: System.DirectoryServices.DirectoryEntry
    
    Name                        MemberType Definition
    ----                        ---------- ----------
    ConvertDNWithBinaryToString CodeMethod static string ConvertDNWithBinaryToString(psobject deInstance, psobject dnWit...
    ConvertLargeIntegerToInt64  CodeMethod static long ConvertLargeIntegerToInt64(psobject deInstance, psobject largeInt...
    Description                 Property   System.DirectoryServices.PropertyValueCollection Description {get;set;}
    groupType                   Property   System.DirectoryServices.PropertyValueCollection groupType {get;set;}
    Name                        Property   System.DirectoryServices.PropertyValueCollection Name {get;set;}
    objectSid                   Property   System.DirectoryServices.PropertyValueCollection objectSid {get;set;}
    

    您看不到 add() 方法,因为它没有在 PowerShell 中公开。这是PSBase 帮助您访问原始对象的地方。

    PS H:\> $objGroup.PSBase | gm
    
    
       TypeName: System.Management.Automation.PSMemberSet
    
    Name                      MemberType Definition
    ----                      ---------- ----------
    Disposed                  Event      System.EventHandler Disposed(System.Object, System.EventArgs)
    Close                     Method     void Close()
    CommitChanges             Method     void CommitChanges()
    CopyTo                    Method     adsi CopyTo(adsi newParent), adsi CopyTo(adsi newParent, string newName)
    CreateObjRef              Method     System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
    DeleteTree                Method     void DeleteTree()
    Dispose                   Method     void Dispose(), void IDisposable.Dispose()
    Equals                    Method     bool Equals(System.Object obj)
    GetHashCode               Method     int GetHashCode()
    GetLifetimeService        Method     System.Object GetLifetimeService()
    GetType                   Method     type GetType()
    InitializeLifetimeService Method     System.Object InitializeLifetimeService()
    Invoke                    Method     System.Object Invoke(string methodName, Params System.Object[] args)
    InvokeGet                 Method     System.Object InvokeGet(string propertyName)
    InvokeSet                 Method     void InvokeSet(string propertyName, Params System.Object[] args)
    MoveTo                    Method     void MoveTo(adsi newParent), void MoveTo(adsi newParent, string newName)
    RefreshCache              Method     void RefreshCache(), void RefreshCache(string[] propertyNames)
    Rename                    Method     void Rename(string newName)
    ToString                  Method     string ToString()
    AuthenticationType        Property   System.DirectoryServices.AuthenticationTypes AuthenticationType {get;set;}
    Children                  Property   System.DirectoryServices.DirectoryEntries Children {get;}
    Container                 Property   System.ComponentModel.IContainer Container {get;}
    Guid                      Property   guid Guid {get;}
    Name                      Property   string Name {get;}
    NativeGuid                Property   string NativeGuid {get;}
    NativeObject              Property   System.Object NativeObject {get;}
    ObjectSecurity            Property   System.DirectoryServices.ActiveDirectorySecurity ObjectSecurity {get;set;}
    Options                   Property   System.DirectoryServices.DirectoryEntryConfiguration Options {get;}
    Parent                    Property   adsi Parent {get;}
    Password                  Property   string Password {set;}
    Path                      Property   string Path {get;set;}
    Properties                Property   System.DirectoryServices.PropertyCollection Properties {get;}
    SchemaClassName           Property   string SchemaClassName {get;}
    SchemaEntry               Property   adsi SchemaEntry {get;}
    Site                      Property   System.ComponentModel.ISite Site {get;set;}
    UsePropertyCache          Property   bool UsePropertyCache {get;set;}
    Username                  Property   string Username {get;set;}
    

    我们可以使用Invoke() 方法在这个对象上调用add 方法。您只需将Add 作为方法名和用户名作为参数传递。

    因此,总而言之,PSBase 概念应该根据上下文使用。

    【讨论】:

    • 我知道 psbase 将有助于访问原始对象。但我可以直接使用 add 属性。那么是否需要使用 psbase 对象呢?
    • 就像我说的,它是上下文相关的。在某些地方,如果没有它,它可能会起作用。但是,对于某些对象方法,您需要使用 PSBase。我会用一个具体的例子来更新我的答案。
    【解决方案2】:

    引用this answer:

    当您使用[ADSI] type accelerator 创建DirectoryEntry 对象时,您得到的实际上并不是DirectoryEntry 对象,而是一个带有精细PowerShell 包装器的DirectoryEntry。通过访问这个包装对象的psbase,您可以访问下面的原始DirectoryEntry 对象。

    .Add('..') 实际上不是DirectoryEntry 函数,它是由类型加速器映射到.psbase.Invoke('Add', '..') 的函数。建议您使用.psbase.Invoke() 的人担心向后和向前兼容性等等。使用哪个完全取决于您。

    创建类型加速器是为了加快处理基础对象的速度,包括创建.Add() 等便利。 基本上,如果您不打算使用类型加速器功能,那么使用类型加速器根本没有意义。不妨多走一步,创建自己的 DE 对象:

    $de = New-Object System.DirectoryServices.DirectoryEntry('WinNT://./Administrators,group')
    $de.Invoke('Add', '...')
    $de.Close()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      相关资源
      最近更新 更多