我试图编译一个有权访问我们组织中的一组计算机/服务器的组列表,关于MSDN许可证报告..: – /
把我的工作放在本地组成员关于Hey Scripting Guy的博客文章中: http : //blogs.technet.com/b/heyscriptingguy/archive/2013/10/27/the-admin-s-first-steps-local-组membership.aspx
但是,我需要validation每个服务器上的多个组中的成员。 而这里来的尴尬的东西,我不记得之前看到:当添加额外的参数的函数的参数部分,它无法得到任何组成员。
function Test-LocalSecurityGroups { [CmdletBinding()] param( [parameter( ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [string[]]$ComputerName = $env:COMPUTERNAME, [parameter( ValueFromPipelineByPropertyName=$true)] [string[]]$Group ) BEGIN { Add-Type -AssemblyName System.DirectoryServices.AccountManagement $ctype = [System.DirectoryServices.AccountManagement.ContextType]::Machine } PROCESS{ foreach ($Computer in $ComputerName) { Write-Verbose "Connecting to $Computer" $context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $Computer $idtype = [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName $group = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, 'Administrators') $group.Members | select @{N='Server'; E={$computer}}, @{N='Group Name'; E={$group.Name}}, @{N='Domain'; E={$_.Context.Name}}, samaccountName } # end foreach } # end PROCESS }
下面是一些不起作用的例子(根本没有返回结果):
PS D:\> Test-LocalSecurityGroups -ComputerName $env:COMPUTERNAME -Group "Administrator" PS D:\> PS D:\> $env:COMPUTERNAME, "Computer1" | Test-LocalSecurityGroups PS D:\> PS D:\> $env:COMPUTERNAME, "Computer1" | Test-LocalSecurityGroups -Group "Administrators" PS D:\>
然而,即使没有用Groupvariables做任何事情。 如果我注释掉这部分:
[parameter( ValueFromPipelineByPropertyName=$true)] [string[]]$Group )
它工作正常..所以,任何人都可以向我解释,为什么发生这种情况? 我有一个更大的框架围绕整个function,但是我得到同样的问题时,它是孤立的,所以我的结论是,要么我忘记了一些关于PowerShell的可行性或有什么有线的事情..
没有错误,用户列表只是空的。 我从PowerShell ISE中运行它时遇到了同样的问题,并直接将其直接加载到shell(包括点源和粘贴到shell中)。
另一种方法是用两个独立的函数来完成,然后在第三个函数中比较结果。 但是这真的不是一个好的解决scheme,现在呢? :-干DRY FTW!
PowerShellvariables名称不区分大小写 。
换句话说: $Group.Equals($group)
在PROCESS块中,当您将GroupPrincipal对象分配给$group ,您正在有效地尝试覆盖现有的$Group参数variables。
只需更改内联variables名称,并添加另一个循环来遍历每个组名:
foreach ($Computer in $ComputerName) { Write-Verbose "Connecting to $Computer" $context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext -ArgumentList $ctype, $Computer $idtype = [System.DirectoryServices.AccountManagement.IdentityType]::SamAccountName foreach($groupName in $Group) { $groupPrincipal = [System.DirectoryServices.AccountManagement.GroupPrincipal]::FindByIdentity($context, $idtype, $groupName) $groupPrincipal.Members | select @{N='Server'; E={$computer}}, @{N='Group Name'; E={$groupPrincipal.Name}}, @{N='Domain'; E={$_.Context.Name}}, samaccountName } } # end foreach