Adding members to Active Directory Login Groups in SQL Server through powershell

 By using the below command lets we can add members to the active directory login groups. While Providign Group name dont provide the Domain name group alone works.


<#
The below Windows feature command will import Poweshell Module
Add-WindowsFeature
#>
Add-WindowsFeature RSAT-AD-PowerShell
Import-Module ServerManager
import-module activedirectory
<#
Space is compulsory between FirstName and LastName else it throws error
Depends on requirement you can choose like(-like) or equal(-eq) commands.
#>
Get-ADUser -Filter 'Name -like "*Ramesh, Mamillapalli"'
Add-ADGroupMember -Identity "GroupNameHere" -Members (Get-ADUser -Filter 'Name -like "*Ramesh, Mamillapalli"')
Get-ADUser -Filter 'Name -like "*MamilRam*"'
The below query fetch group members from active dirctory domain group
cls
$GroupName='GroupNameHere'
Get-ADGroup -filter *|sort name|Where-object {$_.Name -eq $GroupName}|Get-ADGroupMember|
Select-Object @{Name="GroupName";Expression={$GroupName}},Name,SamAccountName|Format-Table -AutoSize
The below query will bring the Active directory members more than one domain groups.
#Dont pass domain name like this domainname\groupname
cls
$GroupNames=@('ADGroup1',
'ADGroup2')
$GroupNames|
ForEach-Object{
$SingGroup=$_
Get-ADGroup -filter *|sort name|Where-object {$_.Name -eq $SingGroup}|Get-ADGroupMember|
Select-Object @{Name="GroupName";Expression={$SingGroup}},Name,SamAccountName|Format-Table -AutoSize
}
We can use multiple conditions in single -Filter parameter. Here is an example
get-aduser -filter "(GivenName -like 'Ramesh*')-and (Surname -like 'Mamillapalli*')"|
Format-Table GivenName,Name,SamAccountName,SurName,UserPrincipalName
This will show us the hidden properites with in the command.
Get-Aduser -Filter 'SamAccountName -like "*rama*"' -Properties *|Format-List * *

Comments

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Pre login Handshake or Connection Timeout Period

Transparent Data Encryption(TDE) with Master Key and Certificate in SQL Server