• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1452
  • Last Modified:

VBscript: If file exist, run code and output to text file. If file not exist only output to text file

Hi

I want to run a VBSCRIPT at login for my users that accomplishes the following.

1 I want to check if a folder exist on a workstation.
2 If folder exists, I want it to output to the "Acrobat9Installed.txt": username and computername
3 If folder exists, I also want it to run the code that modifies the registry.
4 If the folder does not exist, I want it only to output: username and computername to a diffrent txt file "Acrobat9NOTInstalled.txt" and it should  NOT make the registry change.

I only want "UN and CompName" which are  these two in the file and not all the other obj.

 objTextFile.WriteLine objComputer.Name & vbtab & _
 objComputer.Username & vbtab & _

Its giving me many errors if i reduce the codes ect. I have snippets that work independently, i just cannot get them to work together for my purpose. The snippets are sectioned below.

Thank you!!!

Regards /Dabosa
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists ("C:\Program Files\Adobe\Reader 9.0\Reader") Then 
 
'--------------------------------------------------------'
 
on Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("\\servername\folder\Acrobat9Installed.txt",8)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_BIOS")
Set colSettings = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_OperatingSystem")
Set colComputer = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_ComputerSystem")
 
For Each objBIOS in colBIOS
For Each objOperatingSystem in colSettings
For Each objComputer in colComputer
 objTextFile.WriteLine objComputer.Name & vbtab & _
 objComputer.Username & vbtab & _
 objComputer.Manufacturer & vbtab & _
 objComputer.Model & vbtab & _
 objComputer.CurrentTimeZone & vbtab & _
 objBIOS.Manufacturer & vbtab & _
 objBIOS.SerialNumber & vbtab & _
 objBIOS.SMBIOSBIOSVersion & vbtab & _
 objOperatingSystem.Name & vbtab & _
 objOperatingSystem.Version & vbtab & _
 objOperatingSystem.ServicePackMajorVersion & vbtab & _
 objOperatingSystem.Manufacturer & vbtab & _
 objOperatingSystem.WindowsDirectory & vbtab & _
 objOperatingSystem.FreePhysicalMemory & vbtab & _
 objOperatingSystem.TotalVirtualMemorySize & vbtab & _
 objOperatingSystem.FreeVirtualMemory 
Next
Next
Next
 
objTextFile.Close
 
'-------------------------------------------------------'
 
Const HKEY_CLASSES_ROOT = &H80000000
 
strComputer = "."
 
Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Adobe\Acrobat\Exe"
strValueName = ""
strValue = """C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"""
 
objRegistry.SetStringValue HKEY_CLASSES_ROOT, strKeyPath, strValueName, strValue
 
'------------------------------------------------------'
 
Else
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists ("C:\Program Files\Adobe\Reader 9.0\Reader") Then
 
'------------------------------------------------------'
 
 
on Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("\\servername\folder\Acrobat9NOTInstalled.txt",8)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colBIOS = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_BIOS")
Set colSettings = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_OperatingSystem")
Set colComputer = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_ComputerSystem")
 
For Each objBIOS in colBIOS
For Each objOperatingSystem in colSettings
For Each objComputer in colComputer
 objTextFile.WriteLine objComputer.Name & vbtab & _
 objComputer.Username & vbtab & _
 objComputer.Manufacturer & vbtab & _
 objComputer.Model & vbtab & _
 objComputer.CurrentTimeZone & vbtab & _
 objBIOS.Manufacturer & vbtab & _
 objBIOS.SerialNumber & vbtab & _
 objBIOS.SMBIOSBIOSVersion & vbtab & _
 objOperatingSystem.Name & vbtab & _
 objOperatingSystem.Version & vbtab & _
 objOperatingSystem.ServicePackMajorVersion & vbtab & _
 objOperatingSystem.Manufacturer & vbtab & _
 objOperatingSystem.WindowsDirectory & vbtab & _
 objOperatingSystem.FreePhysicalMemory & vbtab & _
 objOperatingSystem.TotalVirtualMemorySize & vbtab & _
 objOperatingSystem.FreeVirtualMemory 
Next
Next
Next
 
objTextFile.Close

Open in new window

0
Dabosa
Asked:
Dabosa
  • 6
  • 3
  • 3
1 Solution
 
sirbountyCommented:
This should do it..
Const FilePath = "\\servername\folder\"
Const HKEY_CLASSES_ROOT = &H80000000
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextFile, BIOSData, OSData, CSData
 
If objFSO.FolderExists ("C:\Program Files\Adobe\Reader 9.0\Reader") Then 
  DoUpdate
Else
  DoMissing
End If
 
objTextFile.Close
 
Sub  DoUpdate()
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9Installed.txt",8)
  GetDetails()
  objTextFile.WriteLine objComputer.Name & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
 
  Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
  strKeyPath = "SOFTWARE\Adobe\Acrobat\Exe"
  strValueName = ""
  strValue = """C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"""
 
  objRegistry.SetStringValue HKEY_CLASSES_ROOT, strKeyPath, strValueName, strValue
End Sub
 
Sub DoMissing()
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9NOTInstalled.txt",8)
  GetDetails()
  objTextFile.WriteLine objComputer.Name & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
End Sub
 
Sub GetDetails()
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  Set colBIOS = objWMIService.ExecQuery  ("SELECT * FROM Win32_BIOS")
 
  For Each objBIOS in colBIOS
    BIOSData = objBIOS.Manufacturer & vbtab & objBIOS.SerialNumber & vbtab & objBIOS.SMBIOSBIOSVersion 
  Next
 
  Set colSettings = objWMIService.ExecQuery  ("SELECT * FROM Win32_OperatingSystem")
  For Each objOperatingSystem in colSettings
    OSData = objOperatingSystem.Name & vbtab & objOperatingSystem.Version & vbtab & objOperatingSystem.ServicePackMajorVersion & vbtab & _
         objOperatingSystem.Manufacturer & vbtab & objOperatingSystem.WindowsDirectory & vbtab & objOperatingSystem.FreePhysicalMemory & vbtab & _
         objOperatingSystem.TotalVirtualMemorySize & vbtab & objOperatingSystem.FreeVirtualMemory 
  Next
 
  Set colComputer = objWMIService.ExecQuery ("SELECT * FROM Win32_ComputerSystem")
  For Each objComputer in colComputer
    CSData = objComputer.Username & vbtab & objComputer.Manufacturer & vbtab & objComputer.Model & vbtab &  objComputer.CurrentTimeZone
  Next
End Sub

Open in new window

0
 
DabosaAuthor Commented:
I get an error if I run it on line 18, Char 3

Error: Object required: 'objComputer'
Code:800A01A8

I have only modified "\\servername\folder\"

Thanks!!
0
 
käµfm³d 👽Commented:
Try:
Const FilePath = "\\servername\folder\"
Const HKEY_CLASSES_ROOT = &H80000000
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextFile, BIOSData, OSData, CSData
 
If objFSO.FolderExists ("C:\Program Files\Adobe\Reader 9.0\Reader") Then 
  DoUpdate objComputer
Else
  DoMissing objComputer
End If
 
objTextFile.Close
 
Sub  DoUpdate(ByRef objComputer As Object)
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9Installed.txt",8)
  GetDetails()
  objTextFile.WriteLine objComputer.Name & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
 
  Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
  strKeyPath = "SOFTWARE\Adobe\Acrobat\Exe"
  strValueName = ""
  strValue = """C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"""
 
  objRegistry.SetStringValue HKEY_CLASSES_ROOT, strKeyPath, strValueName, strValue
End Sub
 
Sub DoMissing(ByRef objComputer As Object)
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9NOTInstalled.txt",8)
  GetDetails()
  objTextFile.WriteLine objComputer.Name & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
End Sub
 
Sub GetDetails()
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  Set colBIOS = objWMIService.ExecQuery  ("SELECT * FROM Win32_BIOS")
 
  For Each objBIOS in colBIOS
    BIOSData = objBIOS.Manufacturer & vbtab & objBIOS.SerialNumber & vbtab & objBIOS.SMBIOSBIOSVersion 
  Next
 
  Set colSettings = objWMIService.ExecQuery  ("SELECT * FROM Win32_OperatingSystem")
  For Each objOperatingSystem in colSettings
    OSData = objOperatingSystem.Name & vbtab & objOperatingSystem.Version & vbtab & objOperatingSystem.ServicePackMajorVersion & vbtab & _
         objOperatingSystem.Manufacturer & vbtab & objOperatingSystem.WindowsDirectory & vbtab & objOperatingSystem.FreePhysicalMemory & vbtab & _
         objOperatingSystem.TotalVirtualMemorySize & vbtab & objOperatingSystem.FreeVirtualMemory 
  Next
 
  Set colComputer = objWMIService.ExecQuery ("SELECT * FROM Win32_ComputerSystem")
  For Each objComputer in colComputer
    CSData = objComputer.Username & vbtab & objComputer.Manufacturer & vbtab & objComputer.Model & vbtab &  objComputer.CurrentTimeZone
  Next
