Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help getting script to save a file and then open saved file 1

Status
Not open for further replies.

Dontbyteme

IS-IT--Management
May 14, 2014
8
US
The VBScript I created is supposed to ask what PC a person wants info on then go get that info, put it in a txt file that it has saved to the users c:\users\%username%\documents folder and then ask the person if they want to view that txt file. After the person clicks on Yes, it's supposed to open that txt file. But the script does not expand environment variables like %username%. However the scripting host Shell interface does but when I try to use it, I get an error message when I click on 'Yes' (to open the saved file) on Line 69 that says "Object doesn't support this property or method". Error code is 800A01B6 Char 3. Can you tell me a better way to do this please or how I can fix this so it will work? Thank you, here is my code:

Code:
Option Explicit

        Dim objGroup, objFile, objSMBIOS, objItem, objWMIService
        Dim strComputer, wmi, oTS, colSMBIOS, IPConfigSet, IPConfig, colItems
        Dim objShell
        Dim FSO, f

        strComputer = Inputbox ("Enter the Computer name you want info on")
        Set objWMIService = GetObject("winmgmts:" _
                & "{impersonationLevel=impersonate}!\\" _
                & strComputer & "\root\cimv2")

        MsgBox "This will take between 5 - 10 mins, so please be patient." & " " & _
        "You will receive another message when scanning is complete." & VbCrLf & "Thank you."

'Gives user the option to view requested information (part 1 or 2)
        Set objShell = CreateObject("WScript.Shell")

'File name for remote system results
        Set fso = WScript.CreateObject("Scripting.Filesystemobject")
        Set f = fso.CreateTextFile("c:\users\" _
                & createobject("wscript.shell").expandenvironmentstrings("%username%") & "\Documents" & strComputer & ".txt")

        'On Error Resume Next
'Query network PC's for information
                Set objGroup = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
                        f.WriteLine "Today is:" & FormatDateTime(Now,0)
                        f.WriteLine "Computer:" & strComputer

'Find serial number
        Set colSMBIOS = objWMIService.ExecQuery("Select * from Win32_SystemEnclosure")
        For Each objSMBIOS in colSMBIOS
        f.WriteLine "Serial Number: " & objSMBIOS.SerialNumber
        Next

'Find MAC & IP
        Set IPConfigSet = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True")
        For Each IPConfig in IPConfigSet
                f.WriteLine "IP Address:   " & Join(IPConfig.IPAddress,"|")
                f.WriteLine "MAC Address:  " & IPConfig.MACAddress
        Next

'Query system to installed KB's
        f.WriteLine
        f.WriteLine "INSTALLED HOTFIXES"
        f.WriteLine
        Set colItems = objWMIService.ExecQuery("Select * from Win32_QuickFixEngineering")
        For Each objItem in colItems
                f.WriteLine "" & objItem.HotFixID
        Next

'Print out list of installed software
        f.WriteLine
        f.WriteLine "INSTALLED SOFTWARE"
        f.WriteLine

'Query system for installed software
        Set colItems = objWMIService.ExecQuery("Select * from Win32_Product")
        For Each objItem in colItems
                f.WriteLine "Caption: " & objItem.Caption
                f.WriteLine "Version: " & objItem.Version
                f.WriteLine
        Next
 
Your sample doesn't have 69 lines, so which line gives the error?
 
[code='Gives user the option to view requested information (part 1 or 2)
Set objShell = CreateObject("WScript.Shell")

'File name for remote system results
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
Set f = fso.CreateTextFile("c:\users\" _
& createobject("wscript.shell").expandenvironmentstrings("%username%") & "\Documents" & strComputer & ".txt")
vbscript][/code]
This seems flawed to me.
First of all you already loaded the object for the shell, then
loaded again? w/in another statement?
As previous post suggests, not sure the line numbers but be really surprised this works? Regardless I would clean this up.
Also if your environment is mixed this isn't flexing for older win7 machines w/ 'documents and settings'
Also not sure if all users have permissions for the WMI calls.
Also 5-10 mins....seems like a LONG time.
If you know what you want from the WMI queries, query them instead of SELECT * and then filtering, you're pulling a lot of extra information you apparently don't care for.
 
Also Win32_Product is evil, for a number of reasons (and is the root cause of the 5 - 10 minute running time of your script). You probably don't really want to be using it ...
 
>root cause of the problems

Oh, I'd suggest that

Set f = fso.CreateTextFile("c:\users\" _
& createobject("wscript.shell").expandenvironmentstrings("%username%") & "\Documents" & strComputer & ".txt")

might be a problem as well ...
 
Thank you for your responses.

guitarzan - I'm sorry about the code in my last message, part of it was cut off. I've included the remainder of the code below. The line of code that is giving me an error is objShell.Run("notepad" & f)

I think the error is because I'm asking it to open the saved file at location variable f.

Code:
'Print out list of installed software
        f.WriteLine
        f.WriteLine "INSTALLED SOFTWARE"
        f.WriteLine

'Query system for installed software
        Set colItems = objWMIService.ExecQuery("Select * from Win32_Product")
        For Each objItem in colItems
                f.WriteLine "Caption: " & objItem.Caption
                f.WriteLine "Version: " & objItem.Version
                f.WriteLine
        Next

'Gives user the option to view requested information (part 2 or 2)
        If MsgBox("Would you like to view this computer's information?", vbYesNo) = vbYes Then
                objShell.Run("notepad" & f)
        End If

VulcanJedi - Our environment is all Windows7, some 32bit and some 64bit. I did try commenting out the first object shell

Set objShell = CreateObject("WScript.Shell")

and then re-ran the script but it still error out at the same place. About the permissions, only those of us in IT will be running this script. I'll look more into just requesting the info we want from WMI instead of Select * From WMI. I used Select * because that's what I was taught.

strongm - could you tell me a better way than Win32_Product. I would much rather go about this a better way it's just that I don't know a better way. You're right, I'd rather have this script take less time to run because 5 - 10 mins is a long time to wait.
 
strongm - Thank you. I just read about Win32_Product from your link and from Microsoft's website. Wow I had no idea that querying WMI would do that. Microsoft suggested that I use Win32reg_AddRemovePrograms because it is a much lighter and effective way to get a list of all installed programs.

guitarzan - Thank you for your info too. Using the registry sounds like a better way to go.
 
objShell.Run("notepad" & f)
You need a space between notepad and the parameter. Also "f" will not give to the path to the text file. But you already know the name of the text file, why not save it in a variable:

Code:
Dim strOutputFile
...
strOutputFile = _
   "c:\users\" & createobject("wscript.shell").expandenvironmentstrings("%username%") & "\Documents" & strComputer & ".txt"
Set f = fso.CreateTextFile(strOutputFile)
...
objShell.Run("notepad " & strOutputFile)

While you are at it, you need a backslash between the folder "Documents" and the filename you are trying to create (this is what strongm was hinting at earlier).
... "\Documents[highlight #FCE94F]\[/highlight]" & strComputer & ".txt
 
>use Win32reg_AddRemovePrograms

Yep, but you will likely not find it ... - sometimes Microsoft copy and paste between KB articles without context. Basically Win32reg_AddRemovePrograms is a class that gets added only if you install the SCCM (SMS as was) client on your PC. But it is just a wrapper to reading the registry as the linked article already does.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top