VBScript
VBScript
Session 18
Session 18
?What we learn last session
Subjects for session 18
Subjects for session 18
WshNetwork Object
WshNetwork Object
Mapping a nwtwork printer.Mapping a nwtwork printer.
Mapping a network drive.Mapping a network drive.
WshShell Object
WshShell Object
Environment Varibles.Environment Varibles.
Creating shorcuts.Creating shorcuts.
Special folder.Special folder.
Using the registry.Using the registry.
WshNetwork Object
WshNetwork Object
Provides access to the shared resourcesProvides access to the shared resources on the network to which your computer is on the network to which your computer is connected.
connected.
You create aYou create a WshNetworkWshNetwork object whenobject when you want to connect to network shares you want to connect to network shares and network printers, disconnect from and network printers, disconnect from network shares and network printers, network shares and network printers,
map or remove network shares, or access map or remove network shares, or access information about a user on the network. information about a user on the network.
WshNetwork Object
WshNetwork Object
The following example demonstratesThe following example demonstrates displaying the domain name, computer displaying the domain name, computer name, and user name for the current name, and user name for the current
computer system using the WshNetwork computer system using the WshNetwork object.
object.
Set
Set objWshNetwork =objWshNetwork = CreateObjectCreateObject("WScript.Network")("WScript.Network") MsgBox
MsgBox "Domain = " "Domain = " & objWshNetwork.UserDomain& objWshNetwork.UserDomain MsgBox
MsgBox "Computer Name = " "Computer Name = " & objWshNetwork.ComputerName& objWshNetwork.ComputerName MsgBox
WshNetwork Object
WshNetwork Object
AddWindowsPrinterConnection Method
AddWindowsPrinterConnection Method
Adds a Windows-based printer connection to yourAdds a Windows-based printer connection to your computer system.
computer system.
SyntaxSyntax
object object ..AddWindowsPrinterConnectionAddWindowsPrinterConnection(( strPrinterPathstrPrinterPath ))
Using this method is similar to using the PrinterUsing this method is similar to using the Printer option on Control Panel to add a printer
option on Control Panel to add a printer connection.
connection.
Unlike theUnlike the AddPrinterConnectionAddPrinterConnection method, thismethod, this method allows you to create a printer connection method allows you to create a printer connection without directing it to a specific port, such as
without directing it to a specific port, such as LPT1.
LPT1.
WshNetwork Object
WshNetwork Object
MapNetworkDrive Method
MapNetworkDrive Method
Adds a shared
Adds a shared network drive to your
network drive to your
computer system.
computer system.
Syntax
Syntax
object object ..MapNetworkDriveMapNetworkDrive((strLocalNastrLocalNa
me
me,, strRemoteNamestrRemoteName, [, [bUpdateProfilebUpdateProfile],], [[strUser strUser ], [], [strPassword strPassword ])])
An attempt to map a non-shared
An attempt to map a non-shared
network drive results in an error.
network drive results in an error.
WshShell Object
WshShell Object
You create aYou create a WshShellWshShell object wheneverobject whenever you want to run a program locally,
you want to run a program locally,
manipulate the contents of the registry, manipulate the contents of the registry, create a shortcut, or access a system create a shortcut, or access a system folder.
folder.
TheThe WshShellWshShell object provides theobject provides the
Environment
Environment collection.collection.
This collection allows you to handleThis collection allows you to handle environm
environmental variables (such ental variables (such asas WINDIR, PATH, or PROMPT).
WshShell Object
WshShell Object
CreateShortcut Method
CreateShortcut Method
Creates a new shortcut, or opens Creates a new shortcut, or opens an existing shortcut.an existing shortcut.
SyntaxSyntax
object object ..CreateShortcutCreateShortcut((strPathnamestrPathname))
TheThe CreateShortcutCreateShortcut method returns either amethod returns either a
WshShortcut
WshShortcut object or aobject or a WshURLShortcutWshURLShortcut object.object.
Simply calling theSimply calling the CreateShortcutCreateShortcut method does notmethod does not
result in the creation of a shortcut. result in the creation of a shortcut.
The shortcut object and changes you may have madeThe shortcut object and changes you may have made
to it are stored in memory until you save it to
to it are stored in memory until you save it to disk withdisk with the
WshShell Object
WshShell Object
Special Folders Property
Special Folders Property
Returns aReturns a SpecialFoldersSpecialFolders object (a collection of object (a collection of
special folders). special folders).
SyntaxSyntax
object.object.SpecialFoldersSpecialFolders(objWshSpecialFolders)(objWshSpecialFolders)
TheThe WshSpecialFoldersWshSpecialFolders object is a collection.object is a collection.
It contains the entire It contains the entire set of Windows special folders,set of Windows special folders,
such as the Desktop
such as the Desktop folder, the Start Menu folder, andfolder, the Start Menu folder, and the Personal Documents
the Personal Documents folder.folder.
The special folder name is used to index into theThe special folder name is used to index into the
collectio
collection to retrieve the n to retrieve the special folder you want.special folder you want.
TheThe SpecialFoldersSpecialFolders property returns an empty stringproperty returns an empty string
if the requested folder (
WshShell Object
WshShell Object
Special Folders Property
Special Folders Property
The following special folders are The following special folders are available:available:
AllUsersDesktopAllUsersDesktop AllUsersStartMenuAllUsersStartMenu AllUsersProgramsAllUsersPrograms AllUsersStartupAllUsersStartup DesktopDesktop FavoritesFavorites FontsFonts MyDocumentsMyDocuments NetHoodNetHood PrintHoodPrintHood ProgramsPrograms RecentRecent SendToSendTo StartMenuStartMenu StartupStartup TemplatesTemplates
WshShell Object
WshShell Object
Using the Registry
Using the Registry
RegRead method.RegRead method.
Returns the value of a key or value-name fromReturns the value of a key or value-name from the registry.
the registry.
RegDelete method.RegDelete method.
Deletes a key or one of its values from theDeletes a key or one of its values from the registry.
registry.
RegWrite methodRegWrite method
Creates a new key, adds another value-nameCreates a new key, adds another value-name to an existing key (and assigns it a value), or to an existing key (and assigns it a value), or changes the value of an existing value-name. changes the value of an existing value-name.
WshShell Object
WshShell Object
Registry Abbreviations
Registry Abbreviations
Abbreviation Abbreviation Root key NameRoot key Name
HKCU HKCU HKEY_CURRENT_USER HKEY_CURRENT_USER HKLM HKLM HKEY_LOCAL_MACHINE HKEY_LOCAL_MACHINE HKCR HKCR HKEY_CLASSES_ROOT HKEY_CLASSES_ROOT HKEY_USERS HKEY_USERS HKEY_USERS HKEY_USERS HKEY_CURRENT_CONFIG HKEY_CURRENT_CONFIG HKEY_CURRENT_ HKEY_CURRENT_CONFIGCONFIG
WMI
WMI
Windows Management Instrumentation
Windows Management Instrumentation
Windows® Management Instrumentation (WMI) is aWindows® Management Instrumentation (WMI) is a
component of the Microsoft® Windows® operating system component of the Microsoft® Windows® operating system and is the M
and is the Microsoft implementation of Web-Based Enterpriseicrosoft implementation of Web-Based Enterprise Management (WBEM), which is an industry initiative to
Management (WBEM), which is an industry initiative to develop a standard technology for accessing management develop a standard technology for accessing management information in an enterprise environment.
information in an enterprise environment.
WMI uses the Common Information Model (CIM) industryWMI uses the Common Information Model (CIM) industry standard to represent systems, applications, networks, standard to represent systems, applications, networks, devices, and other managed components.
devices, and other managed components.
You can use WMI to automate administrative tasks iYou can use WMI to automate administrative tasks in ann an enterprise environment.
enterprise environment.
WMI can be used in all Windows-based applications, and isWMI can be used in all Windows-based applications, and is most useful in enterprise
WMI
WMI
WMI Classes
WMI Classes
MicrosoftMicrosoft®® WindowsWindows®® classes give you the means toclasses give you the means to
manipulate a variety of objects. The following identifies the manipulate a variety of objects. The following identifies the categories of Windows classes.
categories of Windows classes.
Computer system hardware - Classes that Computer system hardware - Classes that representrepresent hardware related objects.
hardware related objects.
Operating System - Classes that represent operating systemOperating System - Classes that represent operating system related objects.
related objects.
Installed Applications - Classes that reInstalled Applications - Classes that represent softwarepresent software related objects.
related objects.
WMI Services ?Management - Classes used to manage WMI.WMI Services ?Management - Classes used to manage WMI.
Performance Counters - Classes that represent formatted andPerformance Counters - Classes that represent formatted and raw performance data.
WMI
WMI
Win32_OperatingSystem Class
Win32_OperatingSystem Class
TheThe Win32_OperatingSystemWin32_OperatingSystem WMIWMI class represents an operating system class represents an operating system
installed on a Windows computer system. installed on a Windows computer system.
Any operating system that can beAny operating system that can be installed on a Windows system is a installed on a Windows system is a descendent or member of this class. descendent or member of this class.
If a computer has multiple operatingIf a computer has multiple operating
systems installed, this class returns only systems installed, this class returns only an instance for the
an instance for the currently activecurrently active operating system.
WMI
WMI
Win32_OperatingSystem Class
Win32_OperatingSystem Class
Methods
Methods
RebootReboot ShutDownShutDown SetDateTimeSetDateTime Win32_ShutdownWin32_Shutdown
For more information
For more information
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_o
WMI
WMI
Example
Example
Const
Const WMI_REBOOT = 2WMI_REBOOT = 2 strComputer = "."
strComputer = "." Set
Set objWMIServiobjWMIService ce == GetObjectGetObject("winmgmts:\\" & strComputer & "\root\cimv2")("winmgmts:\\" & strComputer & "\root\cimv2") Set
Set colItems = colItems = objWMIServicobjWMIService.ExecQuery("e.ExecQuery("Select * Select * from Win32_OperatingSystem",,from Win32_OperatingSystem",,48)48) For
For EachEach objItemobjItem inin colItemscolItems MsgBox
MsgBox "LastBootUpTime: " & objItem.LastBootUpTime"LastBootUpTime: " & objItem.LastBootUpTime MsgBox
MsgBox "FreePhysicalMemory: " & objItem.FreePhysicalMemory"FreePhysicalMemory: " & objItem.FreePhysicalMemory objWMIServic
objWMIService.ExecMethoe.ExecMethod d objItem ,objItem , ““ShutDown"ShutDown" objItem.Wi
objItem.Win32Shutdown(n32Shutdown(WMI_REBOOT)WMI_REBOOT) Next
Make sure to visit us
Make sure to visit us
Tutorials
Tutorials
Articles
Articles
Projects
Projects