VBScript Session 18 1 What we learn last session? 2 Subjects for session 18 WshNetwork Mapping a nwtwork printer. Mapping a network drive. WshShell Object Object Environment Varibles. Creating shorcuts. Special folder. Using the registry. WMI 3 WshNetwork 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. 4 WshNetwork 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.ComputerName MsgBox "User Name = " & objWshNetwork.UserName 5 WshNetwork 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. 6 WshNetwork Object MapNetworkDrive Method Adds a shared network drive to your computer system. Syntax object.MapNetworkDrive(strLocalNa me, strRemoteName, [bUpdateProfile], [strUser], [strPassword]) An attempt to map a non-shared network drive results in an error. 7 WshShell 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). 8 WshShell Object CreateShortcut 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. 9 WshShell Object Special 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. 10 WshShell Object Special Folders Property The following special folders are available: AllUsersDesktop AllUsersStartMenu AllUsersPrograms AllUsersStartup Desktop Favorites Fonts MyDocuments NetHood PrintHood Programs Recent SendTo StartMenu Startup Templates 11 WshShell Object Using the Registry RegRead method. RegDelete method. Returns the value of a key or value-name from the registry. 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. 12 M A C H I N E WshShell Object Registry Abbreviations HH KK Root key Name E C YR HKEY_CURRENT_USER _ HKEY_LOCAL_MACHINE C HKEY_CLASSES_ROOTL A HKEY_USERS S HKEY_CURRENT_CONFIG S E S _ R O O T Abbreviation HKCU HKLM HKCR HKEY_USERS HKEY_CURRENT_CONFIG 13 WMI Windows 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. 14 WMI WMI 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. 15 WMI Win32_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. 16 WMI Win32_OperatingSystem Class Methods Reboot ShutDown SetDateTime Win32_Shutdown For more information http://msdn.microsoft.com/library/default.asp?url=/library/enus/wmisdk/wmi/win32_operatingsystem.asp 17 WMI Example 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.LastBootUpTime MsgBox "FreePhysicalMemory: " & objItem.FreePhysicalMemory objWMIService.ExecMethod objItem , “ShutDown" objItem.Win32Shutdown(WMI_REBOOT) Next 18 Make sure to visit us Tutorials Articles Projects And much more www.AdvancedQTP.com 19