Condividi tramite


How To Automate Troubleshooting and Resolving Registry Key Issues

This post will show you how to use Windows Troubleshooting Pack Designer to troubleshoot registry key issues.  This can come in handy if you ever find yourself needing a fast way to repeatedly modify a registry based setting on a PC.  Instead of mucking around in regedit, all you need to do is run the troubleshooter.

These instructions will give you a troubleshooting pack that works on Windows 7 operating systems.  However, I will update this post after Windows 8 release to show you how you can get the same tools to work on Windows 8.  Once you are able to do this, there are multitudes of regkeys and values you can modify.  Just make sure you don’t go running to the dark side with this one.  Heed this warning:

The registry contains really important operation and configuration information on your PC. If you edit it incorrectly, you could end up with some serious issues. Please, before you do anything described below, back up your registry . If you want to know more about working with the registry, see KB 310516 .

I’ve chosen a relatively harmless Windows 7 registry tweak for illustration purposes only.  With a little imagination (or experience with repetitive registry modifications) you can come up with your own custom application of this tool.

Step 1 – Start TSPD: Open Windows Troubleshooting Pack Designer.  Installation steps are listed in this blog post.  If you have not applied the TSPD fix yet, be sure to do that.

Step 2 - Create a project: Select “Project > New”.  Name your project Enable Aero.  A troubleshooting pack directory named Enable Aero is then created in your $User\Documents\troubleshooting packs folder.

Step 3 – Create the Title Screen: Your Project Name field is given the default name “Enable Aero” – you can change it if you want, but for this example, leave it as is.  The project name shows up as the title in the first screen of the troubleshooter.  Enter the following under Project Description:  “Turn Aero on”.  You can leave the “Privacy URL” field blank unless you’re interested in providing users with a privacy link.

Step 4 - Plan your workflow: Troubleshooting packs work in three main functional steps, each “compartmentalized” into independent scipts that “talk” to each other via Windows Troubleshooting Platform.  This is discussed in detail in the Device error troubleshooting sample.  These steps are as follows: Detect, Resolve, and Verify.  When you created your project in Troubleshooting Pack Designer, these scripts were added for you as part of the project.

You first need to define the state which “triggers” the necessary resolution steps to take place.  You don’t want a fix being applied if something’s not broken, right?  Once you’ve understood your detection logic, define the resolution steps.  Finally. you’ll need logic to verify that resolver actually fixed the problem we were looking for. In many cases, you can simply recycle the detection script itself.  Let’s do this is pseudocode (pseudoscript?) first:

Detect: Check the REG_DWORD-type “Composition” registry value in HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM. Is the data for this value set to “0” ? In this case “1” represents “Aero on” and “0” represents “Aero off”. If the “Composition” value data is not 1 (on) then our root cause detection is true and we can step into the resolving the problem. If the value is already 1, then we don’t have the problem and root cause detection is false.

Resolve: Set “Composition” data to “1”

Verify : Rerun detection script and if the resolver succeeded, then root cause detection will return false instead of true.

Step 5 - Add a Root Cause: In Project Explorer on the left, click “Add New Root Cause”.  A “root cause” is essentially a test condition.  As mentioned above, if we detect the root cause then we will apply the fix.  Let’s make the Root Cause ID “IncorrectRegValueData”.  This ID is referenced by your scripts.  The Root Cause Name should be “Composition registry value data incorrect”.  Leave the Root Cause Description blank for now.

Step 6 – Define the Troubleshooter:   In Project Explorer, click “Define Troubleshooter”.  You should not need to change the Troubleshooter properties presented – assuming you are an admin on the machine you’re working on, you don’t need the troubleshooter to run with elevated privileges.  We will not be using interactions in this example.

Step 7 – Define the Resolver:   In Project Explorer, click “Define Resolver”.  Our Resolver Name will be “Update registry value data”.  You can leave the Resolver Description blank for now.  Also, leave the Prompt, Elevation, Interaction and Parameter options as they are.

Step 8 - Define the Verifier:   In Project Explorer, click “Define Verifier”.  Because it is possible for a verifier script to determine the root cause has been resolved and we can reuse the troubleshooting script, leave the Verifiable and Reuse Troubleshooter options as they are.

Step 9 – Edit Root Cause Scripts:   In Project Explorer, click “Edit Root Cause Scripts”.   Let’s start with the troubleshooter that contains the detection logic.  Click on “Edit Troubleshooter Script” to open it in PowerShell.  Notice that Troubleshooting Pack Designer named your script consistently with the RootCauseID you defined earlier (TS_IncorrectRegValueData.ps1).

You will see the following in your troubleshooting script:

# TroubleshooterScript - This script checks for the presence of a root cause
# Key Cmdlets:
# -- update-diagrootcause flags the status of a root cause and can be used to pass parameters
# -- get-diaginput invokes an interactions and returns the response
# -- write-diagprogress displays a progress string to the user

$RootCauseID = "IncorrectRegValueData"

# Your detection Logic Here

$RootCauseDetected = $true #Replace "$true" with the result of your detection logic

#The following line notifies Windows Troubleshooting Platform of the status of this root cause
update-diagrootcause -id $RootCauseId -detected $RootCauseDetected

In place of ”# Your detection Logic Here”, add the following PowerShell logic:

#Detection logic: Select registry path and find the data stored under the "Composition" registry value
$regPath = Get-ItemProperty HKCU:\Software\Microsoft\Windows\DWM
$regValueData = $regPath."Composition"

#Check if the registry data is not 1. If this is true, root cause has been detected

if
($regValueData -ne "1")
{
    $RootCauseDetected = $true
}

else
{
    $RootCauseDetected = $false
}  

Be sure to leave the line that notifies Windows Troubleshooting Platform of the root cause status:

#The following line notifies Windows Troubleshooting Platform of the status of this root cause
update-diagrootcause -id $RootCauseId -detected $RootCauseDetected

Next update the resolver script (RS_IncorrectRegValue.ps1).  When you first open it, it will look like this:

# Resolver Script - This script fixes the root cause. It only runs if the Troubleshooter detects the root cause.
# Key cmdlets:
# -- get-diaginput invokes an interactions and returns the response
# -- write-diagprogress displays a progress string to the user

# Your logic to fix the root cause here

Now replace “# Your logic to fix the root cause here ” with this:

$regPath = "HKCU:\Software\Microsoft\Windows\DWM"
$value = "Composition"
$data = 1

Set-ItemProperty $regPath $value $data

Be sure to save your scripts and your project.  You’re now ready to test you troubleshooter!

Step 10 – Test your troubleshooter: In Troubleshooting Pack Designer, select “Build > Run” or press F5 to build and run the troubleshooter.  You should end up with a screen that looks like the screenshot below. 

Be sure to check regedit and see that the “Composition” value data under HKEY_CURRENT_USER\Software\Microsoft\Windows\DWM has been changed from 0 to 1 (if it was already 1, then Windows Troubleshooting Platform gives you a window that says “Troubleshooting couldn’t identify the problem”).  To test that Aero has been enabled, you will have to log off and log back on.   You now have a troubleshooting pack that checks a specific registry value and data, and changes it if it’s in a particular state.  Such is the versatility of PowerShell combined with Windows Troubleshooting Platform.