简体   繁体   English

签入Powershell之后将用户添加到AD组

[英]Adding a User to AD group after checking in Powershell

I'm trying to check our users in AD to see if they are part of a group, and if they're not add them to it. 我正在尝试检查AD中的用户是否属于某个用户组,以及是否未将其添加到该用户组中。

The script I have doesn't seem to be working. 我拥有的脚本似乎无法正常工作。

*Import-Module ActiveDirectory

$Grpex26Month = "EX-Retention 26 Months"
$Grpex13Month = "EX-Retention 13 Months"

Function Check-IsGroupMember{

Param($user,$grp)

$strFilter = "(&(objectClass=Group)(name=" + $grp +"))"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"

$colResults = $objSearcher.FindOne()

$objItem = $colResults.Properties

([string]$objItem.member).contains($user)
}

$userList = get-aduser -f {surname -like 'm*'}

Foreach ($user in $userList) {
$Check = Check-IsGroupMember $user $grpex13month

If ($Check -eq 'False') {
Add-adgroupmember $grpex13month $user 
write-host $user.Name 
}
}*

Now this is a script I modified that previously removed from one group and adding to another but I thought the changes above would still be ok. 现在,这是我修改过的脚本,以前已从一个组中删除并添加到另一个组中,但是我认为上面的更改仍然可以。 I'm also only searching for just 'M' at present as I know there is a user in this section that requires this. 我目前也只搜索“ M”,因为我知道此部分中有一个用户需要此功能。

This user isn't even being found...this is an example of response I'm getting for all the users apart from the new ones that were set up after this script was first written. 甚至没有找到该用户...这是我得到的所有响应示例,除了在首次编写此脚本后设置的新用户之外。

MLastName, FirstName 
Add-adgroupmember : The specified account name is already a member of the group
At \\ServerName\DataUsers$\DKendall\Scripts\Exchange groupremoval.ps1:31 char:1
+ Add-adgroupmember $grpex13month $user
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (EX-Retention 13 Months:ADGroup)[Add-ADGroupMember], ADException
+ FullyQualifiedErrorId : ActiveDirectoryServer:1378,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

To explain what Matt is getting at a little more, the issue is with this check: 为了说明Matt的功能,请检查以下内容:

If ($Check -eq 'False') {

When PowerShell is asked to evaluate a statement it looks at the type of the first object, and attempts to convert the second object to the same type. 当要求PowerShell对一条语句进行评估时,它将查看第一个对象的类型,并尝试将第二个对象转换为相同的类型。 In this case $Check is [boolean] meaning its value is either $true or $false . 在这种情况下, $Check[boolean]表示其值为$true$false When you echo it to the host the PowerShell formatter will convert it to a string to make it user friendly, but that's not what the object actually is. 当您将其回显到主机时,PowerShell格式化程序会将其转换为字符串,以使其对用户友好,但这实际上不是对象。

$Check = $false

$Check.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Boolean                                  System.ValueType

Now, as Matt stated, any string with a length longer than 0 will evaluate as $true when converted to [boolean] . 现在,正如Matt所言,任何长度大于0的字符串在转换为[boolean]时都将被评估为$true So PowerShell looks at your statement, sees that $Check is [boolean] and tries to convert 'False' to [boolean] to match it. 因此,PowerShell查看您的语句,发现$Check[boolean]并尝试将'False'转换为[boolean]以匹配它。 Since 'False' is a string with a length over 0 it converts to $true . 由于'False'是长度超过0的字符串,因此它将转换为$true Now we know that $Check = $false , and since it converted your string to a [boolean] value of $true the statement reads: 现在我们知道$Check = $false ,并且由于它将字符串转换为$true[boolean]值,因此该语句显示为:

If ($false -eq $true) {

So in effect, your script is doing the exact opposite of what you want it to. 因此,实际上,脚本正在执行与您想要的相反的操作。 Solutions to this include: 解决方案包括:

If ($Check -eq $false) {

or 要么

If (-Not $Check) {

or the shortened version (what I would use) 或缩短的版本(我会用什么)

If(!$Check){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM