Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (03/27/09)


Determining if SQL Is Installed; How to Read Paired Information into a Dictionary Object 

Hey, Scripting Guy! Question

Hey, Scripting Guy! Being an avid reader of your columns for the last several years has been a blast. I wrote you once before but with no success (in terms of a reply, which was expected, and in terms of not seeing an article related to my question, which was disappointing). I guess you are trying to catch as many people’s issues as you can with the least effort, also understandable.

I’m pretty sure neither of these questions are popular areas, so I actually don’t expect a reply, direct or otherwise, but as an eternal optimist, I wanted to try, at least it might make it something different to think about.

Question 1 deals with the various versions of MS SQL—how do you remotely determine if a system has any versions of MS SQL (MSSQL, SQLExpress, etc.) installed and then determine the names of the databases in it? This question has its root in the security implications of an unknown/unprotected database in the enterprise (more than 8000 desktops and 400 servers in my case).

Question 2 might be a bit easier, or it could be a bit harder for you, I’m not sure. I have not seen any articles related to this area (not from you or on the Web in general), so it is also possible you are trying to avoid it. The question is how do you read a formatted list of paired information (like computer names and MAC addresses) into a dictionary object and then later save all the paired information into a CSV or text file?

In any case I will keep my expectations low for an actual reply so as not to be disappointed. Keep up the good work.

– DA

Spacer Hey, Scripting Guy! Answer

Hi DA,

We are sorry you did not receive a reply previously, but it’s not our fault. Honest. There was a flood, we ran out of gas, the tux did not come back from the cleaner, an old friend came in from out of town. Actually, we only took over the job in November. One of our personal goals is to answer all the e-mail we receive. We get a lot, so it’s harder than it sounds.

Question 1: How do I determine the version of SQL server? See http://support.microsoft.com/kb/321185 and http://social.msdn.microsoft.com/Forums/en-US/sqlsmoanddmo/thread/b46bde55-09ec-449f-8711-8355db9542e8/. You should be able to use the WIN32_Product WMI class and retrieve a listing of software that includes SQL. You would then need to parse the version information with information about the version. It is not super clean, but is about the best you can do.

Question 2: You will find more information about the dictionary object here and here. Also, take a look at this HSG article. It illustrates using a dictionary to parse key/pair information.

Finally, you may want to check out the Microsoft Press book, Microsoft VBScript Step by Step. (Full disclosure: Ed wrote it.) It has some really good information about dictionary objects, text files, and the like. Write back and let me know how you are getting along.


How Can I Get a Text String onto the Clipboard Using VBScript?


Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to use a .vbs file to get a text string onto the clipboard. I’ve being using the Internet Explorer object method for ages, but now a security warning pops up so I found a .vbs example using the Office Word object instead, which overcomes the popup message. The trouble is I have a few terminal servers that do not have Office Word installed. Is there another application such as Notepad that is pretty much guaranteed to be on a clean Microsoft Windows machine that can be used instead? If the answer is “yes,” the .vbs code would be great to try.

– DB

Spacer Hey, Scripting Guy! Answer

Hi DB,

I am assuming this is the type of thing you have been doing and now you are using something like this, but you need a new technique. Here you go.

On some versions of the Microsoft Windows operating system, there is a utility called clip.exe. I know it is available on Windows Server 2008 and Windows Vista. In earlier versions of the operating system, it may have been a resource kit utility.

PS C:\> clip /?CLIPDescription:    Redirects output of command line tools to the Windows clipboard.    This text output can then be pasted into other programs.Parameter List:    /?                  Displays this help message.Examples:    DIR | CLIP          Places a copy of the current directory                        listing into the Windows clipboard.    CLIP < README.TXT   Places a copy of the text from readme.txt                        on the Windows clipboard.

If you are using Windows PowerShell, you can download the Windows PowerShell Community Extensions, which include two cmdlets that allow you to copy and paste to and from the clipboard. These are designed to work with Windows PowerShell 1.0, but I know they are working on Windows PowerShell 2.0 versions of the Windows PowerShell Community Extensions (PSCX), which I am sure will ship around the time of Windows PowerShell 2.0. You can find information about Windows PowerShell by going to the Windows PowerShell Scripting Hub.


Active Directory Scripting the Shutdown of PCs


Hey, Scripting Guy! Question

Hey, Scripting Guy! Our group has decided to implement an Active Directory script for shutdown of PCs at a time yet to be specified with wake on LAN options (if someone remotes into their PC). We want it to put up a Yes/No window to allow the user to prevent the shutdown for 10 seconds or some predefined amount of time. Can you help me with this?

– PH

