CREATING AN OU Description Creates a new organizational unit within Active Directory directory service.
Script Code Set objDomain = GetObject("LDAP://dc=fabrikam,dc=com") Set objOU = [Link]("organizationalUnit", "ou=Management") [Link]
Creating an OU in an Existing OU Description Creates a new organizational unit (OU2) in an existing organizational unit (OU1). Script Code Set objOU1 = GetObject("LDAP://ou=OU1,dc=na,dc=fabrikam,dc=com") Set objOU2 = [Link]("organizationalUnit", "ou=OU2") [Link]
Create User Account Description Creates a user account in Active Directory. This script only creates the account, it does not enable it. Script Code Set objOU = GetObject("LDAP://OU=management,dc=fabrikam,dc=com") Set objUser = [Link]("User", "cn=MyerKen") [Link] "sAMAccountName", "myerken" [Link]
Creating 1,000 User Accounts Description Demonstration script that creates 1,000 user accounts (named UserNo1, UserNo2, UserNo3, etc.) in the Users container in Active Directory. The script is useful for test scenarios that require multiple user accounts. Script Code Set objRootDSE = GetObject("LDAP://rootDSE") Set objContainer = GetObject("LDAP://cn=Users," & _ [Link]("defaultNamingContext")) For i = 1 To 1000 Set objLeaf = [Link]("User", "cn=UserNo" & i) [Link] "sAMAccountName", "UserNo" & i [Link] Next [Link] "1000 Users created."
Move a Group Within a Domain Description Moves a group account from the HR OU to the Users container. Script Code Set objOU = GetObject("LDAP://cn=Users,dc=NA,dc=fabrikam,dc=com") [Link] "LDAP://cn=atl-users,ou=HR,dc=NA,dc=fabrikam,dc=com", _vbNullString
Change Computer Account Attributes Description Demonstration script that changes the location attribute for a computer account in Active Directory directory service. Script Code Set objComputer = GetObject _ ("LDAP://CN=atl-dc-01,CN=Computers,DC=fabrikam,DC=com") [Link] "location", "Building 37, Floor 2, Room 2133" [Link]
Change User Account Attributes Description Configures user account attributes found on the General Properties page of the user account object in Active Directory Users and Computers. Script Code Const ADS_PROPERTY_UPDATE = 2 Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") [Link] "givenName", "Ken" [Link] "initials", "E." [Link] "sn", "Myer" [Link] "displayName", "Myer, Ken" [Link] "physicalDeliveryOfficeName", "Room 4358" [Link] "telephoneNumber", "(425) 555-1211" [Link] "mail", "myerken@[Link]" [Link] "wWWHomePage", "[Link] [Link] ADS_PROPERTY_UPDATE, _ "description", Array("Management staff") [Link] ADS_PROPERTY_UPDATE, _ "otherTelephone", Array("(800) 555-1212", "(425) 555-1213") [Link] ADS_PROPERTY_UPDATE, _ "url", Array("[Link] [Link]
Change User Password Description Changes the password for a user. Requires you to know the user's previous password. Script Code Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") [Link] "i5A2sj*!", "jl3R86df"
CHANGING THE LOCAL ADMINISTRATOR PASSWORD Description Binds to the local Administrator account on the computer MyComputer, and changes the password for the account to testpassword Script Code strComputer = "MyComputer" Set objUser = GetObject("WinNT://" & strComputer & "/Administrator, user") [Link] "testpassword" [Link]
Configure Organization Properties for a User Account Description Configures organization information for the MyerKen Active Directory user account. The script also assigns MyerKen as the manager for LewJudy and AkersKim Script Code Set objUser = GetObject _ ("LDAP://cn=Myerken,ou=Management,dc=NA,dc=fabrikam,dc=com") [Link] "title", "Manager" [Link] "department", "Executive Management Team" [Link] "company", "Fabrikam" [Link] "manager", _ "cn=AckermanPilar,OU=Management,dc=NA,dc=fabrikam,dc=com" [Link] Set objUser01 = GetObject _ ("LDAP://cn=LewJudy,OU=Sales,dc=NA,dc=fabrikam,dc=com") Set objUser02 = GetObject _ ("LDAP://cn=AckersKim,OU=Sales,dc=NA,dc=fabrikam,dc=com") [Link] "manager", [Link]("distinguishedName") [Link] "manager", [Link]("distinguishedName") [Link] [Link]
Create a Computer Account Description Creates and enables a computer account in Active Directory, which must be used by an Administrator when adding a workstation to the domain. Script Code strComputer = "atl-pro-001" Const ADS_UF_PASSWD_NOTREQD = &h0020 Const ADS_UF_WORKSTATION_TRUST_ACCOUNT = &h1000 Set objRootDSE = GetObject("LDAP://rootDSE") Set objContainer = GetObject("LDAP://cn=Computers," & _ [Link]("defaultNamingContext")) Set objComputer = [Link]("Computer", "cn=" & strComputer) [Link] "sAMAccountName", strComputer & "$" [Link] "userAccountControl", _ ADS_UF_PASSWD_NOTREQD Or ADS_UF_WORKSTATION_TRUST_ACCOUNT [Link]
Delete a Computer Account Description Deletes an individual computer account in Active Directory. Script Code strComputer = "atl-pro-040" Set objComputer = GetObject("LDAP://CN=" & strComputer & _ ",CN=Computers,DC=fabrikam,DC=com") [Link](0)
Determine User Account Status Description Identifies whether a user account is enabled or disabled. Script Code Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") If [Link] = FALSE Then [Link] "The account is enabled." Else [Link] "The account is disabled." End If Determine When an Account Expires Description Returns the expiration date for a user account. Script Code On Error Resume Next Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") dtmAccountExpiration = [Link] If [Link] = -2147467259 Or _ dtmAccountExpiration = "1/1/1970" Then [Link] "No account expiration specified" Else [Link] "Account expiration:" & _ [Link] End If Disable a User Account Description Disables a user account. Script Code Const ADS_UF_ACCOUNTDISABLE = 2 Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") intUAC = [Link]("userAccountControl") [Link] "userAccountControl", intUAC OR ADS_UF_ACCOUNTDISABLE [Link]
Enable a User Account Description Enables a user account. Script Code Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") [Link] = FALSE [Link] Disable the User Cannot Change Password Option Description Disables the User Cannot Change Password option, allowing the user to change their password. Script Code Const ADS_ACETYPE_ACCESS_DENIED_OBJECT = &H6 Const CHANGE_PASSWORD_GUID = _ "{ab721a53-1e2f-11d0-9819-00aa0040529b}" Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") Set objSD = [Link]("nTSecurityDescriptor") Set objDACL = [Link] arrTrustees = Array("nt authority\self", "everyone") For Each strTrustee In arrTrustees For Each ace In objDACL If(LCase([Link]) = strTrustee) Then If(([Link] = ADS_ACETYPE_ACCESS_DENIED_OBJECT) And _ (LCase([Link]) = CHANGE_PASSWORD_GUID)) Then [Link] ace End If End If Next Next [Link] "nTSecurityDescriptor", objSD [Link] Enabling a User to Logon at Any Time Description Configures the MyerKen Active Directory user account so that the user can logon at any time on any day of the week. Script Code Const ADS_PROPERTY_CLEAR = 1 Set objUser = GetObject _ ("LDAP://cn=MyerKen,ou=Management,dc=NA,dc=fabrikam,dc=com") [Link] ADS_PROPERTY_CLEAR, "logonHours", 0 [Link]
Enumerate Computer Accounts in Active Directory Description Returns the name and location for all the computer accounts in Active Directory. Script Code Const ADS_SCOPE_SUBTREE = 2 Set objConnection = CreateObject("[Link]") Set objCommand = CreateObject("[Link]") [Link] = "ADsDSOObject" [Link] "Active Directory Provider" Set [Link] = objConnection [Link] = _ "Select Name, Location from 'LDAP://DC=fabrikam,DC=com' " _ & "where objectClass='computer'" [Link]("Page Size") = 1000 [Link]("Timeout") = 30 [Link]("Searchscope") = ADS_SCOPE_SUBTREE [Link]("Cache Results") = False Set objRecordSet = [Link] [Link] Do Until [Link] [Link] "Computer Name: " & [Link]("Name").Value [Link] "Location: " & [Link]("Location").Value [Link] Loop Enumerate Installed Hot Fixes Description Returns a list of all the hot fixes installed on a computer. Script Code strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colQuickFixes = [Link] _ ("Select * from Win32_QuickFixEngineering") For Each objQuickFix in colQuickFixes [Link] "Computer: " & [Link] [Link] "Description: " & [Link] [Link] "Hot Fix ID: " & [Link] [Link] "Installation Date: " & [Link] [Link] "Installed By: " & [Link] Next
Enumerate Installed Software Description Returns a list of software that was installed on a computer using Windows Installer. Script Code Set objFSO = CreateObject("[Link]") Set objTextFile = [Link]("c:\scripts\[Link]", True) strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colSoftware = [Link] _ ("Select * from Win32_Product")
[Link] "Caption" & vbtab & _ "Description" & vbtab & "Identifying Number" & vbtab & _ "Install Date" & vbtab & "Install Location" & vbtab & _ "Install State" & vbtab & "Name" & vbtab & _ "Package Cache" & vbtab & "SKU Number" & vbtab & "Vendor" & vbtab _ & "Version" For Each objSoftware in colSoftware [Link] [Link] & vbtab & _ [Link] & vbtab & _ [Link] & vbtab & _ objSoftware.InstallDate2 & vbtab & _ [Link] & vbtab & _ [Link] & vbtab & _ [Link] & vbtab & _ [Link] & vbtab & _ [Link] & vbtab & _ [Link] & vbtab & _ [Link] Next [Link]
Enumerating All Domain Controllers Description Returns a list of all the domain controllers in the [Link] domain. Script Code Const ADS_SCOPE_SUBTREE = 2 Set objConnection = CreateObject("[Link]") Set objCommand = CreateObject("[Link]") [Link] = "ADsDSOObject" [Link] "Active Directory Provider" Set [Link] = objConnection [Link] = _ "Select distinguishedName from 'LDAP://cn=Configuration,DC=fabrikam,DC=com' " _ & "where objectClass='nTDSDSA'" [Link]("Page Size") = 1000 [Link]("Timeout") = 30 [Link]("Searchscope") = ADS_SCOPE_SUBTREE [Link]("Cache Results") = False Set objRecordSet = [Link] [Link] Do Until [Link] [Link] "Computer Name: " & [Link]("distinguishedName").Value [Link] Loop
Join Computer to a Domain Description Joins a computer to a domain and creates the computer's account in Active Directory. Script Code Const JOIN_DOMAIN = 1 Const ACCT_CREATE = 2 Const ACCT_DELETE = 4 Const WIN9X_UPGRADE = 16 Const DOMAIN_JOIN_IF_JOINED = 32 Const JOIN_UNSECURE = 64 Const MACHINE_PASSWORD_PASSED = 128 Const DEFERRED_SPN_SET = 256 Const INSTALL_INVOCATION = 262144 strDomain = "FABRIKAM" strPassword = "ls4k5ywA" strUser = "shenalan" Set objNetwork = CreateObject("[Link]") strComputer = [Link] Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _ strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _ strComputer & "'") ReturnValue = [Link](strDomain, _ strPassword, _ strDomain & "\" & strUser, _ NULL, _ JOIN_DOMAIN + ACCT_CREATE) Move a Computer Account Description Moves a computer account from the Computers container in Active Directory to an OU. Script Code Set objNewOU = GetObject("LDAP://OU=Finance,DC=fabrikam,DC=com") Set objMoveComputer = [Link] _ ("LDAP://CN=atl-pro-03,CN=Computers,DC=fabrikam,DC=com", "CN=atl-pro-03") Move a User Account Description Moves a user account from one OU to another. Script Code Set objOU = GetObject("LDAP://ou=sales,dc=na,dc=fabrikam,dc=com") [Link] _ "LDAP://cn=BarrAdam,OU=hr,dc=na,dc=fabrikam,dc=com", vbNullString
Rename a Computer and Computer Account Description Renames a computer and its corresponding Active Directory computer account. Requires Windows XP or Windows Server 2003, and must be run on the local computer. Script Code strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colComputers = [Link] _ ("Select * from Win32_ComputerSystem") For Each objComputer in colComputers err = [Link]("WebServer") [Link] err Next Require a Password Change Description Forces a user to change their password the next time they logon. Script Code Set objUser = GetObject _ ("LDAP://CN=myerken,OU=management,DC=Fabrikam,DC=com") [Link] "pwdLastSet", 0 [Link] Reset a Computer Account Password Description Resets a computer account password in Active Directory. Script Code Set objComputer = GetObject("LDAP://CN=atl-dc-01,CN=Computers,DC=Reskit,DC=COM") [Link] "atl-dc-01$" Retrieve Account Properties Description Retrieves user account attributes found on the Account page of the user account object in Active Directory Users and Computers. Script Code On Error Resume Next Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") [Link] strUserPrincipalName = [Link]("userPrincipalName") strSAMAccountName = [Link]("sAMAccountName") strUserWorkstations = [Link]("userWorkstations") Set objDomain = GetObject("LDAP://dc=fabrikam,dc=com") [Link] Array("dc"), 0 strDC = [Link]("dc") [Link] [Link] [Link] [Link] "userPrincipalName: " & strUserPrincipalName "sAMAccountName: " & strSAMAccountName "UserWorkstations: " & strUserWorkstations "dc: " & strDC
Retrieve Organization Information Description Retrieves user account attributes found on the Organization page of the user account object in Active Directory Users and Computers. Script Code On Error Resume Next Set objUser = GetObject _ ("LDAP://cn=myerken,ou=management,dc=fabrikam,dc=com") [Link] strTitle = [Link]("title") strDepartment = [Link]("department") strCompany = [Link]("company") strManager = [Link]("manager") strDirectReports = _ [Link]("directReports") [Link] "title: " & strTitle [Link] "department: " & strDepartment [Link] "company: " & strCompany [Link] "manager: " & strManager For Each strValue in strDirectReports [Link] "directReports: " & strValue Next Retrieve System Information Description Uses WMI to retrieve the same data found in the System Information applet. Script Code strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colSettings = [Link] _ ("Select * from Win32_OperatingSystem") For Each objOperatingSystem in colSettings [Link] "OS Name: " & [Link] [Link] "Version: " & [Link] [Link] "Service Pack: " & _ [Link] _ & "." & [Link] [Link] "OS Manufacturer: " & [Link] [Link] "Windows Directory: " & _ [Link] [Link] "Locale: " & [Link] [Link] "Available Physical Memory: " & _ [Link] [Link] "Total Virtual Memory: " & _ [Link] [Link] "Available Virtual Memory: " & _ [Link] [Link] "OS Name: " & [Link] Next
Set colSettings = [Link] _ ("Select * from Win32_ComputerSystem") For Each objComputer in colSettings [Link] "System Name: " & [Link] [Link] "System Manufacturer: " & [Link] [Link] "System Model: " & [Link] [Link] "Time Zone: " & [Link] [Link] "Total Physical Memory: " & _ [Link] Next Set colSettings = [Link] _ ("Select * from Win32_Processor") For Each objProcessor in colSettings [Link] "System Type: " & [Link] [Link] "Processor: " & [Link] Next Set colSettings = [Link] _ ("Select * from Win32_BIOS") For Each objBIOS in colSettings [Link] "BIOS Version: " & [Link] Next Create a Local Group on a Computer Description Creates a local group named FinanceUsers on a computer named MyComputer. Script Code strComputer = "MyComputer" Set objComputer = GetObject("WinNT://" & strComputer & ",computer") Set objGroup = [Link]("group", "FinanceUsers") [Link] Creating a Global Group Description Creates a new global security group -- atl-users02 -- within Active Directory directory service. Script Code Set objOU = GetObject("LDAP://OU=management,dc=fabrikam,dc=com") Set objGroup = [Link]("Group", "cn=atl-users02") [Link] "sAMAccountName", "atl-users02" [Link]
Deleting a Group from Active Directory Description Deletes a group named atl-users from the HR organizational unit in the hypothetical domain [Link]. Script Code Set objOU = GetObject("LDAP://ou=hr, dc=fabrikam,dc=com") [Link] "group", "cn=atl-users"
Create a Network Share Description Creates a shared folder named FinanceShare, setting the maximum number of simultaneous connections to 25, and adding a share description. Script Code Const FILE_SHARE = 0 Const MAXIMUM_CONNECTIONS = 25 strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objNewShare = [Link]("Win32_Share") errReturn = [Link] _ ("C:\Finance", "FinanceShare", FILE_SHARE, _ MAXIMUM_CONNECTIONS, "Public share for the Finance group.") [Link] errReturn
Modify a Network Share Description Accesses a shared folder named FinanceShare, changes the maximum number of simultaneous connections to 50, and provides a new share description. Script Code strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colShares = [Link] _ ("Select * from Win32_Share Where Name = 'FinanceShare'") For Each objShare in colShares errReturn = [Link](50, _ "Public share for HR administrators and the Finance Group.") Next [Link] errReturn
Publish a Shared Folder
Description Publishes a shared folder in Active Directory, assigning the folder a description and three keywords. Script Code
Set objComputer = GetObject _ ("LDAP://OU=Finance, DC=fabrikam, DC=com") Set objShare = [Link]("volume", "CN=FinanceShare") [Link] "uNCName", "\\atl-dc-02\FinanceShare" [Link] "Description", "Public share for users in the Finance group." [Link] "Keywords", Array("finance", "fiscal", "monetary") [Link]