End Sub

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
DabosaAuthor Commented:
Still error on

Line:15
Char 33
ERROR: Expected: ')'
CODE: 800A03EE

I have set the path to C:\AcroReader\  for testing


Thanks!!
0
 
sirbountyCommented:
Here you go...
 FilePath = "\\servername\folder\"
Const HKEY_CLASSES_ROOT = &H80000000
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextFile, BIOSData, OSData, CSData, strComputer
 
If objFSO.FolderExists ("C:\Program Files\Adobe\Reader 9.0\Reader") Then 
  DoUpdate
Else
  DoMissing
End If
 
objTextFile.Close
 
Sub  DoUpdate()
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9Installed.txt",8)
  GetDetails()
  objTextFile.WriteLine objComputer.Name & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
 
  Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
  strKeyPath = "SOFTWARE\Adobe\Acrobat\Exe"
  strValueName = ""
  strValue = """C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"""
 
  objRegistry.SetStringValue HKEY_CLASSES_ROOT, strKeyPath, strValueName, strValue
End Sub
 
Sub DoMissing()
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9NOTInstalled.txt",8)
  GetDetails()
  objTextFile.WriteLine strComputer & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
End Sub
 
Sub GetDetails()
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  Set colBIOS = objWMIService.ExecQuery  ("SELECT * FROM Win32_BIOS")
 
  For Each objBIOS in colBIOS
    BIOSData = objBIOS.Manufacturer & vbtab & objBIOS.SerialNumber & vbtab & objBIOS.SMBIOSBIOSVersion 
  Next
 
  Set colSettings = objWMIService.ExecQuery  ("SELECT * FROM Win32_OperatingSystem")
  For Each objOperatingSystem in colSettings
    OSData = objOperatingSystem.Name & vbtab & objOperatingSystem.Version & vbtab & objOperatingSystem.ServicePackMajorVersion & vbtab & _
         objOperatingSystem.Manufacturer & vbtab & objOperatingSystem.WindowsDirectory & vbtab & objOperatingSystem.FreePhysicalMemory & vbtab & _
         objOperatingSystem.TotalVirtualMemorySize & vbtab & objOperatingSystem.FreeVirtualMemory 
  Next
 
  Set colComputer = objWMIService.ExecQuery ("SELECT * FROM Win32_ComputerSystem")
  For Each objComputer in colComputer
    strComputer = objComputer.Name
    CSData = objComputer.Username & vbtab & objComputer.Manufacturer & vbtab & objComputer.Model & vbtab &  objComputer.CurrentTimeZone
  Next
End Sub

Open in new window

0
 
DabosaAuthor Commented:
Same error as first time

line 18
Char 3
Error: Object required: 'objComputer'
Code:800A01A

Thanks!!
0
 
käµfm³d 👽Commented:
Is what's posted the the entire code, because I don't see anything missing related to a ')'.
0
 
sirbountyCommented:
forgot to update that line - sorry - try this:
 FilePath = "\\servername\folder\"
Const HKEY_CLASSES_ROOT = &H80000000
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextFile, BIOSData, OSData, CSData, strComputer
 
If objFSO.FolderExists ("C:\Program Files\Adobe\Reader 9.0\Reader") Then 
  DoUpdate
Else
  DoMissing
End If
 
objTextFile.Close
 
Sub  DoUpdate()
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9Installed.txt",8)
  GetDetails()
  objTextFile.WriteLine strComputer & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
 
  Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
  strKeyPath = "SOFTWARE\Adobe\Acrobat\Exe"
  strValueName = ""
  strValue = """C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"""
 
  objRegistry.SetStringValue HKEY_CLASSES_ROOT, strKeyPath, strValueName, strValue
End Sub
 
Sub DoMissing()
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9NOTInstalled.txt",8)
  GetDetails()
  objTextFile.WriteLine strComputer & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
End Sub
 
