Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (7/24/09)

Hey, Scripting Guy! Quick-Hits Friday: The Scripting Guys Respond to a Bunch of Questions (7/24/09)

  • Comments 1
  • Likes

 

  

Troubleshooting a VBScript Script That Reports All Software Installed on a Computer

 

 

Hey, Scripting Guy! QuestionHey Scripting Guy! I am pretty new to use VBScript and seem to have hit a wall. I put together the script below to output a file containing all the software installed on a machine. Every time I run the script, I get an 80041001 error, which points to the line with the “Next” statement. I have been scouring the Web for info on this particular issue, but cannot seem to find anything that might be related. I am on a Windows XP machine with SP3.

This is part of a bigger project that I am working on that will query network machines for various properties such as CPU, OS, RAM, RAID, NIC, etc., and then self-register with an http post to a Web server for inventory reporting. I have managed to get all the other parts working except for this one. Any assistance would be greatly appreciated.

QuerySoftwareWriteToTextFile.vbs

Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objTextFile = objFSO.CreateTextFile("C:\software.txt", True)
 
StrComputer = “.”
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colSoftware = objWMIService.ExecQuery ("Select * from Win32_Product")
               
For Each objSoftware in colSoftware
                                strDesc = objSoftware.Description
                                strIdent = objSoftware.IdentifyingNumber
                                strInstDate = objSoftware.InstallDate2
                                strName = objSoftware.Name
                                strVend = objSoftware.Vendor
                                strVer = objSoftware.Version
                                objTextFile.WriteLine strDesc & vbtab & strIdent & vbtab & strInstDate & vbtab & strName & vbtab & strVend & vbtab & strVer
Next  
 
objTextFile.Close
 
WScript.Quit()


-- DM


Hey, Scripting Guy! Answer

Hello DM, 80041001 is WbemFailed. This means that the error is coming from WMI.

I ran the QuerySoftwareWriteToTextFile.vbs script on my Windows 7 computer, and it runs just fine. I believe you probably need to rebuild the WMI repository on your computer because it has become corrupted. Here is an interesting blog posting from the Performance team at Microsoft.

 

Troubleshooting an Active Directory Maintenance Script

 

Hey, Scripting Guy! QuestionHey Scripting Guy! I am very new to scripting and through some trial and error I have managed to write a script that basically does what I need to achieve. However I am still having some errors. I hope to achieve the following with the script:

·         Sort through the personal directory (all users’ documents) of a mapped drive and compare the folder name to those in Active Directory (LDAP).

·         If the user folder is not an active user (per LDAP), move the folder to another location on a different drive.

·         Check the Profiles directory and delete it.

·         Log the results to a text file.

I seem to be getting a lot of intermittent errors at the following location in my script:

 Set filetxt = objFSO.OpenTextFile("G:old\LogFile.txt", 8, True)

Currently I am copying the folder and renaming it because I seemed to be having permission errors when trying to do a direct move (although I can manually move without any problems). Here’s the script.

On Error Resume Next
Err.Clear
'Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFldr = objFSO.GetFolder("Z:\Personal")
Set Fldrs = objFldr.SubFolders

For Each Fldr In Fldrs 
            strBase   =  "<LDAP://staff>;"
            strFilter = "(&(objectcategory=person) (sAMAccountName=" & Fldr.Name & "));"
            strAttrs  = "sAMAccountName;"
            strScope  = "subtree"
           
            set objConn = CreateObject("ADODB.Connection")
            objConn.Provider = "ADsDSOObject"
            objConn.Open "Active Directory Provider"
           
            set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
            dim found
            found = False

            while Not objRS.EOF
            if objRS.Fields(0).Value = Fldr.Name then
                                    found = True
            objRS.MoveNext
                        end if
                        wend

Set filetxt = objFSO.OpenTextFile("G:old\LogFile.txt", 8, True)
if err <> 0 then
msgbox err.number & "    " & objFSO.path
end if
dim filetxt

            if ((Fldr.Size)/1024/1024) < "1" then
                                    objFSO.DeleteFolder "Z:\Personal\" & Fldr.Name, True
                                    filetxt.WriteLine(Date & "   " & Time & "       " & Fldr.Name & "            Delete                " & "Size (MB):    " & Fldr.Size)
                                    if  Err.Number <> 0  then
                                    msgbox err.number & "    " & Fldr.Name
                                    end if
                                    filetxt.Close
                        else      
                                    filetxt.WriteLine(Date & "   " & Time & "       " & Fldr.Name & "            Move                 " & "Size (MB):    " & Fldr.Size)
                                    objFSO.CopyFolder "Z:\Personal\" & Fldr.Name, "G:\old\"
                                    objFSO.MoveFolder "Z:\Personal\" & Fldr.Name, "Z:\Personal\" & "Delete-" & Fldr.Name
                                    filetxt.Close       
            End if

Next
msgbox "Process Complete"

 

-- CE

Hey, Scripting Guy! Answer

Hello CE,

I believe you need to close your file after you write to it, before you attempt to move it. This is what is giving the access denied:

filetxt.Close

 

Troubleshooting a Script for Changing Microsoft Office User Information

Hey, Scripting Guy! QuestionHey Scripting Guy! Thank you for taking the time to write How Can I Change the User Information in Microsoft Office. I found it very useful. This script does exactly what I was looking for. However, I ran into a problem when I added this script to the logon process. The users are challenged with the message box seen here:

Image of dialog box displayed


Why is this happening?

-- DR

 

Hey, Scripting Guy! AnswerHello DR,

The prompt you are seeing is warning about normal.dot, which is the startup Word document template. Of course, normal.dot already exists. Word uses it every time it starts up. The error message you are seeing is misleading.

Interestingly enough, when I run the script on my Windows 7 laptop with Office 2007 installed, I do not get the prompt. This tells me the behavior change you are seeing is not due to a change in Office, but perhaps the way the script is being called from the logon script. One of the many security changes that have been made to Microsoft Office in all recent versions has been to limit the ability of scripts to make configuration changes. The problem may be due to security zones or some other thing.

I would put it into a couple of virtual machines (VMs); one domain controller and one desktop machine. I play around with things to see what happens before I deploy it to my network and harass my users with cryptic message boxes.

You should always test your scripts in a nice VM environment anyway. Most of the time I write my scripts in a VM environment, regardless of what the scripts are intended to do (I learned this lesson after I accidently deleted my RAS certificate from my laptop). If you do not already have a good test environment, you should download Virtual PC 2007. It is free and is excellent for creating and testing scripts without causing problems for yourself or others.

You may also wish to ask over on the Scripting Guys Forum to see if anyone there has ideas. I have not run a VBScript from a logon script in years and years, so I simply do not test that scenario.

 

How Can I Request User Name and Password to Map a Network Drive?


Hey, Scripting Guy! QuestionHey Scripting Guy! I am having a terrible time getting my script to work. How should I go about getting the script below to map the drive by asking for the user name and password? If I directly enter the user name and password, the drive maps with no problems. When I use the following script, it does not do anything but ask me for the input boxes specified. I guess I am asking for the correct code for lines 8 and 9 to ask for the password and then authenticate me to my network drive.

ScriptPromptsButDoesNotUseTheCredentialsToMapDrive.vbs

ON ERROR RESUME NEXT
Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path, objNetwork, objNetwork1
WSHNetwork.MapNetworkDrive "X:", "http://webdav/Central_Storage",True

Set objNetwork = WScript.CreateObject("WScript.Network")
strLocalDrive = "X:"
strRemoteShare = "
http://webdav/Central_Storage"
strPer = "TRUE"
strUsr = InputBox ("Please Enter Your Lab Username")
strPas = InputBox ("Please enter your Lab Domain Password")
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, strPer, strUsr, strPas
mDrive = "X:"
Set oShell = CreateObject("Shell.Application")
oShell.NameSpace(mDrive).Self.Name = "Central Storage"

-- DP

Hey, Scripting Guy! Answer

Hello DP, This script works for me:


MapRemoteDrivePromptForCredentials.vbs

'==========================================================================
'
'
' NAME: MapRemoteDrivePromptForCredentials.vbs
'
' DATE  : 6/12/2009
'
' COMMENT: Uses the WshNetwork object to map a remote drive. Uses input box
' to prompt for user name and password
'==========================================================================

'ON ERROR RESUME NEXT
Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path, objNetwork, objNetwork1
'WSHNetwork.MapNetworkDrive "T:", "
\\edwilson\c$
",True
Set objNetwork = WScript.CreateObject("WScript.Network")
strLocalDrive = "T:"
strRemoteShare = "
\\mycomputer\c$"
strPer = "TRUE"
strUsr = InputBox ("Please type your user name")
strPas = InputBox ("Please type your domain password")
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, strPer, strUsr, strPas
'mDrive = "T:"
'Set oShell = CreateObject("Shell.Application")
'oShell.NameSpace(mDrive).Self.Name = "Central Storage"

 

How Can I Capture “Installation Complete” Screens?
 

Hey, Scripting Guy! Question

Hey Scripting Guy! I am not sure if this is the right place or not. I have created and will create more scripts that use WshShell.AppActivate to overlay and to automate installations. Some installations have the same window title on the completion screen as they do during installation. If I use a Do Until Loop to wait for the next “complete” screen, what title do I wait for? I have noticed that these do spawn new ProcessIDs, but they are random. What I am doing now to work around this is to use long Wscript.Sleep 60000 commands to simply wait. Is there a better way of catching the complete screen? If a sendkey command misses its key, will that throw out a False value as an AppActivate in a Do Until Loop would? Below is a sample of my script.

dim WshShell,FSO

set WshShell = CreateObject("WScript.Shell")
set WshEnv = WshShell.Environment("Process")

set FSO = CreateObject("Scripting.FileSystemObject")


WshShell.Run("c:\Utilities\vpnclient-win-msi-5[1].0.03.0560-k9.exe")
Do Until(WshShell.AppActivate ("WinZip Self-Extractor")) = True
     WScript.Sleep 300
Loop
WshShell.AppActivate "WinZip Self-Extractor"
WScript.Sleep 2000
WshShell.SendKeys "{ENTER}"
Do Until(WshShell.AppActivate ("Cisco Systems VPN Client 5.0.03.0560 Setup")) = True
     WScript.Sleep 300
Loop
WshShell.AppActivate ("Cisco Systems VPN Client 5.0.03.0560 Setup")
WScript.Sleep 5000
WshShell.SendKeys "%(n)"
WScript.Sleep 2000
WshShell.SendKeys "%(a)"
WScript.Sleep 2000
WshShell.SendKeys "%(n)"
WScript.Sleep 2000
WshShell.SendKeys "%(n)"
WScript.Sleep 2000
WshShell.SendKeys "%(n)"
WScript.Sleep 60000
WScript.Sleep 60000
WScript.Sleep 60000
WshShell.SendKeys "%(f)"
WScript.Sleep 5000
WshShell.SendKeys "%(n)"
WshShell.SendKeys VBCRLF
set WshShell=Nothing
set WshEnv=Nothing
set FSO=Nothing

wscript.quit


-- DY

Hey, Scripting Guy! Answer

Hello DY,

As you have no doubt seen, using AppActivate and sendkeys is a hit-or-miss proposition. You actually know as much about this as I do. There is no way I can tell you which window title some random program will display anymore than I could tell you what ProcessID you will need to look up.

To be honest, trying to use this methodology to install software is extremely problematic. Most sophisticated software packages will have a better way to automate the installation, such as with an .ini file, .inf file,.xml file, or a .whatever file. These are usually documented on the Web site of the vendor. Many applications actually have a way of automating their own deployment and installation agents via the logon script or via Group Policy.

For the rest, you may want to take a look at AutoIT, it is far better than using AppActivate and sendkeys.

WOOHOO! It is the weekend baby! It has been another great week at the Script Center. We hope you are going to get outside, blow off some steam, and spend some time relaxing and having fun. We certainly will. We will also be around on Facebook and Twitter, and we’ll be checking the Official Scripting Guys Forum. So if you get bored or are simply a script-a-holic like we are, stop by. If we are not busy running or swimming, we will enjoy the company. Oh yeah, we will also probably be checking e-mail at scripter@microsoft.com over the weekend. Until Monday, take care. Peace!

 

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

 

 

 

 

 

 

 

 

 

 

 

 

 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment