Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (01/23/09)
How Can I Return File Names of Files Created in Less Than an Hour from the Current Time?
Hey, Scripting Guy! I am attempting to write a script that looks at the creation dates of all files in a certain directory. If the files were created less than an hour from the current time, the script will return those file names. The script will be a recurring script running every hour. I read the script article concerning the datediff function; however, I am not sure how to compare the objFile.CreationDate (string) and now(). Can you please help?
I used to hate working with dates in VBScript. Here is a collection of scripts that work with files and folders from the Script Center Script Repository. These two articles talk about using the datediff function: the first one is a Sesame Script article from the Library and the second is a Scripting Guy blog article. Working with dates is easier when you use Windows PowerShell and I have a series of Hey Scripting Guy blogs that talk abouit ths.
How Do I Determine the Speed of My Connection?
Hey, Scripting Guy! I´m trying to get the actual connection speed of my Network-Connection, and I’m going mad. It seems that WMI has such a parameter, but the value is always empty. I can see Connection Speed in the Status of the Network-Card, but how can I access that information with a script?
According to the WMI SDK, the speed property has not been implemented on any version of Microsoft Windows before Windows Vista and therefore always returns null. On Windows Vista, it will return the actual connection speed. That is why you are having a problem with your script. Here is a script that will work for you on Windows XP.
strComputer = "." wmiNS = "\root\WMI" wmiQuery = "Select * from MSNdis_LinkSpeed" Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS) Set colItems = objWMIService.ExecQuery(wmiQuery) For Each objItem in colItems wscript.echo "Active: " & objItem.Active wscript.echo "InstanceName: " & objItem.InstanceName wscript.echo "NdisLinkSpeed: " & objItem.NdisLinkSpeed wscript.echo " " next
How Do I Hide the Internet Explorer Address Bar?
Hey, Scripting Guy! We are trying to hide the Internet Explorer address bar, but after searching around and wasting nearly an hour, we gave up. Can you help us, old master of scripting minutiae?
I am not sure I like being called an old master of scripting minutiae, but I will take it as a compliment, which is I am sure the sprit in which it was offered. There is actually a “Hey, Scripting Guy!” article that was written several years ago that talks about this very thing. Here is a script I wrote that does a similar thing:
Set objExplorer = CreateObject("InternetExplorer.Application") objExplorer.Navigate "http://www.microsoft.com/technet/scriptcenter/default.mspx" objExplorer.AddressBar = 0 objExplorer.Visible = 1
How Do I Work with Group Policy Via Scripting?
Hey, Scripting Guy! I have a good knowledge of Group Policy but I am unsure how to work with it through script. Could you please send me some resources related to scripting and Group Policy.
I am not entirely certain from your e-mail message exactly what you are trying to do with scripting and Group Policy. If you would like to actually create Group Policy objects via scripting, you are pretty much out of luck unless you purchase a third-party product. We do have an API that is available through the Group Policy Management Console, which will allow you to manage Group Policy objects via scripting. You could also be talking about using WMI filters with Group Policy. So I am going to send you some links related to both topics. This article talks about using WMI filters in Group Policy. This article talks about scripting WMI-related tasks and contains a number of good ideas. Here is a link to the Group Policy scripting hub, which has links to articles and the Script Repository.
How Can I Collect Information About Local Disk Drives and Output It to Office Excel?
Hey, Scripting Guy! I’m trying to compile a VBScript that will go out to a predefined server list, do a WMI call for local disk drives, and then output that to an Office Excel cell. My problem is that I am overwriting my disk data in the Office Excel cell while I’m in my for…next loop. The data I want to collect is the name, description, size, and free space of every local fixed disk in that server and output that to a single Office Excel cell. How can I append all the disk data into that single cell within Office Excel?
All you need to do is to append to your variable inside the for…each…next loop. This is seen :
a = Array("a","b","c") For each i In a b = b + i Next WScript.echo b
How Can I Monitor a Text File for Specific Words and Have an E-Mail Message Sent as a Result?
Hey, Scripting Guy! I am new to scripting. I need a script that will monitor a text log file for new entries (or lines), that contain the word “Global” or “Remote”. That line needs to be put in an e-mail message and sent to a distribution list. Also, each day a new text log file is created by the application. So, the script needs to account for that. Example :
Wed Nov 12 17:54:44 EST 2008, Global, userid, Unknown, User Logged In Wed Oct 29 11:48:06 EST 2008, HWDevice, userid, 11111111, Device HKxxxxxx successfully processed Action: Remote Application: Remote Application Name=MyRemote
I am not entirely certain I understand exactly what you are asking. When you mention monitoring, that makes me think of looking at something on a rather continuous basis. This kind of scripting is rather difficult and is actually not best done for long periods of time using a script. We can monitor a file for changes using the type of technique that is described in this “Hey, Scripting Guy!” article.
When the file is changed, we could open the file and look for the word “Global” or “Remote”. To do this, you need to use regular expressions, which is another rather advanced kind of scripting. In fact, regular expressions become their own language and are areas in which people specialize. This “Hey, Scripting Guy!” article talks about searching a text file for a particular word.
If this is something that must run continuously, what you really need to do is write a service to do this. If you insist on doing this via script, the second best thing to do would be to create a permanent event consumer via WMI. This can be done by scripting, but is very difficult to do as well. This article talks about the process involved in performing this.
Because you are new to scripting, let me point you to some resources that may be of interest to you. This is the VBScript Getting Started Guide. It has a number of good links to help you get started. You may also wish to consider learning Windows PowerShell. In fact, Windows PowerShell is the future of Microsoft scripting. If I were just starting out, I think I would focus on learning Windows PowerShell and be ahead of the game, rather than learning VBScript, which is currently in maintenance mode at Microsoft. This means there is no new development work being done to it other than a few bug fixes from time to time. In fact, VBScript has not changed since the release of Windows XP. Here is the Windows PowerShell hub. If you decide to learn Windows PowerShell, download and install version 1.0, which is the current, released product.
How Long Can I Make an Array?
Hey, Scripting Guy! If I use the script below, I can enter all the SAMACCOUNT names in the array to filter just those user objects in Active Directory Directory Services? Is there a limitation to how long the array can be?
Set colItems = GetObject _ ("LDAP://ou=Servers, dc=fabrikam, dc=com") colItems.Filter = Array(“User”, "Computer") For Each objItem in colItems Wscript.Echo objItem.CN Next
An array is, I believe, pretty much limited by the amount of memory you have in your computer. Play with this script to demonstrate:
StartTime = Timer Dim a() For i = 0 To 1000000 ReDim Preserve a(i) a(i) = i Next WScript.Echo UBound(a) endTime = Timer WScript.Echo "It took: " & FormatNumber(endtime - startTime) & " seconds"
You will also notice this takes a significant amount of time as the size of the array approaches more than a million items. On my computer the above script took 45 seconds and used a maximum of 35 MB of RAM.
How Do I Open the Windows PowerShell Help File?
Hey, Scripting Guy! I downloaded the WindowsPowerShellV2_CTP3_SDK.chm, but when I open it on my Windows Vista computer, all I get is “Navigation to Web Page was cancelled.” Is this file corrupt or what?
You have to right-click the .chm file and unblock it in Windows Vista.
How Do I Detect or Enumerate Disabled Network Adapters?
Hey, Scripting Guy! Is there a way to detect or enumerate disabled network adapters? I’ve disabled my Local Area Connection and checked it via WMI’s Win32_NetworkAdapter. I get NetConnectionStatus of ‘0’ (Disconnected). I would like to see the same behavior the Network Connections Control Panel applet provides.
Unfortunately, WMI does not report this information. However, DevCon will probably work for you.
How Can I Tell Who Is Using a File on a Network?
Hey, Scripting Guy! How can I find out who is using a file on the network, such as an Office Excel file that a user has had open for a long time and has forgotten to close). Thank you very much in advance for your assistance in this matter.
Strange thing: I have been asked this question twice today! And I don’t have a better answer for you than I did for the other guy. I don’t think it can be done. You may want to check The Official Scripting Guys Forum! and see what they say.
Have a safe weekend, and we will see you back on Monday. Until then, peace.
Ed Wilson and Craig Liebendorfer, Scripting Guys