Learn about Windows PowerShell
In this post:
Why Are Some Script Repository Scripts Not Showing All Compatible Platforms?
Hey, Scripting Guy! In reference to a script in the Script Repository, it does not show Windows Server 2008 R2 or Windows Server 2008 as being supported platforms, as you can see in the following image.
It runs on my Windows Server 2008. Did the script simply pre-date the release of the operating system? If so, I think we should be able change the page to a Yes to those platforms.
The reason scripts on the Script Repository are not designated as working with Windows Server 2008 R2 or Windows Server 2008 simply means they were written before Windows Server 2008 R2 and Windows Server 2008 were released. It does not necessarily mean they will not work, it means we have not tested them on those platforms. There are thousands of scripts on the Script Repository, and though we test the scripts we post at the time they are posted, we do not have the resources or bandwidth to go back and revisit scripts to update the boxes.
I wish we could allow others to change the boxes, but we cannot open up the permissions like that. So if you find a script that works on another platform, you can post a comment on the script via the Discussions tab. Based on feedback from you and others, we decided to make a change to the “Verified on the following platforms” section of each script’s description. Keep in mind, it does not mean that the Scripting Guys have verified the script works on the checked platforms, but that the person submitting the script thinks it will run on the indicated platforms. The change is shown in the following image.
Addressing a Windows PowerShell Syntax Question
Hey, Scripting Guy! Recently, I was working on a script using Get-Credential, and found an example on the Internet that had this strange syntax:
It has something to do with clicking Cancel in the Get-Credential window, but what does it actually mean? What would be the correct code without using the symbols?
In Windows PowerShell, $? is used to see if the last command completed successfully. If a command completes without error, $? will return True. This is shown here:
The ! character, is the not operator. Therefore, the line shown here says if the previous command did not complete successfully, exit.
What would the code be without the symbols? Well, you could not write this in exactly the same manner. You could write the command without using the ! operator as shown here:
But I am not sure why you would want to do so. Actually, I seldom use $? because it is very rudimentary, and the results can be misleading, as shown here:
Notice that 1/0 does generate an error, but because the command completed, $? reports True. A better indicator of errors is the $error object. The count property will tell you how many errors have occurred. As shown here, there are two errors—not just one. In addition, the $error object contains rich information about the error that was generated. Use the Get-Member cmdlet to examine the error object:
Why Is This Script Giving Me so Much Output?
Hey, Scripting Guy! Hope all is going well in scripting world. I'm really liking the new site so far.
I was wondering if you could help shed some light on why when I run the following script I get back way more than I asked for. I know that I can just pipe the output to a Select-Object and narrow down the results but was just hoping to get some insight on why all of those "__*" properties come through. Thanks for any help you can provide!
Those are the system properties of the WMI object. They actually offer some really cool information (at least for someone who wrote a WMI book, such as yours truly). For example, -relpath can be used by the WMI instance accelerator like this:
__RELPATH : Win32_LogicalDisk.DeviceID="C:"
Here is the cool code. Put WMI in square brackets to retrieve a specific instance of a WMI class:
The __Server property often tells you the name of the machine that is supplying the data. Here, it is not showing up. Note that when you use the –property parameter, you also get the system properties:
The pretty output is due to the format XML file that controls the output for the win32_logicaldisk WMI class. The Windows PowerShell team has done this for dozens of commonly used WMI classes. What I teach in my various Windows PowerShell books is this:
However, in Windows PowerShell 2.0 we decided to display additional properties. So now we are down to using wildcard characters as shown here:
Keep in mind, that choosing the three specific properties does not improve performance because WMI still has to retrieve the entire instance of the classes. You get back a custom object, but that is because of Windows PowerShell, not WMI. You are better off doing your display filtering, rather than the double work of selecting properties, and then reselecting them for format list.
Well, this concludes another edition of Quick-Hits Friday. Join us tomorrow for the Weekend Scripter as we delve into the mysteries of…well, we will let that remain a mystery for now.
If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys