WSH (Windows Scripting Host) İle Active Directory’de Programlama
Active Directory, Scripting, Windows Server September 30th, 2008Visual Basic Script kodlarını kullanarak Active Directory içerisinde nesne oluşturma, silme, nesneler üzerinde değişiklikler yapma, nesneleri sorgulama, toplu halde nesneler açma gibi işlevleri yerine getirebilirsiniz.
RootDSE Nedir?
RootDSE’nin amacı kullanıcı nesnesini Active Directory’ye bağlamak için gerekli tutamağı sağlamaktır. RootDSE’nin açılımı Root
Directory Service Entry’dir. RootDSE objelerin ne yaptıklarını açıklar. Teknik olarak VBS’den daha ziyade WSH ile GetObject methodunu kullanarak Active Directory’ye bağlantıyı sağlar. Büyük organizasyonlarda RootDSE’yi root domain’e göre belirleyebilirsiniz. Normalde RootDSE o anda bulunulan domain’e ayarlıdır. Bütün bu söylediklerimizin özeti olarak RootDSE’nin active directory’nin ta kendisi olduğunu söylememiz yanlış olmaz.
ADSI Edit - Support Tool
RootDSE’nin ne yaptığına farklı bir perspektiften bakarsak, bunu bize en iyi açıklayacak araç Windows Server 2003 CD’si içerisindeki Support dizini içerisindeki Tools dizininden Windows Server Support Tool’lar kurulduktan sonra gelen ADSIEDIT aracıdır. ADSI, Active Directory içerisindeki kullanıcı ve bilgisayar gibi objelerin özelliklerini ve niteliklerini öğrenmek için gerekli grafiksel arayüzü sağlar. ADSIEDIT kullanarak Active Directory yapısındaki üç kabın içerisini görebilirsiniz.Bunlar:
- Domain
- Configuration
- Schema
Biz bu kapların her birine partition(active directory database bölümleri) adını veriyoruz.
1 - RootDSE ‘den Domain adını çekmek
‘ RootDSE nesnesinden domain adını çeken kod.
Set objRootDSE = GetObject(”LDAP://RootDSE”)
strDomain = objRootDSE.Get(”DefaultNamingContext”)
Eğer mevcut domain adını görmek isterseniz yukarıdaki kodun en alt satırına aşağıdaki satırlardan birini yazın. Böylece domain adını çekip size bir mesaj kutusunda gösterecektir.
WScript.Echo strDomain ‘ Domain adını control etmek için test amaçlı konuldu
Veya
Msgbox(strDomain) ‘ Domain adını control etmek için test amaçlı konuldu
Not 1: Biz bu örnekte objRootDSE ve strDomain değişken isimlerini kullandık. Sizde kendinize uygun, kurallara uyan degisken isimlerini rahatça kullanabilirsiniz. Fakat tabii ki anlamlı değişkenler kullanmak en iyisidir.
Note 2:
DefaultNamingContext RootDSE (Active Directory) ‘nin bir özelliğidir.
2 – Objelerin Oluşturulacağı Yerin(objContainer) Belirlenmesi
Set objContainer = GetObject(”LDAP://” & strOUContainer)
Not 1:
strOUContainer nesnelerin oluşturulacağı yeri gösteren son derece önemli bir değişkendir. Bu değişkenin değerini domain adınıza ve OU adına gore doğru bir biçimde ayarlamalısınız.
Not 2:
“LDAP://” nesnenin oluşturulacağı hedef yerin active directory olduğunu söyler.
Şimdi uygulamalarla bunları görelim.
Uygulama 1: OU Olusturma
- Notepad`i acin.
- OUac.vbs isimli bir dosya olusturun.
- Asagidaki kodlari bu dosya icine yazin.
Set oRoot = GetObject(”LDAP://rootDSE”)
Set oDomain = GetObject(”LDAP://” & oRoot.Get(”defaultNamingContext”))
Set oOU=oDomain.Create(”organizationalUnit”, “ou=maya”)
oOU.Put “Description”, “MAYASOFT Bilgi Teknolojileri Merkezi”
oOU.SetInfo
Yukarıdaki script kodunu yazıp çalıştırdıktan sonra bulunduğunuz domain altında maya isimli bir OU açar ve Description özelliğine MAYASOFT Bilgi Teknolojileri Merkezi ifadesini atar.
Uygulama 2: Kullanıcı Olusturma
Set oRoot = GetObject(”LDAP://rootDSE”)
Set oDomain = GetObject(”LDAP://” & oRoot.Get(”defaultNamingContext”))
Set oUser = oOU.Create(”User”, “cn=egitmen”)
oUser.Put “sAMAccountName”, ” egitmen ”
oUser.Put “Description”, “Marka Olmus Uzman Egitmenler”
oUser.SetInfo
oUser.SetPassword “123+abc”
oUser.AccountDisabled = False
oUser.SetInfo
Wscript.Echo “Basariyla tamamlandi, Active Directory Users and Computers bakın!!!”
Yukarıdaki script kodunu yazıp çalıştırdıktan sonra bulunduğunuz domain altında egitmen isimli bir kullanıcı açar ve Description özelliğine Marka Olmus Uzman Egitmenler yazar. Burada SETINFO methodu onaylama amaçlı kullanılır. Setinfo satırına gelene kadar henüz obje oluşturulmaz. Setinfo satırı ile işleme onay verilir. SetPassword ile kullanıcı hesabına şifre atanır. AccountDisabled methodu ile kullanıcı hesabının enable mı yoksa disable mı olarak açılacağı belirlenir. Yapılan bu değişiklikler yine veritabanına yazılması ve güncellenmesi için setinfo methodu ile onaylanmıştır. Wscript.Echo ile işlem tamamlandıktan sonra bir mesaj görüntüleme gerçekleştirilmiş oluyor.
Uygulama 3: Toplu Kullanıcı Olusturma
‘TOPLUM OU’su içerisine toplu kullanıcı ekleme
‘Mesut Aladağ – Yasin Aşır tarafından yazılmış script
Dim nesneOU
Set oRoot = GetObject(”LDAP://rootDSE”)
Set oDomain = GetObject(”LDAP://” & oRoot.Get(”defaultNamingContext”))
Set objRootDSE = GetObject(”LDAP://rootDSE”)
Set nesneOU=oDomain.Create(”organizationalUnit”, “ou=Toplum”)
nesneOU.Put “Description”, “Mesut Aladağ – Yasin Aşır Yazdı…”
nesneOU.SetInfo
Set nesnekap= GetObject(”LDAP://OU=Toplum,” & _
objRootDSE.Get(”defaultNamingContext”))
For i = 1 To 10
Set nesnetip=nesnekap.Create(”User”, “cn=Kullanici” & i)
nesnetip.Put “sAMAccountName”, “Kullanici” & i
nesnetip.SetInfo
Next
WScript.Echo “Tebrikler, 10 Kullanıcı açıldı F5 basarak güncelleyin”
Yukarıdaki kod çalıştırılınca once domain altında Toplum isimli bir OU açar. Daha sonra bu OU içerisine kullanıcı1′den kullanıcı10′a kadar 10 adet kullanıcı oluşturur.
VBScript Kullanarak Satis OU’su içerisine Kullanıcı Ekleme
Bu örnekte, VBScript kullanarak Satis OU’su içerisine yeni bir kullanıcı eklemeyi öğreneceksiniz. Bu örnek ADSI ve VBScript ile active directory’de programlamanın kolaylığı ve rahatlığını göreceksiniz.Bu örnekte biz kullanıcının sadece belli özelliklerini tanımladık.Siz arzu ederseniz tüm özelliklerini tanımlayabilirsiniz.
NotePad’i açın ve aşağıdaki kodu yazıp, UserAC.vbs adı ile kaydedin ve çalıştırın.
Set oRoot = GetObject(”LDAP://rootDSE”)
Set oDomain = GetObject(”LDAP://ou=satis,” & oRoot.Get(”defaultNamingContext”))
Set objRootDSE = GetObject(”LDAP://rootDSE”)
Set oUser=oDomain.Create(”User”, “CN=Feyzo”)
oUser.Put “samAccountName”,”Feyzo”
oUser.Put “givenName”,”Feyzo”
oUser.Put “sn”,”Kibar”
oUser.Put “userPrincipalName”,”feyzok@mayasoft.com.tr”
oUser.SetInfo
Set oUser = Nothing ‘oUser değişkenini siliyor.
Set oDomain = Nothing ‘ oDomain değişkenini siliyor.
WScript.Echo “Tamamlandı”
Yukarıdaki kodu çalıştırdığınızda active directory içerisindeki satış OU’su içerisine Feyzo isimli bir kullanıcı hesabının eklendiğini göreceksiniz.
VBScript Kullanarak Kullanıcı Silmek
Bu örnekte, VBScript kullanarak Satis OU’su içerisindeki feyzo isimli kullanıcı hesabını silmeyi öğreneceksiniz.
NotePad’i açın ve aşağıdaki kodu yazıp, UserSIL.vbs adı ile kaydedin ve çalıştırın.
Set oRoot = GetObject(”LDAP://rootDSE”)
Set oDomain = GetObject(”LDAP://ou=satis,” & oRoot.Get(”defaultNamingContext”))
Set objRootDSE = GetObject(”LDAP://rootDSE”)
oDomain.Delete “User”, “CN=Feyzo”
Set oDomain = Nothing ‘oContainer değişkenini siliyor.
WScript.Echo “Tamamlandı”
VBScript Kullanarak Toplu Olarak Kullanıcı Özelliklerini Değiştirme
Bu örnekte, VBScript kullanarak Satis OU’su içerisindeki bütün kullanıcı hesaplarının adres bilgilerinde güncellemeler yapacağız.
NotePad’i açın ve aşağıdaki kodu yazıp, UserDegistir.vbs adı ile kaydedin ve çalıştırın.
Set oRoot = GetObject(”LDAP://rootDSE”)
Set oDomain = GetObject(”LDAP://ou=satis,” & oRoot.Get(”defaultNamingContext”))
Set objRootDSE = GetObject(”LDAP://rootDSE”)
ModifyUsers oDomain
Set oDomain = Nothing ‘oContainer değişkenini siliyor.
WScript.Echo “Tamamlandı”
Sub ModifyUsers(oObject)
Dim oUser
oObject.Filter = Array(”user”)
For Each oUser in oObject
oUser.Put “st”,”Beşiktaş”
oUser.Put “streetAddress”,”Barbaros Blv. No:9″
oUser.Put “postalCode”,”80690″
oUser.Put “l”,”Beşiktaş”
oUser.SetInfo
Next
End Sub
Gidip Active Directory Users and Computers içerisinden baktığınızda aşağıdaki gibi oluştuğunu göreceksiniz.
VBScript Kullanarak Objeleri Export Etmek
Bu örnekte, VBScript kullanarak Satis OU’su içerisindeki bütün kullanıcı hesapları ve bunların niteliklerini bir text dosyaya listelemeyi göreceksiniz.
NotePad’i açın ve aşağıdaki kodu yazıp, RaporAl.vbs adı ile kaydedin ve çalıştırın.
Dim OutPutFile
Dim FileSystem
Set oRoot = GetObject(”LDAP://rootDSE”)
Set oDomain = GetObject(”LDAP://ou=satis,” & oRoot.Get(”defaultNamingContext”))
Set objRootDSE = GetObject(”LDAP://rootDSE”)
Set FileSystem = WScript.CreateObject(”Scripting.FileSystemObject”)
Set OutPutFile = FileSystem.CreateTextFile(”c:\rapor.txt”, True)
EnumerateUsers oDomain
OutPutFile.Close
Set FileSystem = Nothing
Set oDomain = Nothing ‘oDomain değişkenini siliyor.
WScript.Echo “Tamamlandı”
WScript.Quit(0)
Sub EnumerateUsers(oCont)
Dim oUser
For Each oUser In oCont
Select Case LCase(oUser.Class)
Case “user”
If Not IsEmpty(oUser.distinguishedName) Then
OutPutFile.WriteLine “dn: ” & oUser.distinguishedName
End If
If Not IsEmpty(oUser.name) Then
OutPutFile.WriteLine “name: ” & oUser.Get (”name”)
End If
If Not IsEmpty(oUser.st) Then
OutPutFile.WriteLine “st: ” & oUser.st
End If
If Not IsEmpty(oUser.streetAddress) Then
OutPutFile.WriteLine “streetAddress: ” & oUser.streetAddress
End If
Case “organizationalunit” , “container”
EnumerateUsers oUser
End Select
OutPutFile.WriteLine
Next
End Sub
WSH Kullanarak Grup Hesabı Oluşturmak
Set oRoot = GetObject(”LDAP://rootDSE”)
Set oDomain = GetObject(”LDAP://” & oRoot.Get(”defaultNamingContext”))
Set oGroup = oDomain.Create(”Group”, “cn=Patronlar”)
oGroup.Put “sAMAccountName”, ” Patronlar ”
oGroup.SetInfo
Wscript.Echo ” Tebrikler Basariyla tamamlandi”
Yukarıdaki script kodunu yazıp çalıştırdıktan sonra bulunduğunuz domain altında Patronlar isimli bir grup açar.
Script İle Bilgi Toplama:
Kullanıcı adını , bilgisayar adını ve çalıştığınız domain adını veren script:
Set ad = WScript.CreateObject(”WScript.Network”)
WScript.Echo “Computer Name = ” & ad.ComputerName & VBCrLf
_
& “User Name = ” & ad.UserName & VBCrLf & “Domain = ” & ad.UserDomain
Not 1: VBCrLf alt satıra geçmeyi sağlar.
Not 2: Alt çizgi (Underscore(_)) kod yarıda kalınca ikinci satırdan devam etmeyi sağlar.



January 21st, 2010 at 10:57 am
Peki bunu web üzerinde nasıl kullanabiliriz vbscript ya da javascriptle. Değiştirme olmayacak tabi ki. Sadece bilgi alma. Windows Authentication bilgisiyle LDAP’tan kullanıcı bilgisini alma.
February 3rd, 2010 at 6:34 am
I think, that you are mistaken. Let’s discuss it. Write to me in PM, we will talk.
March 4th, 2010 at 9:28 am
Very good news
March 17th, 2010 at 9:21 pm
Goodmorning
awesome post - i’m creating video about it and i will post it to youtube !
if you wana to help or just need a link send me email !
April 3rd, 2010 at 4:35 pm
You had some nice points here. I done a research on the topic and got most peoples will agree with your blog.
April 23rd, 2010 at 10:56 pm
tag heuer
May 9th, 2010 at 12:24 am
Thanks to the author.
May 19th, 2010 at 8:32 pm
Mesut ALADAĞ Blog Archive WSH Windows Scripting Host İle Active Directory de Programlama best post. I has been collection in my blog. 2010/05/05
May 27th, 2010 at 8:34 pm
Mesut ALADAĞ Blog Archive WSH Windows Scripting Host İle Active Directory de ProgramlamaI just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.2010-5-28
May 30th, 2010 at 3:10 am
Set your life time more easy take the mortgage loans and everything you want.
May 30th, 2010 at 10:38 am
replica watches
June 5th, 2010 at 6:19 pm
Mesut ALADAĞ Blog Archive WSH Windows Scripting Host İle Active Directory de ProgramlamaI bookmarked it.2010-6-5
June 5th, 2010 at 6:21 pm
Mesut ALADAĞ Blog Archive WSH Windows Scripting Host İle Active Directory de ProgramlamaValuable info. Lucky me I found your site by accident, I bookmarked it. 2010-6-5
June 5th, 2010 at 6:26 pm
Mesut ALADAĞ Blog Archive WSH Windows Scripting Host İle Active Directory de Programlamabest post. I has been collection in my blog.2010-6-5
June 7th, 2010 at 6:59 pm
armani
June 8th, 2010 at 12:25 am
replica chopard watches
June 8th, 2010 at 8:45 am
Mesut ALADAĞ Blog Archive WSH Windows Scripting Host İle Active Directory de ProgramlamaPretty good post.2010-6-8
June 17th, 2010 at 7:55 pm
интеретсный блог почему только так мало читателей на нём
June 24th, 2010 at 2:33 am
replica handbags
June 28th, 2010 at 4:23 pm
http://www.mesutaladag.com is very interesting. I enjoyed your blog a lot. Thank you.
July 9th, 2010 at 12:21 pm
watchvisa
July 11th, 2010 at 8:27 am
http://www.mesutaladag.com is great! Revealed Getting Approved in the Best Payday Loans This is the main reason why payday loans eist A payday loan is actually a shortterm loan that is awarded to employees and are payable on the net payday In other words payday loans are cash advances
July 22nd, 2010 at 12:37 pm
replica watch
July 22nd, 2010 at 6:30 pm
On this neighbourhood - http://theironinsurance.info , you desire espy the best decision in favour of these problems
July 26th, 2010 at 8:57 am
fake watches for sale
July 31st, 2010 at 8:23 pm
Wow, wonderful blog post! I even have shown this to my friends!
August 1st, 2010 at 8:09 pm
That’s right I said! Author fellow. Nothing to add ….
August 3rd, 2010 at 12:54 am
original cialis kaufen sie cialis rezeptpflichtig bestellen cialis bestellen rezeptfrei
August 3rd, 2010 at 4:19 am
dental hygienist miami
August 3rd, 2010 at 6:58 am
original cialis test cialis preis rezeptfrei online apotheke cialis billig
August 6th, 2010 at 2:15 pm
rolex watch
August 6th, 2010 at 2:39 pm
Комплексный прогон
August 7th, 2010 at 10:51 am
Интересует привлечение клиентом или баннерная реклама? Тогда зайдите на наш интернет-сайт http://www.mediumsurf.com . Наш серфинг веб-сайтов дает разрешённый, простой и бесплатный способ продвижения веб-сайтов, поднятие мест в рейтингах, накрутка статистики, накрутка счетчика посещений по различным ценам.
August 7th, 2010 at 2:22 pm
Excellent post
August 7th, 2010 at 2:34 pm
add to bookmarks
August 7th, 2010 at 3:05 pm
автор excellent site
August 7th, 2010 at 3:16 pm
theme not up to the end
August 7th, 2010 at 3:47 pm
Very good a post the author write ещё
August 7th, 2010 at 5:19 pm
intresting site
August 7th, 2010 at 6:30 pm
Very nice site
August 7th, 2010 at 7:59 pm
Nice news
August 7th, 2010 at 11:57 pm
intresting to esteem yours blog
August 9th, 2010 at 6:33 am
содействие в реализации автомашины схемой SEO по особо свежим технологиям. Быстро и дешево. высокая эффективность. расположение в каталогах, досках, форумах. О Вашем автомашина узнает тысячная аудитория. Все объявления содержат фотокарточку авто. детальнее на http://pr-auto7.narod.ru/ или ICQ 596-558-744 ПРОДАТЬ МАШИНУ СТАЛО ПРОСТО!!!
August 9th, 2010 at 4:48 pm
Good post. I just came across your blog and just wanted to say that I really enjoy your blog posts. I will subscribe to your feed and look forward to reading some more of your posts.
August 9th, 2010 at 5:03 pm
В наличии генераторы Вепрь - дизельные электростанции. Склад дизельных электростанций.
August 11th, 2010 at 1:32 am
I do think this is a most incredible website for proclaiming great wonders of Our God!
August 13th, 2010 at 6:32 pm
swiss replica watch
August 15th, 2010 at 3:48 pm
replica louis vuitton
August 16th, 2010 at 4:44 pm
louis vuitton handbags
August 16th, 2010 at 5:01 pm
cheap rolex watch
August 16th, 2010 at 5:07 pm
ugg boots
August 18th, 2010 at 9:57 am
buy viagra in england,buy viagra meds online
August 18th, 2010 at 9:35 pm
rolex watch
August 18th, 2010 at 10:18 pm
http://www.mesutaladag.com is great! I loved reading it. Thank you for all your work.
August 24th, 2010 at 9:01 am
Your post is very useful. Thank you so much for presenting such an abundance of valuable related information. I am going to bookmark your web site and will be certainly returning. Once again, I respect all your work and as well , providing such a lot of essential facts for the readers.
August 24th, 2010 at 10:38 am
Clothes Store makes online shopping a snap with a suggestion tool, easy searching and this season’s must-haves. Shop women’s and men’s clothes now.
August 26th, 2010 at 2:39 pm
In Starcraft, a mecca of Korea University has discovered that computer addiction can heal - ostensibly in Starcraft - with antidepressants. These usually come in depression, panic attacks, phobias and much more used.
Starcraft addiction seems to represent a serious problem in Korea. The Department of Psychiatry, Chung Ang University found in a study that video games can be and in the case of the study ostensibly cured Starcraft addiction with large doses of bupropion. It is a dopamine and norepinephrine inhibitor that is normally used as an antidepressant. This mainly depression, obsessive-compulsive disorders and panic attacks, phobias, eating disorders, chronic pain, withdrawal syndrome, lethargy, insomnia and more are discussed (45901vbe).
The University of Chung Ang underwent eleven o’clock Starcraft-addicted Koreans this treatment. After six weeks of the study authors reported the first success. The desire of the players was therefore decreased by 23.6 percent, while the playing back of the subjects in their favorite game by 35.5 percent went.
August 27th, 2010 at 10:45 pm
There are many interesting here. Hope to see some more in future
September 1st, 2010 at 2:14 pm
replica watch, replica watch
September 1st, 2010 at 6:45 pm
watches, watches
September 1st, 2010 at 11:14 pm
srrenzjnxxmupztqdoizrqaghddnlzbexvo
September 3rd, 2010 at 3:07 pm
boots, boots