Showing posts with label VB Script. Show all posts
Showing posts with label VB Script. Show all posts

How to add Domain Users to Group in Active Directory through vbscript

In order to add domain user to domain group first get the group object then get the domain user from active directory, then add user's ADsPath to group as shown in below sample code.
 
Set objGroup = GetObject("LDAP://CN=TestGroup,OU=TestOU,DC=TEST,DC=com")
Set objUser = GetObject("LDAP://CN=TestUser,OU=TestOU,DC=TEST,DC=com")
objGroup.Add(objUser.ADsPath)
Wscript.echo "Successfully added user TestUser to TestGroup"

How to set password for users in Active Directory through vbscript



Below is the sample vbscript to set password for Users in Active Directory

'First connect to container (OU) under which we will create user
TempUserName="TempUser"
Set objUser = GetObject("LDAP://cn=TempUserName,OU=TestOU,DC=TEST,DC=COM")

objUser.SetPassword "Testme123"
'Also set password never expires
objUser.Put "PasswordExpired", CLng(1)
objUser.SetInfo

Wscript.echo "Sucessfully Set password for user TempUser"


Code above connects to the active directory object with CN=TempUser, ( connects to user TempUser) under TestOU, domain Test.com. The SetPassword will set the provided password for user and additionally we can set Password Never expires flag for that user.  

How to enable user account in Active Directory through vbscript

Below is the sample vbscript to enable the user from Active Directory

Set objOU = GetObject("LDAP://TestDC/OU=TestUO,DC=Test,DC=com")
Set objUser = GetObject("LDAP://CN=TempUser,OU=TestOU,DC=Test,DC=com")
objUser.AccountDisabled = FALSE
objUser.SetInfo
Wscript.echo "Successfully enabled TempUser from Test.Com"

Note: Before executing above script please make sure that the user with samaccount name TempUser exist in the domain and OU with name TestOU exists under Test.Com root container.

How to create User, Computer or Group in Active Directory through vbscript

Below is the sample vbscript to create the Users in Active Directory


'First connect to container (OU) under which we will create user
Set objOU = GetObject("LDAP://TESTDC/OU=TestOU,DC=TEST,DC=COM")
username="TempUser"
Set objUser = objOU.Create("User", "cn="&username)
objUser.Put "sAMAccountName", username
objUser.Put "displayName", "Temp Name"
objUser.SetInfo
Wscript.echo "Created TempUser in Domain Test.com, having domain controller TESTDC"


Above code first connects to the container under which we are going to create user. Then we create the user with CN=TempUser, this will create user object under TestOU, domain Test.com, having domain controller TestDC. Once object is created, we have set some of the user properties such as samAccountName, displayName. SetInfo will save the modified properties of the created object.

Note: Before executing above script please make sure that the user with samaccount name TempUser does not exist in the domain and OU with name TestOU exists under Test.Com root container.

To create Computer or Group objects follow the same script except change the type of object to be created such as for computer use:
Set objComp = objOU.Create("Computer", "cn="&computername)
And for Group:
Set objGrp = objOU.Create("Group", "cn="&groupname)

Locking your remote computer

Using simple VB Script you can lock your computer. Following is the code to lock the computer


 On Error Resume Next


Set objShell = CreateObject("Wscript.Shell")
objShell.Run "%windir%\System32\rundll32.exe user32.dll,LockWorkStation"


Now the challenge is, how can you use this script to lock the remote computer. This is pretty simple, using VB Script, copy this lock script to a remote computer, then execute the script on remote computer, this will lock the remote computer.



Const OverwriteExisting = TRUE

Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "<LockScriptPath>", _
    "\\<Remote_computer_Name>\path_to_copy_file", OverWriteExisting

strComputer = "<Remote_computer_Name>"
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")

Error = objWMIService.Create _
    ("cscript path_to_copy_file", null, null, _
        intProcessID)

In Above code replace:
LockScriptPath: With the local machine path of lock script ( code given above)
Remote_computer_Name : Name or IP address of remote computer to lock.
path_to_copy_file : Path on remote computer where you can copy the lock script. 



     

VB Script for Creating New Folder

Here is a sample VB Script code for creating new directory.
The code will launch a dialog box for input as directory name. If you pass correct name and press OK, directory will get created.



Option Explicit
Dim objFSO, objFolder, strDirectory
strDirectory = InputBox("This is the the input box will say") 
if(0 = StrComp(strDirectory, "")) Then
WScript.Quit
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Directory : '" & strDirectory & "' created sucessfully."
WScript.Quit 

Caching is a technique used to store frequently accessed data in a temporary storage layer to improve system performance and reduce latency....