VBscript can use WMI object to access properties of Domain and users.
===============================================
Unlock User Account
-------------------------------------------------------------------------------
This is a sample code to unlock user account when user put wrong password too many times and his account has been locked.
Code 1:
Dim oUser
Set oUser = GetObject("WinNT://DomainName/UserName")
WScript.Echo "user name:" & oUser.Name&vbcr&"Is account locked:" & oUser.IsAccountLocked
oUser.IsAccountLocked = 0
WScript.Echo "user name:" & oUser.Name&vbcr&"Is account locked:" & oUser.IsAccountLocked
A better pice of code can be found here:
http://community.spiceworks.com/scripts/show/54-unlock-user-account
Code 2:
Dim oUser
LDAPpath = ' to find LDAP distinguished name (DN) in AD
Set objUser = GetObject("LDAP://" & LDAPpath)
objUser.IsAccountLocked = False
oUser.SetInfo
===================================================
Reset User Account Password
-------------------------------------------------------------------------------
This is a sample code to reset user account password.
Dim oUser
LDAPpath = ' to find LDAP distinguished name (DN) in AD
Set objUser = GetObject("LDAP://" & LDAPpath)
strPassword = InputBox("Please put a new password.","Reset User password")
oUser.SetPassword strPassword
oUser.SetInfo
===================================================
Force User to Change Account Password at The First Login
-------------------------------------------------------------------------------
This is a sample code to reset user account password.
Dim oUser
LDAPpath = ' to find LDAP distinguished name (DN) in AD
Set objUser = GetObject("LDAP://" & LDAPpath)
objUser.PwdLastSet = 0
oUser.SetInfo
===================================================
Find LDAP distinguished name (DN) in AD
-------------------------------------------------------------------------------
I found this script code on Microsoft website.
http://gallery.technet.microsoft.com/scriptcenter/dee78632-f6d0-4be3-920f-27165fa60767
'**************************************************************
'= Created By: Devin H.
'= Function Name: distinguish Date: 10/19/2005 Version:1.1
'=
'= Variables: strObject, strType
'=
'= This function will return the distinguished name of an object stored in
'= Active Directory. This is useful when you don't know exactly where an
'= object is located or want to move something.
'=
'= Usage: Wscript.Echo distinguish("DevinH","user")
'= Returns: cn=DevinH,ou=MyUsers,dc=ScriptCentric,dc=com
'=
'**************************************************************
Function distinguish(strObject, strType)
Select case strType
Case lcase("computer")
strobject = strObject & "$"
Case lcase("user")
'Good
Case lcase("group")
'Good
Case else
Wscript.Echo "Their is an error in the script"
End Select
' Determine DNS domain name (this could be hard coded).
Set objRootDSE = getObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.get("defaultNamingContext")
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = createObject("ADODB.Connection")
Set objCommand = createObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select distinguishedname, Name, Location from 'LDAP://" & strDNSDomain & _
"' Where objectClass='" & strType & "' and samaccountname='" & strobject & "'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
distinguish = objRecordSet.Fields("distinguishedname")
objRecordSet.MoveNext
Loop
End Function
===================================================
This is my code with all these together
'*********** code begin*****************
On Error Resume Next
strUser = InputBox("Please enter a user name.","user_password")
If strUser = vbNullString then
MsgBox "Either Cancel was selected or you did not enter a user name.", 16, "User Unlock"
WScript.Quit
End If
strDomain = "myDOMAIN"
dim LDAPpath
Const STRING_OBJ_TYPE = "user"
LDAPpath = distinguish(strUser,STRING_OBJ_TYPE)
Set objUser = GetObject("LDAP://" & LDAPpath)
If Err.Number <> 0 Then
MsgBox (strUser) & " isn't a valid user name!", 48,"Reset User password"
Wscript.Quit
End If
Err.Clear
On Error Resume Next
style = vbYesNo + vbDefaultButton2
'set new password
WantNewPwd = MsgBox("Do you want to reset user's password?", style,"user_password")
If WantNewPwd = vbYes Then
strPassword = InputBox("Please put a new password.","Reset User password")
objUser.SetPassword strPassword
End If
'force user to change the password at the first time login
forceNewPsd = MsgBox("Do you want user to change password at first login?", style,"user_password")
If forceNewPsd = vbYes Then
objUser.PwdLastSet = 0
End If
UnlockAccount = MsgBox("Do you want to unlock user account?", style,"user_password")
If UnlockAccount = vbYes Then
objUser.IsAccountLocked = False
End If
objUser.SetInfo
If Err.Number <> 0 Then
MsgBox("An error has occurred. " & Err.Number)
Err.Clear
Else
MsgBox "The Password/Account Has Been Changed For " & UCase(strDomain) & "\" & UCase(strUser)
End If
Wscript.Quit
'=====================================================
'= Created By: Devin H.
'= Function Name: distinguish Date: 10/19/2005 Version:1.1
'=
'= Variables: strObject, strType
'=
'= This function will return the distinguished name of an object stored in
'= Active Directory. This is useful when you don't know exactly where an
'= object is located or want to move something.
'=
'= Usage: Wscript.Echo distinguish("DevinH","user")
'= Returns: cn=DevinH,ou=MyUsers,dc=ScriptCentric,dc=com
'======================================================
Function distinguish(strObject, strType)
Select case strType
Case lcase("computer")
strobject = strObject & "$"
Case lcase("user")
'Good
Case lcase("group")
'Good
Case else
Wscript.Echo "Their is an error in the script"
End Select
' Determine DNS domain name (this could be hard coded).
Set objRootDSE = getObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.get("defaultNamingContext")
Const ADS_SCOPE_SUBTREE = 2
Set objConnection = createObject("ADODB.Connection")
Set objCommand = createObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select distinguishedname, Name, Location from 'LDAP://" & strDNSDomain & _
"' Where objectClass='" & strType & "' and samaccountname='" & strobject & "'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
distinguish = objRecordSet.Fields("distinguishedname")
objRecordSet.MoveNext
Loop
End Function
'***************end of code*************
Thursday, June 20, 2013
Thursday, June 13, 2013
copy, move, and create files &/or folders with VBScript
We can use the methods in Object FileSystemObject or Shell to copy, move, and create files &/or folders.
The information about Object FileSystemObject is in this page:
http://msdn.microsoft.com/en-us/library/hww8txat(v=vs.84).aspx
The information about Object Shell is in this page:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb774094(v=vs.85).aspx
Example:
To copy files from one folder to another folder:
Using FileSystemObject:
===============
dim sourcePath
dim destPath
dim filesys
sourcePath="c:\temp\*.*"
destPath="d:\temp2\"
set filesys=CreateObject("Scripting.FileSystemObject")
filesys.CopyFile sourcePath, destPath, True
set filesys=nothing
================
This code copys all the files from c:\temp to c:\temp2. The script runs at the backgroud
Using Shell:
===============
dim sourcePath
dim destPath
sourcePath="c:\temp\*.*"
destPath="d:\temp2\"
dim objShell
dim objFolder
dim cFlag
set objShell = CreateObject("shell.application")
set objFolder = objShell.NameSpace(destPath)
cFlage = 48 'only copy the files
objFolder.CopyHere sourcePath,cFlag
set objShell = nothing
set objFolder = nothing
WScript.Sleep 100
==========================
The function displays a progress dialog box.
Note: for some reason, the option in the method CopyHere does not work on my computer.
The information about Object FileSystemObject is in this page:
http://msdn.microsoft.com/en-us/library/hww8txat(v=vs.84).aspx
The information about Object Shell is in this page:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb774094(v=vs.85).aspx
Example:
To copy files from one folder to another folder:
Using FileSystemObject:
===============
dim sourcePath
dim destPath
dim filesys
sourcePath="c:\temp\*.*"
destPath="d:\temp2\"
set filesys=CreateObject("Scripting.FileSystemObject")
filesys.CopyFile sourcePath, destPath, True
set filesys=nothing
================
This code copys all the files from c:\temp to c:\temp2. The script runs at the backgroud
Using Shell:
===============
dim sourcePath
dim destPath
sourcePath="c:\temp\*.*"
destPath="d:\temp2\"
dim objShell
dim objFolder
dim cFlag
set objShell = CreateObject("shell.application")
set objFolder = objShell.NameSpace(destPath)
cFlage = 48 'only copy the files
objFolder.CopyHere sourcePath,cFlag
set objShell = nothing
set objFolder = nothing
WScript.Sleep 100
==========================
The function displays a progress dialog box.
Note: for some reason, the option in the method CopyHere does not work on my computer.
Subscribe to:
Comments (Atom)