Thursday, September 06, 2007

 I was looking for something else and came across this in the Scripting guy archives...

Set objFile = objFSO.OpenTextFile("c:\scripts\test.txt", ForReading,False,TriStateTrue)

The first two parameters probably don’t faze you much: they’re simply the full path to the file we want to open and the constant ForReading. And you’re right: this is standard operating procedure when it comes to reading text files. But what about those other two parameters, one False, the other the constant TriStateTrue?

This is where the Scripting Guys perform their magic. The optional third parameter is a Boolean parameter that, if True, creates the specified text file if the file cannot be found. Because we’re only interested in opening an existing file, we set this parameter to False (which is also the default value).

That brings us to parameter 4, our old pal TriStateTrue. If the fourth parameter passed to the OpenTextFile method is equal to -1 then the file will be opened as a Unicode file. It’s that easy. Leave the fourth parameter off and the file will be opened as ASCII; set the fourth parameter to -1 and - presto-changeo! - your file will be opened as Unicode,

Hey, Scripting Guy! How Can I Open a Text File as Unicode?

Thursday, September 06, 2007 6:15:41 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Wednesday, August 29, 2007

I was trying to write a script to read a text file and evaluate the data to do some formatting on it.  This is for a project I am working on and the person that gave me the text file wants some changes made based on the contents of the file.  I have done similar things before, but I kept having trouble with this one.

I started with trying to do a readline and then echo that back.  Every time I tried it, I got  ÿ_[  (y with the umlaut, underscore and the bracket)as the output for the first line and then nothing for everything after that.  I tried it against other text files and got the expected result.  I was investigating, trying to figure out if there was a character in the file that didn't work well, and finally decided to try and do a writeline to another text file to see if it would at least write the value out that way. 

Imagine my surprise when the results came back with a space between each letter in the source file.  Turns out that it was in UNICODE format.  I saved it as ANSI and then everything was golden.  I also noticed that the file was half the original size...  must have to do with all those extra spaces...

Wednesday, August 29, 2007 2:32:15 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Tuesday, July 10, 2007

I wanted to be able to remotely connect to a server and see if it had a profile directory for a particular user.  I wrote a VBScript that will connect to a machine through WMI, find out what the ProfilesDirectory path is and then list all the folders in that directory.  It wouldn't take much to be able to output to a text file or some other format.

The next thing I will work on is to have it pull a list of machines from somewhere, go check those machines and then when it finds a particular profile on that machine to go and remove it.   You can see what I have so far:

Const HKLM = &H80000002

Dim strComputer

strComputer = "." 'This computer is the ".".  If you want another computer, replace the .
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\"
strValueName = "ProfilesDirectory"

oReg.GetExpandedStringValue HKLM,strKeyPath,strValueName,strValue

If strValue = "%SystemDrive%\Documents and Settings" Then
    WScript.Echo "Default Path"
    strProfilePath = "C:\Documents and Settings"
Else
    WScript.Echo strValue
    strProfilePath = strValue
End If

Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery _
 ("ASSOCIATORS OF {Win32_Directory.Name='" & strProfilePath & "'} " _
 & "WHERE AssocClass = Win32_Subdirectory " _
 & "ResultRole = PartComponent")
For Each objFolder in colSubfolders
 Wscript.Echo objFolder.Name
Next

Tuesday, July 10, 2007 12:46:25 PM (Central Standard Time, UTC-06:00)  #    Comments [0]
 Wednesday, August 09, 2006

So... the other day I was working on a script.  I had a need to delete several registry Subkeys under one key.  I hadn't really figured out how I was going to go about it, but I happened to pick up a copy of TechNet Magazine on my desk and see at the bottom “Scripting Guys take on the Registry”.  It was a neat coincidence that they happened to talk about the very subject I was about to attack.

http://www.microsoft.com/technet/technetmag/issues/2006/08/ScriptingGuy/default.aspx

 

Wednesday, August 09, 2006 10:41:04 AM (Central Standard Time, UTC-06:00)  #    Comments [0]

Microsoft's Script Center has a “funzone“ that has a weekly scripting puzzle.  I haven't tried it yet, but it would seem like a good way to get some practice scripting. 

http://www.microsoft.com/technet/scriptcenter/funzone/puzzle/default.mspx

Wednesday, August 09, 2006 10:29:03 AM (Central Standard Time, UTC-06:00)  #    Comments [0]