【问题标题】:Import Local Group from remote server via Powershell通过 Powershell 从远程服务器导入本地组
【发布时间】:2015-03-02 18:51:28
【问题描述】:

我正在研究使用 Powershell 将安全设置从一台服务器复制到另一台服务器的最简单方法,我很好奇是否可以导入整个组,包括它的 DescriptionMembers 属性?

以下是我目前拥有的脚本。看来我可以使用ADSI 适配器访问远程服务器上的本地组,但是Create 命令会出现以下错误消息

使用“2”参数调用“创建”的异常:“类型不匹配。(来自 HRESULT 的异常:0x80020005 (DISP_E_TYPEMISMATCH))” 在 \prdhilfs02\install\Monet\ServerUpgrade\DEVHILWB119\Scripts\LocalUsersAndGroups.ps1:25 char:1+ $objCreate = $cn.Create("Group", $objRemote)

$computerName = "DEVWB89"
$objRemote = [ADSI]("WinNT://$computerName/$groupName")

$cn = [ADSI]"WinNT://localhost"
$cn.Create("Group", $objRemote)

编辑

所以我可以使用下面的脚本来完成我想要的。我可以使用远程服务器中的组名称和描述以及组信息。但是,有没有办法使用 Powershell 将 System.DirectoryServices.DirectoryEntry 对象及其所有属性简单地添加到本地计算机?另外,另一个缺点是我必须为组的用户对域进行硬编码。

$cn = [ADSI]"WinNT://localhost"
$computerName = "DEVWB89"

foreach($groupName in $groupArray)
{
    $objRemote = [ADSI]("WinNT://$computerName/$groupName")

    $objGroup = $cn.Create("Group", $($objRemote.Name))
    $objGroup.setinfo()

    $objGroup.description = $objGroup.Description
    $objGroup.setinfo()

    $Members = @($objRemote.psbase.Invoke("Members"))
    $Members | ForEach-Object {$MemberNames += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null) + ",";}

    $tempArray = $MemberNames -split ","

    foreach($member in $tempArray)
    {
        $objGroup.Add("WinNT://SYMETRA/$member, user")
    }
}

【问题讨论】:

  • 开始 Create() 可能需要一个字符串,而不是 System.DirectoryServices.DirectoryEntry,这就是 $objRemote
  • 是的,我想通了。你知道是否可以使用System.DirectoryServices.DirectoryEntry“对象”来创建一个本地组。如果可能和/或语法是什么?
  • $cn.Create("Group", $objRemote.Name) 可能会组成该组,但不会包含您要查找的详细信息。 $objRemote.Description 有描述,我不确定成员列表在哪里或是否可用。
  • 太棒了,我会继续寻找。谢谢!
  • blogs.technet.com/b/heyscriptingguy/archive/2013/10/27/… 如果有帮助的话。如果机器是域成员,这可以解决您可能遇到的一些问题

标签: powershell


【解决方案1】:

这将列出组的所有成员:

$Members = @($objRemote.psbase.Invoke("Members"))
$Members | ForEach-Object {$MemberNames += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null);}
$MemberNames

更多有用信息:

PS C:\Users\YourUser\Desktop> $objAdmin = [ADSI]("WinNT://localhost/Administrator")
PS C:\Users\YourUser\Desktop> $objAdmin | gm


   TypeName: System.DirectoryServices.DirectoryEntry

Name                        MemberType Definition
----                        ---------- ----------
ConvertDNWithBinaryToString CodeMethod static string ConvertDNWithBinaryToString(psobject deInstance, psobject dnWithBinaryIns...
ConvertLargeIntegerToInt64  CodeMethod static long ConvertLargeIntegerToInt64(psobject deInstance, psobject largeIntegerInstance)
AutoUnlockInterval          Property   System.DirectoryServices.PropertyValueCollection AutoUnlockInterval {get;set;}
BadPasswordAttempts         Property   System.DirectoryServices.PropertyValueCollection BadPasswordAttempts {get;set;}
Description                 Property   System.DirectoryServices.PropertyValueCollection Description {get;set;}
FullName                    Property   System.DirectoryServices.PropertyValueCollection FullName {get;set;}
HomeDirDrive                Property   System.DirectoryServices.PropertyValueCollection HomeDirDrive {get;set;}
HomeDirectory               Property   System.DirectoryServices.PropertyValueCollection HomeDirectory {get;set;}
LastLogin                   Property   System.DirectoryServices.PropertyValueCollection LastLogin {get;set;}
LockoutObservationInterval  Property   System.DirectoryServices.PropertyValueCollection LockoutObservationInterval {get;set;}
LoginHours                  Property   System.DirectoryServices.PropertyValueCollection LoginHours {get;set;}
LoginScript                 Property   System.DirectoryServices.PropertyValueCollection LoginScript {get;set;}
MaxBadPasswordsAllowed      Property   System.DirectoryServices.PropertyValueCollection MaxBadPasswordsAllowed {get;set;}
MaxPasswordAge              Property   System.DirectoryServices.PropertyValueCollection MaxPasswordAge {get;set;}
MaxStorage                  Property   System.DirectoryServices.PropertyValueCollection MaxStorage {get;set;}
MinPasswordAge              Property   System.DirectoryServices.PropertyValueCollection MinPasswordAge {get;set;}
MinPasswordLength           Property   System.DirectoryServices.PropertyValueCollection MinPasswordLength {get;set;}
Name                        Property   System.DirectoryServices.PropertyValueCollection Name {get;set;}
objectSid                   Property   System.DirectoryServices.PropertyValueCollection objectSid {get;set;}
Parameters                  Property   System.DirectoryServices.PropertyValueCollection Parameters {get;set;}
PasswordAge                 Property   System.DirectoryServices.PropertyValueCollection PasswordAge {get;set;}
PasswordExpired             Property   System.DirectoryServices.PropertyValueCollection PasswordExpired {get;set;}
PasswordHistoryLength       Property   System.DirectoryServices.PropertyValueCollection PasswordHistoryLength {get;set;}
PrimaryGroupID              Property   System.DirectoryServices.PropertyValueCollection PrimaryGroupID {get;set;}
Profile                     Property   System.DirectoryServices.PropertyValueCollection Profile {get;set;}
UserFlags                   Property   System.DirectoryServices.PropertyValueCollection UserFlags {get;set;}


PS C:\Users\YourUser\Desktop> $Members[0].GetType().InvokeMember("FullName", "GetProperty", $null, $Members[0], $null)
Exception calling "InvokeMember" with "5" argument(s): "The specified domain either does not exist or could not be contacted.
"
At line:1 char:1
+ $Members[0].GetType().InvokeMember("FullName", "GetProperty", $null, $Members[0] ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : COMException

PS C:\Users\YourUser\Desktop> $Members[0].GetType().InvokeMember("PasswordAge", "GetProperty", $null, $Members[0], $null)
PS C:\Users\YourUser\Desktop> $Members[0].GetType().InvokeMember("UserFlags", "GetProperty", $null, $Members[0], $null)

【讨论】:

  • 这只是创建了一个数组,其中包含一堆读取System.__ComObject 的条目。看起来它应该是数组大小,但是我似乎无法访问数组项的任何属性。例如,在foreach 循环中,此条件返回falseif ($_.($member.name))
  • 没错。我认为目的是复制组的属性。您还需要组成员的属性吗?
  • 我的错,我误解了循环中发生的事情。这确实会返回一个名称列表,我可以使用它来添加到组中。但是,我也想获取每个名称的域。我一直在寻找 System.__ComObject 的属性列表,但由于某种原因我可以在网上找到任何东西。如果我循环遍历$MembersGet-Member 似乎也没有返回任何内容......有什么想法吗?
  • 您必须单独绑定到每个用户才能显示他们的属性 AFAIK。没有办法枚举它们的属性:stackoverflow.com/questions/18538840/…
  • 我添加了一些可能对您有帮助的额外信息。您可以查看您是否专门绑定到用户,您可以枚举它的所有属性,但是当您通过组列出它们时,它们看起来并不像所有的都公开。有些返回错误,有些则不返回任何数据。
猜你喜欢
  • 1970-01-01
  • 2021-06-25
  • 2021-06-15
  • 2017-02-03
  • 1970-01-01
  • 2013-03-31
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多