標籤:vbs程式批量禁用域使用者然後移動到指定ou
作為一個大公司的IT管理員,最痛苦的是每天建立、禁用、刪除好幾十個人員使用者資訊,目前我們通過程式大量建立域使用者,但是離職使用者的相關資訊禁用及刪除,需要我們手動來完成,之前檔案介紹了,我們通過powershell來對指定的使用者禁用及移動到指定的OU下,今天我們就通過vbs來實現將指定的使用者禁用及移動到指定的OU。
我們首先是需要查看運行環境,我們建立Dsgrd Object組織單元,然後在該組織單元下建立Domain users二級組織單元,然後建立測試使用者。
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/30/451336_1414646042elJL.png" height="507" />
我們要將使用者禁用後,移動到指定的OU下:Disable Accounts
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/30/451336_1414646042aF3C.png" height="508" />
接下來我們編輯程式:
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/30/451336_1414646043cwK8.png" height="677" />
代碼:
‘==========================================================================‘‘ VBScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1‘‘ NAME: ‘‘ AUTHOR: Windows 使用者 , 12345‘ DATE : 2014/10/29‘‘ COMMENT: ‘‘==========================================================================strDisableAccount = TrueForReading=1strNewParentDN = "OU=Disable Accounts" ‘ move To strContainer = "OU=Dsgrd Object" ‘ source‘ ------ END CONFIGURATION ---------‘ 開始運行功能Set dic = CreateObject("Scripting.Dictionary")Set objRootDSE = GetObject("LDAP://rootDSE")strDomainDN = objRootDSE.Get ("defaultNamingContext") ‘ dong tai huo qu yu Set objContainer = GetObject ("LDAP://"&strContainer&"," & strDomainDN)subContainer objContainer,strUserDNSet objFSO = CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.OpenTextFile("D:\11.txt", ForReading)While Not objFile.AtEndOfStream WScript.Echo VbCrLf strUserDN = trim(objFile.Readline) WScript.Echo strUserDN ‘& VbCrLfIf dic.exists(strUserDN) Then Set objCont = GetObject("LDAP://"&strNewParentDN&","& strDomainDN)‘Set objUser = dic.item(strUserDN)If objUser.AccountDisabled = True Then‘ WScript.Echo "Account for " & objUser.Get("cn") & " currently disabled - Not moved" objCont.MoveHere "LDAP://"& objUser.distinguishedName,"CN="&strUserDN ‘vbNullStringElseWScript.Echo "Account currently enabled"objUser.AccountDisabled = TrueobjUser.SetInfo objCont.MoveHere "LDAP://"& objUser.distinguishedName,"CN="&strUserDN ‘vbNullString WScript.Echo "Account for " & objUser.Get("cn") & " moved to new OU" end IfEnd IfWendobjFile.CloseSet objRootDSE = NothingSub subContainer(objContainer,strUserDN)For Each objUser In objContainer If RegExpTest("OU=.*",objUser.name) Then‘ MsgBox objUser.namesubContainer objUser,strUserDNElse dic.add objUser.sAMAccountName,objUser‘ MsgBox objUser.name &":"& objUser.AccountDisabledEnd If Next‘Set objContainer = NothingEnd Sub Function RegExpTest(patrn, strng) Dim regEx, retVal ‘ 建立變數。 Set regEx = New RegExp ‘ 建立Regex。 regEx.Pattern = patrn ‘ 設定模式。 regEx.IgnoreCase = False ‘ 設定是否區分大小寫。 retVal = regEx.Test(strng) ‘ 執行搜尋測試。 If retVal Then RegExpTest =True Else RegExpTest = False End IfEnd Function
我們設定需要禁用並且移動的賬戶資訊:
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/30/451336_14146460435jE8.png" height="486" />
然後我們運行程式進行查看:
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/30/451336_1414646046PMrC.png" height="457" />
程式執行後的效果:
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;margin:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201410/30/451336_1414646046BiHu.png" height="440" />
本文出自 “高文龍” 部落格,請務必保留此出處http://gaowenlong.blog.51cto.com/451336/1569616
vbs程式批量禁用域使用者然後移動到指定OU