Archive for the 'Scripting' Category

List All Folders in the User Profile Directory

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

Deleting things from the Registry

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


 

Do you want to be a Scritping Guy?

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