Saturday, March 28, 2009

Scripting Q&A

Q: How can I automate the FTP process so it runs on scheduler without user intervention?
A: The FTP.EXE (File Transfer Protocol) command line utility comes standard with the Windows Operating system. Because it's a command line utility, it allows for many options to be entered at run time (simply enter FTP /? at the command prompt for a complete list). When using the -s:filename switch allows for the commands to be read from the filename specified. For example, first create a text file called myFTP.prm with the following commands:

open www.myftpsite.com
myUserID
myPassword
prompt off
binary
mput myfile*.txt

From a command line, execute FTP -s:myFTP.prm. Please note there's no specific extension for file containing the FTP commands. For more info on this, please visit http://support.microsoft.com/kb/96269


Q: How can I execute a command line utility from with a VB Script?
A: The CMD.EXE is a command line utility used for running other command line applications. Typing cmd /? will reveal a variety of command line options for executing the command shell. Listed below is a sample VB script for doing this:

Option Explicit
Dim return, objShell
Set objShell = WScript.CreateObject("WScript.Shell")
return = objShell.Run("cmd /C DIR > DirLog.txt", , True)

After saving these commands into a .vbs file, they can be executed by running "cscript myFile.vbs". Please note the file extension must be .vbs.


Q: Using a VBS script, how can I access registry keys?
A: Listed below is a sample script for accessing registry keys.

Set SysVarReg = WScript.CreateObject("WScript.Shell")
value = SysVarReg.RegRead ("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\Windows Presentation Foundation\Hosting\Hosts\v3.0")
WScript.Echo value

After saving these commands into a .vbs file, they can be executed by running "cscript myFile.vbs". Please note the file extension must be .vbs.

Another useful example of scripting is to query all the processes running on the current machine and output them to the console.

strComputer = "."
Set objServices = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set objProcessSet = objServices.ExecQuery("SELECT Name FROM Win32_Process",,48)

For Each Process in objProcessSet
WScript.Echo Process.Name
Next

For more information on scripting, visit Microsoft's Scripting Center at http://www.microsoft.com/technet/scriptcenter/default.mspx

No comments:

Post a Comment