Now I m free to add Write-Debug statements to my script. The next listing shows the revised script. in VB.NET

Encode Data Matrix 2d barcode in VB.NET Now I m free to add Write-Debug statements to my script. The next listing shows the revised script.

Now I m free to add Write-Debug statements to my script. The next listing shows the revised script.
Draw DataMatrix In VB.NET
Using Barcode creation for .NET Control to generate, create Data Matrix image in .NET framework applications.
www.OnBarcode.com
Read Data Matrix In Visual Basic .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Listing 23.2 Script with trace output added
Code 128 Code Set A Encoder In VB.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Code128 image in .NET framework applications.
www.OnBarcode.com
Create Matrix In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create 2D image in .NET applications.
www.OnBarcode.com
$DebugPreference = "Continue" $name = Read-Host "Enter computer name" write-debug "`$name contains $name" if (test-connection $name) { write-debug "Test-connection was True" get-wmiobject win32_bios -computername $nmae } else { write-debug "Test-connection was False" }
EAN-13 Encoder In Visual Basic .NET
Using Barcode creator for .NET Control to generate, create EAN-13 Supplement 5 image in .NET applications.
www.OnBarcode.com
DataMatrix Generation In VB.NET
Using Barcode encoder for .NET framework Control to generate, create Data Matrix image in Visual Studio .NET applications.
www.OnBarcode.com
The first addition was a call to Write-Debug, asking it to display the contents of the variable $name. What I ve done here is a really cool trick:
Encode Linear Barcode In VB.NET
Using Barcode printer for Visual Studio .NET Control to generate, create 1D Barcode image in .NET applications.
www.OnBarcode.com
Painting International Standard Serial Number In Visual Basic .NET
Using Barcode generator for VS .NET Control to generate, create ISSN - 13 image in .NET framework applications.
www.OnBarcode.com
Because I used double quotation marks, PowerShell will look for the $ character. Whenever it sees it, the shell will assume that all following characters, to the next white space, form a variable name. The shell will then replace the variable name, and the $ character, with the contents of that variable. The first time I refer to the variable, I precede the $ character with the backtick (`) character, which is PowerShell s escape character. That character can be really tough to distinguish from a single quote in certain fonts, but trust me, it s different. It s on the upper left of a U.S. keyboard, on the same key as the tilde (~) character. The backtick takes away the special meaning of the $ character, so that PowerShell doesn t see the first $name as a variable.
Generating Data Matrix ECC200 In Java
Using Barcode creator for Android Control to generate, create Data Matrix 2d barcode image in Android applications.
www.OnBarcode.com
Draw Data Matrix In Java
Using Barcode printer for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
TRY IT NOW Try running this script in the shell, and see what happens.
Recognize Barcode In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
ECC200 Maker In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create Data Matrix ECC200 image in ASP.NET applications.
www.OnBarcode.com
Adding trace code
Generating EAN-13 Supplement 5 In Java
Using Barcode generation for Java Control to generate, create GTIN - 13 image in Java applications.
www.OnBarcode.com
Generate UPC Symbol In Java
Using Barcode generation for Java Control to generate, create GTIN - 12 image in Java applications.
www.OnBarcode.com
You ll also notice that I added a Write-Debug to the inside of the If construct. I even added an Else portion to the construct, containing a third Write-Debug message. That way, no matter which way the If construct s logic goes, I ll see some output and know what s happening inside the script. If you re following along, you should have seen the following output (assuming you entered SERVER-R2 for the computer name):
ANSI/AIM Code 39 Drawer In None
Using Barcode creator for Font Control to generate, create Code 3/9 image in Font applications.
www.OnBarcode.com
PDF417 Maker In Visual Studio .NET
Using Barcode drawer for VS .NET Control to generate, create PDF 417 image in .NET framework applications.
www.OnBarcode.com
Enter computer name: SERVER-R2 DEBUG: $name contains SERVER-R2 DEBUG: Test-connection was True Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Supply an argument that is not null o r empty and then try the command again. At C:\demo.ps1:8 char:43
Barcode Drawer In Java
Using Barcode drawer for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Paint UPC-A Supplement 5 In None
Using Barcode maker for Font Control to generate, create UCC - 12 image in Font applications.
www.OnBarcode.com
That s where you ll realize that there s a typo in that $name variable. PowerShell is clearly telling us that there s a problem with the -computerName parameter; if we look carefully at just that portion of the script, the $nmae typo is more obvious. I ll fix that now and run the script again. Here s a portion of the output:
Creating Barcode In C#
Using Barcode creation for .NET framework Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Code 3 Of 9 Generation In None
Using Barcode maker for Microsoft Excel Control to generate, create Code 39 Full ASCII image in Excel applications.
www.OnBarcode.com
Enter computer name: SERVER-R2 DEBUG: $name contains SERVER-R2 DEBUG: Test-connection was True
SMBIOSBIOSVersion : 6.00 Manufacturer : Phoenix Technologies LTD Name : PhoenixBIOS 4.0 Release 6.0
That looks like what I want. Now I need to test the opposite situation: what happens when I provide a computer name that isn t valid
Enter computer name: nothing DEBUG: $name contains nothing Test-Connection : Testing connection to computer 'nothing' failed: Th e requested name is valid, but no data of the requested type was foun d At C:\demo.ps1:6 char:20
Oops. Not what I was hoping for. That s a logic error: the Test-Connection cmdlet clearly isn t doing what I expected, which was to return a simple True or False value. Let s step out of the script, and just work with the Test-Connection cmdlet from the command line:
PS C:\> test-connection server-r2 Source -----SERVER-R2 SERVER-R2 SERVER-R2 SERVER-R2 Destination ----------server-r2 server-r2 server-r2 server-r2 IPV4Address ----------192.168.10.10 192.168.10.10 192.168.10.10 192.168.10.10 IPV6Address ----------fe80::ec31:bd61:d42... fe80::ec31:bd61:d42... fe80::ec31:bd61:d42... fe80::ec31:bd61:d42...
Debugging techniques
PS C:\> test-connection nothing Test-Connection : Testing connection to computer 'nothing' failed: Th e requested name is valid, but no data of the requested type was foun d At line:1 char:16
Okay, that s definitely not what I expected. When I use the command with a valid computer name, I get back a table of results, not a True or False value. When I use it with an invalid computer name, I still get an error. Time to read the help, by running Help Test-Connection -full. The -full part is very important, because I want very detailed information on the cmdlet and its behavior. Reading through the help, I see that the command returns the echo response replies. The help also says that, unlike the traditional ping command, Test-Connection returns a Win32_PingStatus object ... but you can use the -quiet parameter to force it to return only a Boolean value. Yes, please! Looking at the -quiet parameter, I see it suppresses all errors and returns $True if any pings succeed and $False if all failed. That s what I want, so I ll modify the script accordingly:
$DebugPreference = "Continue" $name = Read-Host "Enter computer name" write-debug "`$name contains $name" if (test-connection $name -quiet) { write-debug "Test-connection was True" get-wmiobject win32_bios -computername $name } else { write-debug "Test-connection was False" }
And I ll run it again, testing it with both a valid and an invalid computer name. Here s the output:
PS C:\> ./demo Enter computer name: server-r2 DEBUG: $name contains server-r2 DEBUG: Test-connection was True SMBIOSBIOSVersion Manufacturer Name SerialNumber Version : : : : 6.00 Phoenix Technologies LTD PhoenixBIOS 4.0 Release 6.0 VMware-56 4d 45 fc 13 92 de 5b 86 : INTEL - 6040000
PS C:\> ./demo Enter computer name: nothing DEBUG: $name contains nothing DEBUG: Test-connection was False
Copyright © OnBarcode.com . All rights reserved.