2015-03-02 110 views
0

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

下面是我目前使用的脚本。看来,我可以用下面的错误信息

异常调用“创建”与“2”参数(S)访问本地组使用ADSI适配器的远程服务器上,但是Create命令炸弹:“类型(“HRESULT异常:0x80020005(DISP_E_TYPEMISMATCH)”) At \ 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") 
    } 
} 
+0

要启动的Create()可能想要一个字符串,而不是一个'系统。 DirectoryServices.DirectoryEntry'这是什么'$ objRemote' – Matt 2015-03-02 19:14:52

+0

是的,想通了。你知道是否有可能使用System.DirectoryServices.DirectoryEntry对象来创建一个本地组..如果可能和/或语法是什么? – NealR 2015-03-02 19:49:17

+0

'$ cn.Create(“Group”,$ objRemote.Name)'可能会成为组,但不会有你正在寻找的细节。 '$ objRemote.Description'有说明,我不确定是否在会员名单或可用的位置。 – Matt 2015-03-02 19:51:37

回答

0

这会列出该组的所有成员:

$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) 
+0

这只是创建一个数组,其中包含一些读取“System .__ ComObject”的条目。它看起来像是它应该是数组大小,但我似乎无法访问数组项的任何属性。例如,在'foreach'循环中,这个条件返回'false':'if($ _。($ member.name))' – NealR 2015-03-02 22:18:50

+0

没错。我认为其目的是复制该组的属性。你还需要该组成员的属性? – 2015-03-02 22:21:04

+0

我的不好,我误解了循环中发生了什么。这确实会返回一个名称列表,我可以使用它来添加到组中。不过,我想为每个名称获取域名。我一直在寻找System .__ ComObject的属性列表,但由于某种原因我可以在网上找到任何东西。 'Get-Member'似乎没有返回任何东西,如果我通过'$ Members'循环...任何想法? – NealR 2015-03-02 22:25:59

相关问题