Use PowerShell and Scheduled Tasks to Automate Troubleshooting

ScriptingGuy1

Summary: Microsoft Scripting Guy Ed Wilson shows how to use Windows PowerShell and scheduled tasks to automate troubleshooting your computer.

Hey, Scripting Guy! Question

  Hey, Scripting Guy! We have a website that is currently hosted on a rather unreliable server. I am tired of constantly being harassed by management that the server is inaccessible. Unfortunately, we do not have the budget to move the site to different hardware. I would like to use the troubleshooting cmdlets to ensure the website is accessible. In addition, I would like to automate this so that it runs every hour as a scheduled task.

— BR

 

Hey, Scripting Guy! Answer Hello BR,

Microsoft Scripting Guy Ed Wilson here. One of the things I love to do is to use a Windows PowerShell command with the Task Scheduler in Windows 7. To me it is the perfect blending of flexibility, power, and convenience. I have written quite a bit about using Windows PowerShell with the Task Scheduler, and a quick review of some of those articles might be worthwhile. In addition, you may want to review the articles from earlier in the week about working with the troubleshooting packs.

This is the fourth in a series of articles detailing working with the troubleshooting pack cmdlets. Monday’s article was Use PowerShell to Troubleshoot Your Windows 7 Computer. Tuesday’s article was Use PowerShell to Automate Troubleshooting of Windows. Yesterday’s article was about using the troubleshooting packs to diagnose remote problems. Please refer to these three articles for background information that is assumed in today’s article.

The first thing to do when thinking about scheduling a Windows PowerShell command is to ensure the command works properly on its own. There are several steps that I need to accomplish to use the Task Scheduler to test my access to a website using the troubleshooting pack. The steps are listed here:

1. Import the TroubleshootingPack module. This was discussed in Monday’s Hey, Scripting Guy! Blog post.

2. Use the Get-TroubleshootingPack cmdlet to retrieve the Networking Troubleshooting pack. This was discussed in Monday’s Hey, Scripting Guy! Blog post.

3. Use the Invoke-TroubleshootingPack cmdlet to walk through the Networking Troubleshooting pack to ensure it works properly. This was discussed in Tuesday’s Hey, Scripting Guy! Blog post.

4. Use the AnswerFile parameter with the Get-TroubleShootingPack cmdlet to create an answer file to permit unattended running of the Networking Troubleshooting pack. This was discussed in Tuesday’s Hey, Scripting Guy! Blog post.

5. Use the Get-TroubleShootingPack cmdlet to retrieve the Networking Troubleshooting pack and pipe it to the Invoke-TroubleShootingPack cmdlet while using the AnswerFile parameter to point to the answer file that was created in step 4. This was discussed in Wednesday’s Hey, Scripting Guy! Blog post.

6. After the command runs properly with the answer file, create a folder to store the results of running the command. This will be discussed in today’s Hey, Scripting Guy! Blog post.

7. Run the command from step 5, specify the Result parameter, and point it to the folder created in step 6. This is discussed in today’s Hey, Scripting Guy! Blog post.

8. When everything is running properly, add the Unattended switched parameter to the command created in step 7. This is discussed in today’s Hey, Scripting Guy! Blog post.

9. Create a scheduled task to run the command created in Step 8. This is discussed in today’s Hey, Scripting Guy! Blog post.

Okay, BR, I am going to start at step 6 in the above list. I want to create a directory to hold my result reports. To do this, I use the alias md and provide a path. The alias md is an alias for the mkdir function that uses the New-Item cmdlet to create a new directory. The use of this command is illustrated here:

PS C:\> md c:\diagoutput

Directory: C:\

Mode LastWriteTime Length Name

—- ————- —— —-

d—- 2/4/2011 7:49 PM diagoutput

PS C:\>

Now I am ready for step 7. I need to retrieve the networking troubleshooting pack by using the Get-TroubleShootingPack cmdlet and pipe the DiagPack object to the Invoke-TroubleShootingPack cmdlet. I will specify the path to the AnswerFile xml file that I created in step 4. In addition, I specify the diagoutput folder to the Result parameter. One thing that is a bit strange about the Result parameter is that it must point to a folder—it does not accept a file name for the results (the reason for this is because more than one file is generated for the results). The command I use appears here:

