IT tutorials
 
Technology
 

Scripting Windows Home Server : Programming the WshShell Object (part 2) - Working with Registry Entries, Working with Environment Variables

9/1/2013 9:41:54 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

5. Working with Registry Entries

The Registry is one of the most crucial data structures in Windows. However, Windows isn’t the only software that uses the Registry. Most 32-bit applications use the Registry as a place to store setup options, customization values the user selected, and much more. Interestingly, your scripts can get in on the act as well. Not only can your scripts read the current value of any Registry setting, but they can use the Registry as a storage area. This enables you to keep track of user settings, recently used files, and any other configuration data that you’d like to save between sessions. This section shows you how to use the WshShell object to manipulate the Registry from within your scripts.

Reading Settings from the Registry

To read any value from the Registry, use the WshShell object’s RegRead method:

WshShell.RegRead(strName)

WshShellThe WshShell object.
strNameThe name of the Registry value or key that you want to read. If strName\), RegRead returns the default value for the key; otherwise, RegRead returns the data stored in the value. Note, too, that strName must begin with one of the following root key names: ends with a backslash (
 Short NameLong Name
 HKCRHKEY_CLASSES_ROOT
 HKCUHKEY_CURRENT_USER
 HKLMHKEY_LOCAL_MACHINE
 N/AHKEY_USERS
 N/AHKEY_CURRENT_CONFIG

The script in Listing 3 displays the name of the registered owner of this copy of Windows.

Listing 3. A Script That Reads the RegisteredOwner Setting from the Registry
Set objWshShell = WScript.CreateObject("WScript.Shell")
strSetting = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\RegisteredOwner"
strRegisteredUser = objWshShell.RegRead(strSetting)
WScript.Echo strRegisteredUser


Storing Settings in the Registry

To store a setting in the Registry, use the WshShell object’s RegWrite method:

WshShell.RegWrite strName, anyValue [, strType]

WshShellThe WshShell object.
strNameThe name of the Registry value or key that you want to set. If strName\), RegWrite sets the default value for the key; otherwise, RegWrite sets the data for the value. strName must begin with one of the root key names detailed in the RegRead method. ends with a backslash (
anyValueThe value to be stored.
strTypeThe data type of the value, which must be one of the following: REG_SZREG_EXPAND_SZ, REG_DWORD, or REG_BINARY. (the default),

The following statements create a new key named ScriptSettings in the HKEY_CURRENT_USER root:

Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.RegWrite "HKCU\ScriptSettings\", ""

The following statements create a new value named NumberOfReboots in the HKEY_CURRENT_USER\ScriptSettings key and set this value to 1:

Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.RegWrite "HKCU\ScriptSettings\NumberOfReboots", 1, "REG_DWORD"

Deleting Settings from the Registry

If you no longer need to track a particular key or value setting, use the RegDelete method to remove the setting from the Registry:

WshShell.RegDelete(strName)

WshShellThe WshShell object.
strNameThe name of the Registry value or key that you want to delete. If strName\), RegDelete deletes the key; otherwise, RegDelete deletes the value. strName must begin with one of the root key names detailed in the RegRead method. ends with a backslash (

To delete the NumberOfReboots value used in the previous example, you would use the following statements:

Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.RegDelete "HKCU\ScriptSettings\NumberOfReboots"

6. Working with Environment Variables

Windows Home Server keeps track of a number of environment variables that hold data, such as the location of the Windows folder, the location of the temporary files folder, the command path, the primary drive, and much more. Why would you need such data? One example would be for accessing files or folders within the main Windows folder. Rather than guessing that this folder is C:\Windows, it would be much easier to just query the %SystemRoot% environment variable. Similarly, if you have a script that accesses files in a user’s Documents folder, hard-coding the username in the file path is inconvenient because it means creating custom scripts for every possible user. Instead, it would be much easier to create just a single script that references the %UserProfile% environment variable. This section shows you how to read environment variable data within your scripts.

The defined environment variables are stored in the Environment collection, which is a property of the WshShell object. Windows Home Server environment variables are stored in the "Process" environment, so you reference this collection as follows:

WshShell.Environment("Process")

Listing 4 shows a script that runs through this collection, adds each variable to a string, and then displays the string.

Listing 4. A Script That Displays the System’s Environment Variables
Option Explicit
Dim objWshShell, objEnvVar, strVariables
Set objWshShell = WScript.CreateObject("WScript.Shell")
'
' Run through the environment variables
'
strVariables = ""
For Each objEnvVar In objWshShell.Environment("Process")
strVariables = strVariables & objEnvVar & vbCrLf
Next
WScript.Echo strVariables

Figure 3 shows the dialog box that appears. (The environment variables in your version of Windows Home Server may be different.)

Figure 3. A complete inventory of a system’s environment variables.

If you want to use the value of a particular environment variable, use the following syntax:

WshShell.Environment("Process")("strName")

WshShellThe WshShell object
strNameThe name of the environment variable

Listing 4 shows a revised version of the script from Listing 1 to create a shortcut. In this version, the Environment collection is used to return the value of the %UserProfile% variable, which constructs the path to the current user’s Desktop folder, as well as the %SystemRoot% variable, which constructs the paths for Notepad and the hosts file.

Listing 4. A Script That Creates a Shortcut File Using an Environment Variable
Option Explicit
Dim objWshShell, strUserProfile, strSystemRoot, objShortcut
Set objWshShell = WScript.CreateObject("WScript.Shell")
strUserProfile = objWshShell.Environment("Process")("UserProfile")
Set objShortcut = objWshShell.CreateShortcut(strUserProfile & _
"\Desktop\Edit Hosts File.lnk")
strSystemRoot = objWshShell.Environment("Process")("SystemRoot")
With objShortcut
.TargetPath = strSystemRoot & "\Notepad.exe"
.Arguments = strSystemRoot & "\System32\Drivers\etc\hosts"
.WorkingDirectory = "C:\"
.Description = "Opens the hosts file in Notepad"
.Hotkey = "Ctrl+Alt+7"
.IconLocation = strSystemRoot & "\System32\Shell32.dll,18"
.WindowStyle = 3
.Save
End With
 
Others
 
- Scripting Windows Home Server : Programming the WshShell Object (part 2) - Running Applications, Working with Shortcuts
- Scripting Windows Home Server : Programming the WshShell Object (part 1) - Displaying Information to the User
- Developer Tooling for Sharepoint 2013 : Developing SharePoint Applications Using SharePoint Designer
- Developer Tooling for Sharepoint 2013 : Site Settings
- Developer Tooling for Sharepoint 2013 : SharePoint Development Across Developer Segments, Web-Based Development in SharePoint
- Windows Small Business Server 2011 : Configuring the Windows Update Client Using Group Policy
- Windows Small Business Server 2011 : Configuring WSUS Using the Windows SBS Console
- Windows Server 2008 : Using the Integrated Scripting Environment - Executing Commands in the ISE, Creating and Saving a Script in the ISE
- Windows Server 2008 : Using the Integrated Scripting Environment - Launching the ISE, Exploring the ISE
- Windows 7 : Scanning Documents - Scanning with Windows Fax and Scan, Using scanned documents
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us