04.07.2013 Views

Windows PowerShell 3.0 Step by Step - Cdn.oreilly.com - O'Reilly

Windows PowerShell 3.0 Step by Step - Cdn.oreilly.com - O'Reilly

Windows PowerShell 3.0 Step by Step - Cdn.oreilly.com - O'Reilly

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

FIGURE 1-1 Errors appear when attempting to update help files that do not support updatable help.<br />

One way to update help and not to receive a screen full of error messages is to run the<br />

Update-Help cmdlet and suppress the errors all together. This technique appears here:<br />

Update-Help -Module * -Force -ea 0<br />

The problem with this approach is that you can never be certain that you have actually received<br />

updated help for everything you wanted to update. A better approach is to hide the errors during the<br />

update process, but also to display errors after the update <strong>com</strong>pletes. The advantage to this approach<br />

is the ability to display cleaner errors. The UpdateHelpTrackErrors.ps1 script illustrates this technique.<br />

The first thing the UpdateHelpTrackErrors.ps1 script does is to empty the error stack <strong>by</strong> calling the<br />

clear method. Next, it calls the Update-Help module with both the module parameter and the force<br />

switched parameter. In addition, it uses the ErrorAction parameter (ea is an alias for this parameter)<br />

with a value of 0. A 0 value means that errors will not be displayed when the <strong>com</strong>mand runs. The<br />

script concludes <strong>by</strong> using a For loop to walk through the errors and displays the error exceptions. The<br />

<strong>com</strong>plete UpdateHelpTrackErrors.ps1 script appears here.<br />

note For information about writing <strong>Windows</strong> <strong>PowerShell</strong> scripts and about using the For<br />

loop, see Chapter 5.<br />

UpdateHelpTrackErrors.ps1<br />

$error.Clear()<br />

Update-Help -Module * -Force -ea 0<br />

For ($i = 0 ; $i -le $error.Count ; $i ++)<br />

{ "`nerror $i" ; $error[$i].exception }<br />

14 <strong>Windows</strong> <strong>PowerShell</strong> 3 <strong>Step</strong> <strong>by</strong> <strong>Step</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!