Get-TroubleshootingPack C:\Windows\diagnostics\system\Networking |

Invoke-TroubleshootingPack -AnswerFile C:\fso\netpack.xml -Result c:\DiagOutPut

After the above command runs, I check my DiagOutPut folder to see if anything appears. As shown in the following image, several files are output as a result of the command.

Image of files in DiagOutPut folder

The .cab, .xml, and .xsl files are overwritten each time the troubleshooting pack is invoked. The .etl file names are unique and therefore the files will accumulate in the folder until manually deleted. I like to use the Get-WinEvent cmdlet to peruse the .etl log files. It is possible to open them using the Event Viewer utility (by using the Open Saved Log action), but the Get-WinEvent cmdlet is a faster option. I like to look at the IDs and the messages.

The use of the Get-WinEvent cmdlet is discussed in two Hey Scripting Guy! articles. In the first I talk about using the Get-Winevent cmdlet to filter event logs, and in the second article I talk about using the Get-WinEvent cmdlet to examine saved event log files.

To do this, I use the following command:

Get-WinEvent -Path C:\DiagOutPut\0DB36E58-62C2-45C6-B743-793CC8BA7CAA.Diagnose.1.etl -Oldest | Format-Table id, message -AutoSize -Wrap

The above command produces nice output, as shown in the following image.

Image of output of command

Now that I know that the command works and produces output, I add the Unattended switch to my command. The command is shown here:

Get-TroubleshootingPack C:\Windows\diagnostics\system\Networking |

Invoke-TroubleshootingPack -AnswerFile C:\fso\netpack.xml -Result c:\DiagOutPut –Unattended

As seen in the following image, when the above command runs, no output appears in the Windows PowerShell console. This is the kind of behavior that is desired for a scheduled command.

Image of no output in console when command runs

It is now time to convert our Windows PowerShell commands into a form that I can use with the Task Scheduler.

For a good overview of using the Task Scheduler to run Windows PowerShell commands, see Use Scheduled Tasks to Run PowerShell Commands on Windows.

When I am preparing my command, I will need to first import the TroubleShootingPack module. I can do that and separate the remaining command with a semicolon. Keep in mind when creating the scheduled task that the path to the results and the path to the unattended XML file need to be accessible to the Task Scheduler. Here is the command I used. (This is a very, very long command. It is a single-line logical command that is broken into multiple lines for display on the blog. Refer to the previously mentioned Scripting Guy article about using scheduled tasks to run Windows PowerShell commands for the details of the wizard.)

PowerShell.exe -command &{Import-Module tr* ; Get-TroubleshootingPack C:\Windows\diagnostics\system\Networking | Invoke-TroubleshootingPack -AnswerFile C:\fso\netpack.xml -Result c:\DiagOutPut -Unattended }

I open the Task Scheduler, create a basic task, and walk through the wizard. After I have created the basic task, I edit the trigger for the task, select the “Repeat task every:” box, and choose 1 hour from the drop-down menu. This is shown in the following image.

Image of editing task trigger

Of course, the hard part about creating scheduled tasks is waiting for them to run. This one works great, as evidenced by the history tab in the following image.

Image showing task ran successfully

The last thing I do is check my output folder to ensure that the scheduled task is writing the outputs to the folder. As shown in the following image, this is working as well.

Image showing outputs being written to output folder

 

BR that is all there is to using scheduled tasks to run a Windows PowerShell troubleshooting pack. This also concludes Troubleshooting Week. Join me tomorrow when I will reveal the 2011 Scripting Games Study Guide. This is an awesome resource, and should be of immediate value even if you are not planning on participating in the Games (but I could not imagine anyone not wanting to compete in the 2011 Scripting Games—they will be awesome!)

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com or post them on the Official Scripting Guys Forum.. See you tomorrow. Until then, peace.

 

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon