How Can I Pipe Over a Variable for Use in a File Name?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! Sorry to bother you but I have been looking for an answer to this for a few hours now. I’ve also been experimenting but have had no luck figuring it out yet. How do you get a variable to pipe over to use in a file name? For example, I am trying to get the hostname to end up in the file name. See below; it will make more sense.

$strComp  = “Computer1”  ; get-eventlog application -newest 200 
| where {$_.message -match "A non-delivery report with a status code of 4.4.6 was generated for recipient rfc822;"} 
| out-file -filepath "c:\scripts\NDR_$strComp_Results.txt"

The file outputs my information fine, but the name comes out as NDR_.txt instead of NDR_Computer1_Results.txt. Is there is a way to make this work or should I just “hard code” the computername in the out-file–filepath property.

- SF

SpacerHey, Scripting Guy! Answer

Hi SF,

I spend all day every day looking for answers, so you are not bothering me at all. Here is the answer to your question:

$strComp  = "MrEd"
get-eventlog application -newest 1 | out-file -filepath "c:\fso\NDR`_$strComp`_Results.txt" 
invoke-Item c:\fso

You were getting hung up with the underscore character (_) in front of your variable. You need to escape it. In Windows PowerShell, we use the backtick character (`) as an escape character. Hope this helps.

 

How Can I Make a Script Account for Daylight Saving Time?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I wrote a script that creates a scheduled task. It was working fine until Daylight Saving Time ended. Since that day, my script has always been off by one hour.

I think the problem is the date-time expressed in UTC format (yyyymmddHHMMSS.xxxxxx±UUU). I changed the UUU clause, subtracting 60 minutes from the previous value. Now it works, but when Daylight Saving Time starts again, I will have to modify the UUU clause again. I would like to know if there is a way to take Daylight Saving Time into consideration, in order not to have to change the script every time Daylight Saving Time starts or ends.

- GP

SpacerHey, Scripting Guy! Answer

Hi GP,

UTC time (Universal Time Coordinate) has the concept of daylight saving time built into the time, and there is no way around it. Luckily, because you have a script, all you need to do is create an Office Outlook reminder to tell you to rerun the script to create a new job before time switches over. You can use the sWbemDateTime object to make it easier to create these types of times in your scripts. This approach is documented here. You may also want to take a look at this article, which talks about using the new task scheduler API in Windows Vista. I believe these jobs have the concept of Daylight Saving Time built into them, and they could solve your problem.

 

Troubleshooting a Script

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have two scripts that I have been working on and I am stuck. The first script reads the BIOS and grabs the asset tag information and then sets that as the name for the PC. It works well. I would like that script to have a built-in restart feature as well because renaming a PC requires a restart. The restart part I haven't had any luck with.

I am unable to get the second script to run. It seems to be right except it errors out with "object required." I am attaching that script here.

DeleteOlderFiles—HasAnERROR.vbs

dim fso

dim directory

dim modified

dim files

set fso=CreateObject("Scripting.FileSystemObject")

set fldr=fso.getfolder("c:\test-del")

setfiles=directory.files

for each modified in files

if datediff("d", modified.datelastmodified, now) > 3 then modified.delete

next

- JFSpacerHey, Scripting Guy! Answer

Hi JF,

For your first problem, I think I would stay with WMI and use the win32_operatingsystem WMI class, which has both a reboot and a shutdown method.

For your second problem, the Files property returns an object, which is a collection of files. Therefore, you need to use Set. In this line of code, you have not used the Set keyword; you have in fact, I imagine, left the space out between Set and Files. This is seen here:

setfiles=directory.files

Your variable name at this point is setfiles. If you change the line, the script will work. The revised line of code is seen here:

Set Files=directory.files

By the way, this is why I recommend Option Explicit for all VBScripts. It is a cheap spell checker, and it would have caught this particular problem. This is covered in the Microsoft Press book, Microsoft VBScript Step by Step.

Here is your revised script:

DeleteOlderFiles.vbs

dim fso

dim directory

dim modified

dim files

set fso=CreateObject("Scripting.FileSystemObject")

set fldr=fso.getfolder("c:\test-del")

set files=directory.files

for each modified in files

if datediff("d", modified.datelastmodified, now) > 3 then modified.delete

next
 

How Can I Find Certain System Information for Any Computer?

 

Hey, Scripting Guy! Question

Hey, Scripting Guy! I find your program the Scriptomatic very interesting and want to use it. I have been going through it and even read the humorous readme file. Basically, I want to find this information from any given machine: Processor speed, HDD capacity and amount of installed memory (RAM). My question is this: Under which WMI classes can I find this information? There are a lot of WMI classes and I’m not exactly a Scriptomatic expert. I would really appreciate your help with this.

- TC

SpacerHey, Scripting Guy! Answer

Hi TC,

You can use these classes to obtain the information you seek:

WIN32_processor

WIN32_logicaldisk

WIN32_computersystem

That completes this week’s Quick-Hits Friday revue. Stay tuned for another fabulous “Hey, Scripting Guy!” article on Monday.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys