July 8th, 2009

Code corner has been rather quiet lately, so today I am going to share one of my favorite scripts.

On a day-to-day basis I get many people asking for me to reset their passwords which can get rather tedious,  to say the least, so I wrote this script which will reset a user account in a specific OU and force a password change on login.

If you want to use this script just change the OU and Domain to match your own, and of course the password you want it to be reset to.

The script also prompts you to confirm who you want to reset by showing the users full name (pulled from AD)

Copy and paste or hit up the download link below.

on error resume next
User = Inputbox("Enter Username")

Set objUser = GetObject("LDAP://cn=" & User & ",ou=Users,dc=AD_DOMAIN")
if err.number <> 0 Then
msgbox "User Not Found"
wscript.quit 0
else
Result = msgbox("Reset " & objUser.Fullname & "'s Password?", vbyesno)
If Result = vbno then wscript.quit 0
objUser.SetPassword("12345678")
objUser.Put "PwdLastSet", 0
objUser.SetInfo
msgbox "Password Reset"
wscript.quit 0
end if

Set objUser = Nothing
113 Downloads : UserReset 442 bytes
May 22nd, 2009

Code CornerIts been a week or so since I’ve added to the Code Corner so today’s entry is a small, simple but useful script for shutting down a Windows PC using VBS and WMI.

A good use for this script is to shutdown all PC’s on a domain or shutdown a room by OU.  The possibilites are endless!

Enjoy!

strPCName = "Computer_Name"

set colOS = getobject("winmgmts:{impersonationlevel=impersonate,(shutdown)}//" & strPCName).instancesof("win32_operatingsystem")
For each objOS in colOS
objOS.win32shutdown(1)
Next
May 11th, 2009

Environment variables, it’s one of those things you not often have to touch, but lately it seems i’ve had to make some manual changes all too often.  So here comes the code tip for today!

The following code looks at the PATH variable but can access any of the environment variables the system holds.

Set WshShell = CreateObject("WScript.Shell")
Set WshSystemEnv = WshShell.Environment("SYSTEM")
WshSystemEnv("PATH") = WshSystemEnv("PATH") & ";C:\Program Files\Mozilla Firefox"

Note: Environment variables must be seperated with a ;

The above code gets the current set string for the environment variable PATH and then adds to it.

Of course you could use this method for other things too, for example finding out if a variable has already been set etc.

April 30th, 2009

I’ve seen a few questions being asked around the interwebs on how to connect to a MySql database with alot of people giving up, or rather, not getting any answers on how to do it. It is possible and here’s how you do it!

First off, you will need to download and install the MySql Driver (which is a free download available here).

Once you have installed the driver you are pretty much good to go!

The next step is to use a script to connect to a database.  The following example is connecting to a local MySql server I use.  It also shows how to run a query against that database.  Enjoy!

Set conn = createobject("ADODB.Connection")
conn.open = "DRIVER={MySQL ODBC 5.1 Driver};"_
& "SERVER=localhost;"_
& "DATABASE=test;"_
& "UID=root;PWD=; OPTION=35;"

Set rs = createobject("ADODB.Recordset")
rs.Open "SELECT * FROM computers", conn

do while not rs.eof
msgbox rs("computer_name")
rs.movenext
loop

rs.close
conn.close 
April 29th, 2009

Following up my previous post on converting Bytes to MB, here is a function which works in the same way but returns a value in GB instead.

Once again feel free to use and pass around, comments are most welcome!

Function ConvertToGB(IncBytes, DecPlaces, ShowText)
	If IsNumeric(IncBytes) Then
		If Not IsNumeric(DecPlaces) Then
			DecPlaces = 0
		End If
		IncBytes = Round(IncBytes/1073741824, DecPlaces)
		If ShowText = True Then
			ConvertToGB = IncBytes & "GB"
		Else
			ConvertToGB = IncBytes
		End If
	End If
End Function

Usage:

'The Following Code Returns "1.15GB"
Result = ConvertToGB(123467890, 2, True)
April 29th, 2009

I’ve been working with VBS alot lately and decided to make a nice little function to convert bytes in to MB as it was becoming a pain to convert every value.

And here it is, feel free to use and share, if you liked it or just want to say thanks then please leave a comment :)

Function ConvertToMB(IncBytes, DecPlaces, ShowText)
	If IsNumeric(IncBytes) Then
		If Not IsNumeric(DecPlaces) Then
			DecPlaces = 0
		End If
		IncBytes = Round(IncBytes/1048576, DecPlaces)
		If ShowText = True Then
			ConvertToMB = IncBytes & "MB"
		Else
			ConvertToMB = IncBytes
		End If
	End If
End Function

Usage:

'ConvertToMB(Bytes,Decimal Places, Return value + "MB" True/False)
'The Following Returns "117.74MB"
Result = ConvertToMB(123456789,2, True)