Day 12 ADSI
Using Powershell you can work on Active directory as well. Active directory is introduced in windows 2000. It’s a system where you can use domain server and create an organization unit –group of people on one unit eg. HR department. And then the users who are part of organization unit. for an enterprise business there would be number of users working so for them ADSI is important.
Please do not try to make implement these stuff if you don’t understand and if your are not authorized to do it. Specially if you are not the windows admin and having not much knowledge of it please don’t use the information given in this blog. For practice purpose you can download the ADAM(Active Directory Application Mode) free for download from Microsoft and work on standalone system
Even I have inherited the examples from book – Powershell in Action
$domain = [ADSI] `
>> “LDAP://localhost:123/dc=NA,dc=Power1,dc=com”
Now create the Orgnaization Unit- HR
$newOU = $domain.Create(“OrganizationalUnit”, “ou=HR”)
$newOU.SetInfo()
Setinfo() is very important until this your information will not set.
Next add the user. For that we need to create the object for ou
$ou = [ADSI] “LDAP://localhost:389/ou=HR,dc=NA,dc=fabrikam,dc=com”
Now create a new object for new user
$newUser = $ou.Create(“user”, “cn=Dogbert”)
Assign property for the $newuser object
$newUser.put(“Title”,”Mice”)
$newUser.put(“Employee ID”,1)
$newUser.Put(“description”, “Micheal”)
$newUser.SetInfo()
And finally update the information onto the server.
Now if we want to inport/create multiple new users we can create an function for that
function New-Employee ($employees
{
>> foreach ($record in $employees)
>> {
>> $newUser = $ou.Create(“user”, “cn=$($record.Name)”)
>> $newUser.Put(“title”, $record.Title)
>> $newUser.Put(“employeeID”, $record.employeeID)
>> $newUser.Put(“description”, $record.Description)
>> $newUser.SetInfo()
>>}
}
You can import the new users via csv as
>>New-Employee (Import-Csv Newusers.csv)
or import one by one or multiple calling function New-Employee
Now to get the users from ou another function
function Get-Employee (
>> [string] $name=’*',
>> [adsi] $ou =
>> “LDAP://localhost:123/ou=HR,dc=NA,dc=Power1,dc=com”
>> )
>> {
>> [void] $ou.psbase
>> $ou.psbase.Children | where { $_.name -like $name}
>> }
Now if you want to search something in your AD need to create an object for .DirectoryServices.DirectorySearcher
$ADDomain = [ADSI]“LDAP://dc=Power1,dc=local”
$ADSearch = New-Object System.DirectoryServices.DirectorySearcher
$ADSearch.SearchRoot = $ADDomain
# Definition of the filter: only computer, search for names
$ADSearch.Filter = „(objectCategory=computer)“
$ADSearch.PropertiesToLoad.Add(“name”)
$results = $ADSearch.FindAll()
# For the result we use a trick to only display the names:
Foreach ($res in $results)
{ $ADComp = $res.Properties
$ADcomp.Name
}
Get information about domain
Get-DomainInfo
Or
Get-DomainInfo .
Quest company has also developed its own AD related cmdlets .
PSCX extiention is another one who make Active directory work as a PSDrive.