Sub GetDetails()
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  Set colBIOS = objWMIService.ExecQuery  ("SELECT * FROM Win32_BIOS")
 
  For Each objBIOS in colBIOS
    BIOSData = objBIOS.Manufacturer & vbtab & objBIOS.SerialNumber & vbtab & objBIOS.SMBIOSBIOSVersion 
  Next
 
  Set colSettings = objWMIService.ExecQuery  ("SELECT * FROM Win32_OperatingSystem")
  For Each objOperatingSystem in colSettings
    OSData = objOperatingSystem.Name & vbtab & objOperatingSystem.Version & vbtab & objOperatingSystem.ServicePackMajorVersion & vbtab & _
         objOperatingSystem.Manufacturer & vbtab & objOperatingSystem.WindowsDirectory & vbtab & objOperatingSystem.FreePhysicalMemory & vbtab & _
         objOperatingSystem.TotalVirtualMemorySize & vbtab & objOperatingSystem.FreeVirtualMemory 
  Next
 
  Set colComputer = objWMIService.ExecQuery ("SELECT * FROM Win32_ComputerSystem")
  For Each objComputer in colComputer
    strComputer = objComputer.Name
    CSData = objComputer.Username & vbtab & objComputer.Manufacturer & vbtab & objComputer.Model & vbtab &  objComputer.CurrentTimeZone
  Next
End Sub

Open in new window

0
 
käµfm³d 👽Commented:
It might be the ByRef I put in there. Perhaps try it without it.
Const FilePath = "\\servername\folder\"
Const HKEY_CLASSES_ROOT = &H80000000
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objTextFile, BIOSData, OSData, CSData
 
If objFSO.FolderExists ("C:\Program Files\Adobe\Reader 9.0\Reader") Then 
  DoUpdate objComputer
Else
  DoMissing objComputer
End If
 
objTextFile.Close
 
Sub  DoUpdate(objComputer)
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9Installed.txt",8)
  GetDetails()
  objTextFile.WriteLine objComputer.Name & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
 
  Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")
  strKeyPath = "SOFTWARE\Adobe\Acrobat\Exe"
  strValueName = ""
  strValue = """C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe"""
 
  objRegistry.SetStringValue HKEY_CLASSES_ROOT, strKeyPath, strValueName, strValue
End Sub
 
Sub DoMissing(objComputer)
  Set objTextFile = objFSO.OpenTextFile(FilePath & "Acrobat9NOTInstalled.txt",8)
  GetDetails()
  objTextFile.WriteLine objComputer.Name & vbtab & CSData & vbTab & BIOSData & vbTab & OSData
End Sub
 
Sub GetDetails()
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  Set colBIOS = objWMIService.ExecQuery  ("SELECT * FROM Win32_BIOS")
 
  For Each objBIOS in colBIOS
    BIOSData = objBIOS.Manufacturer & vbtab & objBIOS.SerialNumber & vbtab & objBIOS.SMBIOSBIOSVersion 
  Next
 
  Set colSettings = objWMIService.ExecQuery  ("SELECT * FROM Win32_OperatingSystem")
  For Each objOperatingSystem in colSettings
    OSData = objOperatingSystem.Name & vbtab & objOperatingSystem.Version & vbtab & objOperatingSystem.ServicePackMajorVersion & vbtab & _
         objOperatingSystem.Manufacturer & vbtab & objOperatingSystem.WindowsDirectory & vbtab & objOperatingSystem.FreePhysicalMemory & vbtab & _
         objOperatingSystem.TotalVirtualMemorySize & vbtab & objOperatingSystem.FreeVirtualMemory 
  Next
 
  Set colComputer = objWMIService.ExecQuery ("SELECT * FROM Win32_ComputerSystem")
  For Each objComputer in colComputer
    CSData = objComputer.Username & vbtab & objComputer.Manufacturer & vbtab & objComputer.Model & vbtab &  objComputer.CurrentTimeZone
  Next
End Sub

Open in new window

0
 
DabosaAuthor Commented:
Same Error Kaufmed
0
 
DabosaAuthor Commented:
Yes sirbounty, that did it!

Its working ive been testing it with many test scenarios.
I would like to have the timeand date also printed in txt file.
I cant seem to find a objcomputer.time&date. How do one actually find these properties or is it methods?
 Its difficult to know what to use, is there a list somewhere to check what X is and what it can do for obj.Computer.X?

For Each objComputer in colComputer
    strComputer = objComputer.Name
    TimeDate = objComputer.Time

Thanks!!
0
 
DabosaAuthor Commented:
Yes sirbounty, that did it!

Its working ive been testing it with many test scenarios.
I would like to have the timeand date also printed in txt file.
I cant seem to find a objcomputer.time&date. How do one actually find these properties or is it methods?
 Its difficult to know what to use, is there a list somewhere to check what X do for obj.Computer.X?

For Each objComputer in colComputer
    strComputer = objComputer.Name
    TimeDate = objComputer.Time

Thanks very much! /D
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now