Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (10/30/09)
- How Do I Run Scripts?
- Which WMI Class Works with IDE Controllers?
- How Can I Randomize and Repeat Sequences?
- How Can I Run a VBScript I Find on the Script Center?
- How Can I Enable the Windows Scripting Host on My Computer?
Hey, Scripting Guy! I am trying to open the script environment because the Script Center site implies that this is best for running scripts to avoid all the windows that can be returned. I cannot seem to find a way of running this. I tried clicking Start -> Run -> “wscript.exe” and Start -> Run -> “cscript.exe,” but neither of these methods worked. Could you tell me how to launch it please?
Microsoft Scripting Guy Ed Wilson here. I am sorry you are having problems running scripts you copy from the TechNet Script Center Gallery. What you need to do is to click Start, click Run, and then type cmd. At the command prompt, type CScript and the name of the script. Suppose you have a script named hello.vbs that contains a line of code that will display the message “hello world,” such as the code that is shown here:
Wscript.echo “hello world”
If you double click the helloWorld.vbs script, a dialog box will appear that says “hello world.” This is because the default execution engine for the .vbs file extension is wscript. The dialog box that appears from double-clicking the helloWorld.vbs script is shown here:
If you open the command prompt and type the word CScript before the path to the script, the results are displayed in the Command Prompt window. This is shown here:
The decision as to whether to display the VBScript results as a series of dialog boxes (as seen in the first image above) or inside the Command Prompt window (as seen in the second image above) is part of the design process for your script. In general, if a script will display a single piece of information, I will go ahead and allow the use of the dialog boxes. But if the script will display several pieces of information, I prefer to use the Command Prompt window because I get annoyed clicking OK all the time.
Hey, Scripting Guy! I need to access the Advanced Properties of the IDE ATA/ATAPI Controllers Channel 0 to read the Current Mode of remote PCs. I need to check to see what mode it is set to. Is this possible to do via VBScript? I have searched and searched using the Scriptomatic, but I do not find anything.
The only WMI class that works with IDE controllers is the Win32_IDEController class. It gives you some really cool information about your IDE controller, but mode is not one of the pieces of information supplied.
How Can I Randomize and Repeat Sequences?
Hey, Scripting Guy! I know that Randomize sets the random seed value based on the system time. I know that I can leave the statement out to generate the same sequence over and over, but how can I set the random seed so that I get the same sequence each time (have the ability to play the same game over and over, but still have different games)?
You leave out the Randomize statement. The DisplayRandomNumbers.vbs, which I just wrote, illustrates this process and is shown here:
‘ NAME: DisplayRandomNumbers.vbs
‘ AUTHOR: ed wilson , msft
‘ DATE : 5/22/2009
‘ COMMENT: Displays random numbers between 1 and 20. Always in the same order.
upperlimit = 20
lowerlimit = 1
For i = lowerlimit To upperlimit
rn = Int((upperlimit – lowerlimit + 1) * Rnd + lowerlimit)
rndNumber = rn & “,” & rndNumber
strLen = Len(rndNumber)
WScript.Echo Left (rndNumber, strLen-1)
Hey, Scripting Guy! I have followed the instructions to the letter at least 20 times yet it still does not seem to work. I paste the code into Notepad and save it as it says with the quotation marks; then, I open up a command prompt and type CScript test.vbs and press ENTER. The error that is generated is shown here:
C:Documents and settingsDenise>cscript test.vbs
Microsft <R> Windows script host version 5.7
Copyright <C> Microsoft corporation. All rights reserved.
Input error: Cannot find script file “C:Documents and settingsDenise>test.vbs”-
Navigating all those folders can be a bear. Perhaps a bear such as this one who was at Tech∙Ed 2009 with Craig and me. I am going to assume you are on Windows XP, because of the space in Documents and Settings. One of the big improvements in Windows Vista is that we got rid of that space! It makes working from the command line much better. Here are some step-by-step instructions for copying a VBScript from the Script Center and running it:
1. Open Notepad.
2. Type: Wscript.Echo “my first script”
3. Click File and then click Save As.
4. In the left pane, click My Documents.
5. In the File name box, type test.vbs (it is not case-sensitive).
6. Click Save.
7. If a dialog box appears about overwriting an existing file, click OK.
8. Click the File menu of Notepad, click Exit, which closes Notepad.
9. Click Start, and then click Run.
10. In the Run dialog box, type cmd, and then click OK.
11. The cmd prompt appears, and the working directory is C:Documents and Settingsyourname>. For example, on my computer it is C:Documents and Settingsed>
12. Type dir and press ENTER. A directory listing appears. One of which should be My Documents.
13. Type cd “My Documents” and press ENTER. (Note: The quotation marks are required. The capital letters are not.) You will now be in the C:Documents and SettingsyournameMy Documents> folder.
14. Type CScript test.vbs. Inside the Command Prompt window, you should see my first script appear.
15. Press the up arrow one time, and you should see CScript test.vbs appear on the command line.
16. Press HOME (on my keyboard it is beside the number pad). You should see the cursor move to the beginning of the command line.
17. Press DELETE eight times until you have deleted this: CScript (and the trailing space). This leaves you with only test.vbs in the Command Prompt window.
18. Press ENTER. Now you will see a popup box that displays the message, “my first script.”
This illustrates creating a script, saving a script, and running a script in the CScript scripting host and the WScript scripting host. It also illustrates that the Wscript.Echo command has two modes of operation. Inside the CScript scripting host, it displays on the command line. When running inside the WScript scripting host, it produces dialog boxes.
I sincerely hope this gets you started in the wonderful world of scripting, and that you stick with this fascinating and amazingly useful skill. Don’t feel bad that it took you more than 20 times to get it working. When I first started programming, I spent hours screaming at the walls trying to get my first piece of code to compile. I was even typing things like computername and username into my code (it didn’t work obviously). So I can certainly understand frustrating documentation and confusing instructions. I have tried to keep those early experiences in mind when I wrote my six scripting books, and as I write the Hey, Scripting Guy! Blog posts. I do not always succeed, but I do try. Please stay with us, and let me know if I forget my early trials and tribulations! Thank you for writing, and for attempting more than 20 times. I know you will get it on the next go around.
p style=”MARGIN: 0in 0in 8pt” class=”MsoNormal”>
Hey Scripting Guy! I was going to try some scripting with WScript and I keep getting this error: “CScript Error: Windows Script Host access is disabled on this machine. Contact your administrator for details.” I am on my home computer and I am logged on to the administrator account. I have been trying to look around to determine how I may enable the scripting host, but I have never really found anything yet. Is there some way I can enable it so I can use these scripts?
This is rather an interesting question. The reason it is interesting is that by default the Windows Scripting Host is enabled. Therefore, someone has disabled it on your computer. It is possible that your ISP has done so, (and I would be really mad if the ISP made changes to my computer without asking for my permission). This is one of the great things about the Windows 7 and Windows Vista User Account Control (UAC) feature: Changes are prompted that require elevated permissions. This is really cool when you install some software package, and they do not tell you they are going to change service configuration or install “phone home” software, and a UAC dialog box is displayed. The scripting host is enabled or disabled via the registry. The registry settings are covered in the Scripting Guide. A detailed discussion of this is in the Microsoft Press book, Microsoft VBScript Step by Ste