Link to home
Start Free TrialLog in
Avatar of Dabosa
Dabosa

asked on

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

Avatar of sirbounty
sirbounty
Flag of United States of America image

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

Avatar of Dabosa
Dabosa

ASKER

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!!
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

Avatar of Dabosa

ASKER

Still error on

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

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


Thanks!!
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

Avatar of Dabosa

ASKER

Same error as first time

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

Thanks!!
Is what's posted the the entire code, because I don't see anything missing related to a ')'.
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Avatar of Dabosa

ASKER

Same Error Kaufmed
Avatar of Dabosa

ASKER

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!!
Avatar of Dabosa

ASKER

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