Spacer Hey, Scripting Guy! Answer

Hi PH,

This is one of my favorite things to do. Here are two articles that talk about doing this: Article 1 and Article 2.


Scripts in the Internet Zone and Potentially Malicious Scripts


Hey, Scripting Guy! Question

Hey, Scripting Guy! I’m getting this popup message that scripting is usually safe and asking me if I want to run a script. What and where do I go to accept this scripting? This popup is blocking my getting to anything on my computer. Help please. Thanks.

– BD

Spacer Hey, Scripting Guy! Answer

Hi BD,

This is because you are running a script from the Internet zone. Though scripts can be beneficial, they can also be very harmful things such as the Melissa virus or the I love you virus. Microsoft Windows has security features to notify you when you are getting ready to run a script. If you think this is keeping you from getting anything done on your computer, think how much worse it would be if you got infected with a virus and you lost access to all your data or your personal data was transferred to someone who stole your identity. Yikes!

IF YOU DO NOT KNOW WHAT A SCRIPT IS GETTING READY TO DO, YOU SHOULD NEVER CLICK ON IT AND SAY OK!!!!!!!!!!!!!!!!!!!!!!!!! (Sorry for the all caps and bold and exclamation points, but that sentence deserved all of that.)

If this is happening at work, your network administrator should sign his scripts or modify the Internet zone locations to preclude you from getting these messages. If this is happening at home, I would simply avoid those Internet sites that are showing you these messages. They are poorly managed and possibly dangerous!


Troubleshooting a Script with a Constant Set to Nothing


Hey, Scripting Guy! Question

Hey, Scripting Guy! After modifying the script in your post here, I’m receiving an “Illegal Assignment” runtime error on the value MY_COMPUTER. The only difference between your code and mine is that I finish by setting MY_COMPUTER = nothing. Is this not allowed?

– SM

Spacer Hey, Scripting Guy! Answer

Hi SM,

In the script, you will notice that the MY_Computer is a constant as seen here:

Const MY_COMPUTER = &H11&

A constant cannot be changed or destroyed. It is, in a word, constant.

When you try to set a constant equal to nothing, you are in fact doing something illegal (don’t worry, the cops are not on their way). You are trying to destroy a constant, which cannot be done. Otherwise, the item would not be constant. To avoid the error, don’t try to destroy a constant.

Now let’s go to the larger issue. In VBScript when a script is finished running, it automatically releases all the variables and constants. So there is no need to have 10 or 15 lines of code that release all the variables in a script by setting them equal to nothing. This practice went out back in the days of Visual Basic 5.0! It is not required.


How Can I Associate .Tiff and .Tif Files with Mspview.exe?


Hey, Scripting Guy! Question

Hey, Scripting Guy! Do you have a VBScript to run that will associate .tiff and .tif files with the mspview.exe application?

– CP

Spacer Hey, Scripting Guy! Answer

Hi CP,

I am not sure of the path to mspview.exe as I do not have it on my computer. You can do this from the command prompt by using the ftype command.

To find your file type associations, you use the assoc command at the command prompt. Make sure you pipe it to the more filter because the list is quite long and will scroll out of your buffer. This is seen here:

Assoc | more

After you know how you want to change the file associations, use the ftype command:

ftype TIFImage.Document="C:\Program Files\MSPVIEW.exe" "%1"

To run the above command from VBScript, use the technique described in this article. Make sure you practice this on a test machine before deploying. I like to use a virtual machine with undo disks enabled for these kinds of things. The ListFileAssociations.vbs script will list file associations using the Assoc command:


Set objShell = WScript.CreateObject("WScript.Shell")objShell.Run("%comspec% /K Assoc | more"), 1, True

The ChangeFileAssociation.vbs script will change the file associations for you. The hardest thing with this script is that you need two sets of quotation marks around the program path and an extra set around %1%. The command is seen here:

ftype TIFImage.Document =""C:\Program Files\MSPVIEW.exe"" ""%1"""

The script is seen here:


Set objShell = WScript.CreateObject("WScript.Shell")objShell.Run("%comspec% /K ftype TIFImage.Document =""C:\Program Files\MSPVIEW.exe"" ""%1"""), 1, True

Well, that ends another Quick-Hits Friday. This also ends another week. Join us on Monday for another installment in the continuing saga of “Hey, Scripting Guy!” If you cannot wait until Monday to find out what we will be talking about, join us on Twitter at http://www.twitter.com/scriptingguys and be among the first in the world to find out. Until then, have a pleasant and enjoyable weekend.


Ed Wilson and Craig Liebendorfer, Scripting Guys




    Leave a comment