Saturday, September 29, 2007

VBScript to read MYSQL database

' Script start

Const adClipString = 2 ' 00000002
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\imexportmysql.txt"
sFSpec = oFS.GetAbsolutePathName( sFSpec )
sFSpec = Replace( sFSpec, "\", "/" )
Dim oCNCT : Set oCNCT = CreateObject( "ADODB.Connection" )
Dim sCS : sCS = "DRIVER={MySQL ODBC 3.51 Driver};" _
+ "Server=localhost;" _
+ "Port=3306;" _
+ "Option=16384;" _
+ "Stmt=;" _
+ "Database=test;" _
+ "Uid=;" _
+ "Pwd=;"
Dim sSQL
oCNCT.Open sCS
If oFS.FileExists( sFSpec ) Then oFS.DeleteFile sFSpec
sSQL = "SELECT * FROM tblPerson INTO OUTFILE '" + sFSpec + "'"
oCNCT.Execute sSQL
WScript.Echo sSQL
sSQL = "DELETE FROM tblPerson"
oCNCT.Execute sSQL
WScript.Echo sSQL
sSQL = "SELECT COUNT(iId) FROM tblPerson"
WScript.Echo sSQL, "=>", oCNCT.Execute( sSQL ).Fields( 0 ).Value
sSQL = "LOAD DATA LOCAL INFILE '" + sFSpec + "' INTO TABLE tblPerson"
oCNCT.Execute sSQL
WScript.Echo sSQL
sSQL = "SELECT * FROM tblPerson"
WScript.Echo sSQL
WScript.Echo oCNCT.Execute( sSQL ).GetString( adClipString, , vbTab, vbCrLf, "NULL" )
End if
oCNCT.Close

output

dbcnt::doImExportMySQL()
SELECT * FROM tblPerson INTO OUTFILE 'M:/trials/02dbcnct/imexportmysql.txt'
DELETE FROM tblPerson
SELECT COUNT(iId) FROM tblPerson => 0
LOAD DATA LOCAL INFILE 'M:/trials/02dbcnct/imexportmysql.txt' INTO TABLE tblPerson
SELECT * FROM tblPerson
1 A
2 B
3 m
4 e
5 K

Script from : http://www.visualbasicscript.com/fb.aspx?m=50814

Queries on customization. Open http://Orangescripts.blogspot.com and post it

VBScript - To get service tag of your desktop

Usage: Script.vbs computer1 computer2 computer3

'Script start

For i = 0 to wscript.arguments.count-1
sComputer = wscript.arguments(i)
Set oWMI = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
Set oCol = oWMI.ExecQuery("Select * from Win32_ComputerSystemProduct")
For Each x in oCol
wscript.echo sComputer & vbtab & x.IdentifyingNumber
Next
Next

'Script End

content from pooradmin.com

Queries on customization. Open http://Orangescripts.blogspot.com and post it .

VBScript to add entry in a Registry path

'Script start

const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "MyNetworkComputername"Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")strKeyPath = "Softwares\Microsoft\Windows\Currentversion\Run"
strValueName = "StartMyProgram"
strVal="myprogram.exe"
oReg.SetstringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strVal

'Script End

Queries on customization. Open http://orangescripts.blogspot.com/ and post it .

Friday, September 28, 2007

WScript.exe and CScript.exe Options

For the most part, options listed in the following table are applicable to both WScript.exe and CScript.exe. Exceptions are noted.

/B Batch mode; suppresses command-line display of user prompts and script errors. Default is Interactive mode.
/D Turns on the debugger.
/E:engine Executes the script with the specified script engine.
/H:CScript or /H:WScript Registers CScript.exe or WScript.exe as the default application for running scripts. If neither is specified, WScript.exe is assumed as the default.
/I Default. Interactive mode; allows display of user prompts and script errors Opposite of Batch mode.
/Job: Runs the specified JobID from the .wsf file.
/logo Default. Displays a banner. Opposite of nologo.
/nologo Prevents display of an execution banner at run time. Default is logo.
/S Saves the current command-line options for this user.
/T:nn Enables time-out: the maximum number of seconds the script can run. The default is no limit. The /T parameter prevents excessive execution of scripts by setting a timer. When execution time exceeds the specified value, CScript interrupts the script engine using the IActiveScript::InterruptThread method and terminates the process.
/U Used with Windows NT and Windows 2000 to force the command line output to be in Unicode. There is no way for CScript to determine whether to output in Unicode or ANSI; it defaults to ANSI.
/X Launches the program in the debugger.
/? Displays a brief description of and usage information for command parameters (the usage information).

Running Scripts from the Command Prompt

Content from Microsoft site.

Windows Script Host enables you to run scripts from the command prompt. CScript.exe provides command-line switches for setting script properties.

Procedures

To run scripts using CScript.exe
Type a command at the command prompt using the following syntax:

Copy Code

cscript [host options...] [script name] [script options and parameters]

Host Options enable or disable various Windows Script Host features. Host options are preceded by two slashes (//).Script name is the name of the script file with extension and necessary path information, for example, d:\admin\vbscripts\chart.vbs. Script options and parameters are passed to the script. Script parameters are preceded by a single slash (/).

Each parameter is optional; however, you cannot specify script options without specifying a script name. If you do not specify parameters, CScript displays the CScript syntax and the valid host parameters.

CScript Example

Suppose, for the purposes of this example, that you have copied the Chart.vbs sample script to the following folder on your computer:

Copy Code

c:\sample scripts\chart.vbs
You can run the script with and without a logo as follows.

To run a script with or without a logo
Start the MS-DOS command prompt.

Enter the following commands at the command prompt (modify accordingly if your sample scripts are located in a different folder):

Copy Code

cscript //logo c:\"sample scripts"\chart.vbs

cscript //nologo c:\"sample scripts"\chart.VBScript

Thursday, September 27, 2007

VBScript for Remote Shutdown

'Script Start


Set OpSysSet = GetObject("winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//Your ip address").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
for each OpSys in OpSysSet
OpSys.Reboot()
Next



'Script End

Got from website www.vittorio.tk

How to execute a VBScript

VBScript to move files according to date

'Script Start

' Covet - Use this script carefully.


Const OverwriteExisting = True

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(" Your source folder - HERE - full path ")



For Each file In f.Files

if DateDiff("m",file.DateCreated, "09/01/2007") = 0 then 'change date accordingly

FSO.CopyFile file.path , " Destination folder with full path and \ at end - HERE " , OverwriteExisting
file.delete

End if


Next



For Each subf In f.subfolders

For Each file In subf.Files

if DateDiff("m",file.DateCreated, "09/01/2007") = 0 then 'change date accordingly

FSO.CopyFile file.path , " Destination folder with full path and \ at end - HERE " , OverwriteExisting
file.delete
End if


Next

Next

wscript.echo "End"





'Script End



How to execute a VBScript

VBScript to find file information

'Script Start


Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("C:\")
For Each file In f.Files
MsgBox file.Name
MsgBox file.DateCreated
MsgBox file.DateLastAccessed
MsgBox file.DateLastModified
MsgBox file.Path
MsgBox file.ShortName
MsgBox file.ShortPath
MsgBox file.Size
Next





'Script End



How to execute a VBScript

VBScript to assign DHCP ip address

'Script Start


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = true")

goDHCP()


Sub goDHCP()
For Each objItem In colItems

errEnable = objItem.EnableDHCP()
If errEnable = 0 Then
Wscript.Echo "DHCP has been enabled."
Else
Wscript.Echo "DHCP could not be enabled."
End If
Msgbox "setting DNS"
errDNS = objitem.SetDNSServerSearchOrder()
errDDNS = objItem.SetDynamicDNSRegistration
msgbox "DNS Set"
errRenew = objItem.RenewDHCPLease
msgbox "renew called"
If errRenew = 0 Then
Wscript.Echo "DHCP lease renewed."
Else
Wscript.Echo "DHCP lease could not be renewed." & err.number & err.description
End If
Next
End Sub





'Script End

Got from website www.vittorio.tk

How to execute a VBScript

VBScript to assign static IP address

'Script Start


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = true")

'// Static Ip addrerss settings
strIPAddress = Array("192.168.36.36")
strSubnetMask = Array("255.255.252.0")
strGateway = Array("192.168.36.1")
strGatewayMetric = Array(1)
strDNSservers = Array ("192.168.76.33","192.168.76.34")


goStatic


Sub goStatic()
For Each objItem In colItems
errEnable = objItem.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objItem.SetGateways(strGateway, strGatewaymetric)
'// This line removes all DNS servers
errDNS = objitem.SetDNSServerSearchOrder()
errDNS = objitem.SetDNSServerSearchOrder(strDNSServers)
If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Next
'// ReQuery to see changes
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = true")
For Each objItem In colItems
If Not IsNull(objitem.ipaddress) Then
For i = LBound(objitem.IPAddress) To UBound(objitem.IPAddress)
WScript.Echo "New IP is " & objitem.IPAddress(i)
Next
End If
If Not IsNull(objitem.ipSubnet) Then
For i = LBound(objitem.IPSubnet) To UBound(objitem.IPSubnet)
WScript.Echo "New subnet is " & objitem.IPSubnet(i)
Next
End If
If Not IsNull(objitem.DefaultIPGateway) Then
For i = LBound(objitem.DefaultIPGateway) To UBound(objitem.DefaultIPGateway)
WScript.Echo "Gateway is " & objitem.DefaultIPGateway(i)
Next
End If
Next
End Sub





'Script End

Got from website www.vittorio.tk

How to execute a VBScript

VBScript to get size of all subfolders in a directory

'Script Start


BaseDirectory = "D:\"

CheckFolder(BaseDirectory)

Function CheckFolder(Directory)
Set sss = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(Directory)
For each sf In f.subfolders
value = round(sf.Size/1048576,2)

wscript.echo sf.name & " " & Value & " Mb"

Next
End Function




'Script End



How to execute a VBScript

VBScript to Retrieve a file from an URL and save it to the Hard Disk

'Script Start


URL = "http://sitename/filename.txt"
saveTo = "C:\filename.txt"

Set Obj1 = CreateObject("MSXML2.ServerObj1")
Obj1.open "GET", URL, false
Obj1.send()
Set Obj2 = CreateObject("ADODB.Stream")
Obj2.Open
Obj2.Type = 1 'adTypeBinary
Obj2.Write Obj1.ResponseBody 'Give the XML string to the ADO Stream
Obj2.Position = 0 'Set the stream position to the start
Set FSO = Createobject("Scripting.FileSystemObject")
if fso.Fileexists(saveTo) then Fso.DeleteFile hdLocation
set FSO = Nothing
Obj2.SaveToFile saveTo
Obj2.Close
Set Obj2 = Nothing
Set Obj1 = Nothing




'Script End

Got from the site www.vittorio.tk

How to execute a VBScript

VBScript to get total physical memory of your desktop

'Script Start


Set obj = GetObject("winmgmts:").InstancesOf("Win32_PhysicalMemory")
i = 1
For Each obj2 In obj
memTmp1 = obj2.capacity / 1024 / 1024
wscript.echo "Module" & i & ": " & memTmp1 & " MB"
TotalRam = TotalRam + memTmp1
i = i +1
Next
wscript.echo "Total RAM: " & TotalRam & " MB"



'Script End



How to execute a VBScript

VBScript to send Email

'Script Start


Set objEmail = CreateObject("CDO.Message")

'Change from field
objEmail.From = "admin1@fabrikam.com"

'change to field
objEmail.To = "admin2@fabrikam.com"

'change subject
objEmail.Subject = "Server down"

'change textbody
objEmail.Textbody = "Server1 is no longer accessible over the network."


objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "Your mail server IP - HERE"
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send



'Script End



How to execute a VBScript

VBScript Running Scripts on Remote Computers

'Script Start


'Substitute server

strRemoteComputer = "servername"

'Substitute script name

strWorkerScript = "scripttorunonremote.vbs"
Set objWshController = WScript.CreateObject("WshController")
Set objRemoteScript = objWshController.CreateScript(strWorkerScript, strRemoteComputer)
objRemoteScript.Execute

Do While Not objRemoteScript.Status = 2
Wscript.Sleep(100)
Wscript.Echo "Remote script not yet complete."
Loop




'Script End



How to execute a VBScript

VBScript to set default printer in your desktop

'Script Start


Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.SetDefaultPrinter("\\servername\printername")





'Script End



How to execute a VBScript

VBScript to enumerate available printers on your desktop

'Script Start


Set objNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = objNetwork.EnumPrinterConnections
For i = 0 to colPrinters.Count -1 Step 2
Wscript.Echo colPrinters.Item(i) & vbTab & colPrinters.Item (i + 1)
Next





'Script End



How to execute a VBScript

VBscript Remove a Windows based printer

'Script Start


Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.RemovePrinterConnection "\\servername\printername"



'Script End



How to execute a VBScript

VBScript add a windows based printer

'Script Start


Set objNetwork = Wscript.CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\\HRServer01\Printer1"


'Script End



How to execute a VBScript

VBScript to display all shared drives

'Script start

Set objNetwork = WScript.CreateObject("WScript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
For i = 0 to colDrives.Count-1 Step 2
Wscript.Echo colDrives.Item(i) & vbTab & colDrives.Item (i + 1)
Next

'Script end

How to execute a VBScript

VBScript to Unmap a Network Drive

'Script start

Set objNetwork = WScript.CreateObject("Wscript.Network")
objNetwork.RemoveNetworkDrive "G:"

'Script end



How to execute a VBScript

VBScript to Map a network drive

'Script start

Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "S:", file://server/share

'Script end

Edit //server/share according to you needs. Current user should have permission to the mentioned folder

How to execute a VBScript

VBScript to find Current username

'Script Start

Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "Current User Name: " & WshNetwork.UserName

'Script End

How to execute a VBScript

How to Execute a VBScript

STEP 1 Copy the script into a notepad
STEP 2 Click save target as - In the file name type "name.vbs" [along with double quotes]
STEP 3 Choose drive
STEP 4 Goto the place where u saved and just double click name.vbs

Wednesday, September 26, 2007

VB Script to get computer name from your workstation


Set WshNetwork = WScript.CreateObject("WScript.Network")
WScript.Echo "You Computer Name = " & WshNetwork.ComputerName


How to execute a VBScript