Title: VBScript
1VBScript
2What we learn last session?
3Subjects for session 18
- WshNetwork Object
- Mapping a nwtwork printer.
- Mapping a network drive.
- WshShell Object
- Environment Varibles.
- Creating shorcuts.
- Special folder.
- Using the registry.
- WMI
4WshNetwork Object
- Provides access to the shared resources on the
network to which your computer is connected. - You create a WshNetwork object when you want to
connect to network shares and network printers,
disconnect from network shares and network
printers, map or remove network shares, or access
information about a user on the network.
5WshNetwork Object
- The following example demonstrates displaying the
domain name, computer name, and user name for the
current computer system using the WshNetwork
object. - Set objWshNetwork CreateObject("WScript.Network
") - MsgBox "Domain " objWshNetwork.UserDomain
- MsgBox "Computer Name " objWshNetwork.Compute
rName - MsgBox "User Name " objWshNetwork.UserName
6WshNetwork Object AddWindowsPrinterConnection
Method
- Adds a Windows-based printer connection to your
computer system. - Syntax
- object.AddWindowsPrinterConnection(
strPrinterPath ) - Using this method is similar to using the Printer
option on Control Panel to add a printer
connection. - Unlike the AddPrinterConnection method, this
method allows you to create a printer connection
without directing it to a specific port, such as
LPT1. - If the connection fails, an error is thrown.
7WshNetwork Object MapNetworkDrive Method
- Adds a shared network drive to your computer
system. - Syntax
- object.MapNetworkDrive(strLocalName,
strRemoteName, bUpdateProfile, strUser,
strPassword) - An attempt to map a non-shared network drive
results in an error.
8WshShell Object
- You create a WshShell object whenever you want to
run a program locally, manipulate the contents of
the registry, create a shortcut, or access a
system folder. - The WshShell object provides the Environment
collection. - This collection allows you to handle
environmental variables (such as WINDIR, PATH, or
PROMPT).
9WshShell ObjectCreateShortcut Method
- Creates a new shortcut, or opens an existing
shortcut. - Syntax
- object.CreateShortcut(strPathname)
- The CreateShortcut method returns either a
WshShortcut object or a WshURLShortcut object. - Simply calling the CreateShortcut method does not
result in the creation of a shortcut. - The shortcut object and changes you may have made
to it are stored in memory until you save it to
disk with the Save method.
10WshShell ObjectSpecial Folders Property
- Returns a SpecialFolders object (a collection of
special folders). - Syntax
- object.SpecialFolders(objWshSpecialFolders)
- The WshSpecialFolders object is a collection.
- It contains the entire set of Windows special
folders, such as the Desktop folder, the Start
Menu folder, and the Personal Documents folder. - The special folder name is used to index into the
collection to retrieve the special folder you
want. - The SpecialFolders property returns an empty
string if the requested folder (strFolderName) is
not available.
11WshShell ObjectSpecial Folders Property
- The following special folders are available
- AllUsersDesktop
- AllUsersStartMenu
- AllUsersPrograms
- AllUsersStartup
- Desktop
- Favorites
- Fonts
- MyDocuments
- NetHood
- PrintHood
- Programs
- Recent
- SendTo
- StartMenu
- Startup
- Templates
12WshShell ObjectUsing the Registry
- RegRead method.
- Returns the value of a key or value-name from the
registry. - RegDelete method.
- Deletes a key or one of its values from the
registry. - RegWrite method
- Creates a new key, adds another value-name to an
existing key (and assigns it a value), or changes
the value of an existing value-name.
13WshShell ObjectRegistry Abbreviations
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS HKEY_USERS
HKEY_CURRENT_CONFIG HKEY_CURRENT_CONFIG
Abbreviation Root key Name
HKCU HKEY_CURRENT_USER
HKLM HKEY_LOCAL_MACHINE
HKCR HKEY_CLASSES_ROOT
HKEY_USERS HKEY_USERS
HKEY_CURRENT_CONFIG HKEY_CURRENT_CONFIG
14WMIWindows Management Instrumentation
- Windows Management Instrumentation (WMI) is a
component of the Microsoft Windows operating
system and is the Microsoft implementation of
Web-Based Enterprise Management (WBEM), which is
an industry initiative to develop a standard
technology for accessing management information
in an enterprise environment. - WMI uses the Common Information Model (CIM)
industry standard to represent systems,
applications, networks, devices, and other
managed components. - You can use WMI to automate administrative tasks
in an enterprise environment. - WMI can be used in all Windows-based
applications, and is most useful in enterprise
applications.
15WMIWMI Classes
- Microsoft Windows classes give you the means to
manipulate a variety of objects. The following
identifies the categories of Windows classes. - Computer system hardware - Classes that represent
hardware related objects. - Operating System - Classes that represent
operating system related objects. - Installed Applications - Classes that represent
software related objects. - WMI Services ?Management - Classes used to manage
WMI. - Performance Counters - Classes that represent
formatted and raw performance data.
16WMIWin32_OperatingSystem Class
- The Win32_OperatingSystem WMI class represents an
operating system installed on a Windows computer
system. - Any operating system that can be installed on a
Windows system is a descendent or member of this
class. - If a computer has multiple operating systems
installed, this class returns only an instance
for the currently active operating system.
17WMIWin32_OperatingSystem Class
- Methods
- Reboot
- ShutDown
- SetDateTime
- Win32_Shutdown
- For more informationhttp//msdn.microsoft.com/lib
rary/default.asp?url/library/en-us/wmisdk/wmi/win
32_operatingsystem.asp
18WMIExample
- Const WMI_REBOOT 2
- strComputer "."
- Set objWMIService GetObject("winmgmts\\"
strComputer "\root\cimv2") - Set colItems objWMIService.ExecQuery("Select
from Win32_OperatingSystem",,48) - For Each objItem in colItems
- MsgBox "LastBootUpTime " objItem.LastBootUpTim
e - MsgBox "FreePhysicalMemory "
objItem.FreePhysicalMemory - objWMIService.ExecMethod objItem , ShutDown"
- objItem.Win32Shutdown(WMI_REBOOT)
- Next
19Make sure to visit us
- Tutorials
- Articles
- Projects
- And much more
- www.AdvancedQTP.com