Mental Jetsam

By Peter Finch

Archive for the ‘VBScript’ Category

Classic ASP code to print current server name

Posted by pcfinch on January 29, 2008

Some simple ASP classic (VB script) code to print out the current server name. Unlike using the Request.ServerVariables(“SERVER_NAME”) function, this method returns the server that the ASP code actually ran on instead of the hostname passes in the URL. This is useful if there is number of backend servers running through a load balancer with “session affinity” and you what to know which one you are talking to.

<html>
<body>
<%
dim dos, env
set shell= CreateObject ("WScript.Shell")
set enviro = shell.Environment ("PROCESS")
whoami = " " & enviro("COMPUTERNAME")
response.write "Current server " & whoami
%>
</body>
</html>

Posted in VBScript | 2 Comments »

Accessing an ADO Database table from VB Script

Posted by pcfinch on April 10, 2007

This is a simple bit of VB Script that will access a database via an ADO object and extract some information from a table. Simply create a file called something like “whatsmypasswordtoday.vbs” and you can double-click on it from explorer, or run from the command line, to check your password if you ever forgot it [not that that ever happens].

In this case the code is querying the users table of an Oracle database and extracting the password of the user called “bob”. More advanced versions of this script could be used to extract or insert information into the database and perform routine batch operations. Although you probably would not write an entire application in VB Script (grin), the following is a good example of now useful, quick and powerful VB script can be in automating everyday windows tasks.

Option Explicit
' on error resume next

dim CR, strConnection, oConn, oCmd, rs, record

CR = chr(13)

strConnection = "Driver={Microsoft ODBC for Oracle};Server=server.mydomain.com;UID=user;Pwd=password;"

Set oConn = CreateObject("ADODB.Connection")
Set oCmd = CreateObject("ADODB.Command")

oConn.Open strConnection
if (Err.Number <> 0) then
  MsgBox "Unable to connect to database" & CR &_
  "Error(" & Err.Number & ")" & CR &_
  Err.Description
  WScript.Quit
end if

Set oCmd.ActiveConnection = oConn
oCmd.CommandText = "SELECT * FROM USERS WHERE LOWER(USERNAME)='bob'"
set rs = oCmd.Execute

rs.MoveFirst
Do Until rs.EOF
  msgBox "password=" & rs.fields("password")
  rs.MoveNext
loop

oConn.Close
set oCmd = nothing
set oConn = nothing

Posted in VBScript | Leave a Comment »

Basic network information using VBScript

Posted by pcfinch on April 1, 2007

The following VBScript runs on MS/Windows and displays the current network information available from the WScript.Network object. The code produces a simple messagebox to display the information. A good reference for this information can be found at www.winguides.com.


Set WshNetwork = WScript.CreateObject("WScript.Network")
CR = chr(13)
'
' Get printers
'
Set Printers = WshNetwork.EnumPrinterConnections
strPrinters = ""
For i = 0 to Printers.Count - 1 Step 2
strPrinters = strPrinters& "  " & Printers.Item(i) & "(" & Printers.Item(i+1) & ")" & CR
Next

'
' Get drives
'
Set Drives = WshNetwork.EnumNetworkDrives
strDrives = ""
For i = 0 to Drives.Count - 1 Step 2
strDrives = strDrives & "  " & Drives.Item(i) & "(" & Drives.Item(i+1) & ")" & CR
Next

MsgBox "WshNetwork.ComputerName = " & WshNetwork.ComputerName & CR &_
"WshNetwork.UserDomain = " & WshNetwork.UserDomain & CR &_
"WshNetwork.UserName = " & WshNetwork.UserName & CR &_
"WshNetwork.AddPrinterConnection (Method) "& CR &_
"WahNetwork.AddWindowsPrinterConnection (Method) " & CR &_
"WshNetwork.EnumPrinterConnections (Method) " & CR &_
strPrinters &_
"WshNetwork.RemovePrinterConnection (Method) " & CR &_
"WshNetwork.SetDefaultPrinter (Method) " & CR &_
"WshNetwork.EnumNetworkDrives (Method) " & CR &_
strDrives &_
"WshNetwork.MapNetworkDrive (Method) " & CR &_
"WshNetwork.RemoveNetworkDrive (Method) "

Posted in VBScript | Leave a Comment »