Solved

How to save log file based on PC name

Posted on 2011-03-01
4
469 Views
Last Modified: 2012-05-11
I have the below vbscript I would like to run run on all PCs in our environment:

 
' Script to enable or disable Outlook Cache Mode
'
' SCRIPT DISCLAIMER: 
' This script is provided "as is" and should work in most environments, however, every environment is different so there are no guarantees!  
' Every effort has been made to ensure the files work as stated.  ActiveDir.Org assume no responsibility for your use of these files.
'
' Author : Matty Holland
' Last Update : 04-Apr-2007 
' ------------------------------------------------
Option Explicit

'On error resume next

' Environment specific Consts

' Path to your log file
Const LogFile = "\\server1\share\computername.log"

' Whether to enable Cache Mode on clients where this is currently disabled
Const EnableCacheMode = false

' Whether to disable Cache Mode on clients where this is currently enabled
Const DisableCacheMode = false

' Setting both EnableCacheMode and DisableCacheMode to false will report the status of ol cached mode on the client only

' End Environment specific Consts


' Registry Constants
Const HKCU = &H80000001
Const RegKeyProfiles = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles"
Const RegKeyName = "13dbb0c8aa05101a9bb000aa002fc45a"
Const RegValueName = "00036601"

' Global Variables
dim arrDisabledValue : arrDisabledValue = Array(&H04,&H00,&H00,&H00) ' value to set disabled
dim arrEnabledValue : arrEnabledValue = Array(&H84,&H01,&H00,&H00) ' value to set enabled (w/o PF's)
dim arrEnabledValueWithPF : arrEnabledValueWithPF = Array(&H84,&H05,&H00,&H00) ' value to set enabled with Public Folder Favourites

Dim ArrAllSubKeys, arrValueTypes, arrValues, objSubKey, subval, sValue, iret
Dim fullKeyPath
Dim oRegWMIProv
Dim oFso
Dim oLogFile
Dim oNet
Dim sComputerName
Dim sUserName

if EnableCacheMode and DisableCacheMode then
    Wscript.echo "Invalid configuration!  You cannot have both EnableCacheMode and DisableCacheMode enabled at the same time.  Quitting.."
    Wscript.quit(-1)
end if

Set oNet = CreateObject("Wscript.Network")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oRegWMIProv = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

sUserName = oNet.UserName
sComputerName = oNet.ComputerName

if oFso.FileExists(LogFile) then
    Set oLogFile = oFso.OpenTextFile(LogFile, 8)
    else
    Set oLogFile = oFso.CreateTextFile(LogFile, False)
    oLogFile.Writeline "ComputerName,UserName,ProfileName,OLCachedModeStatus"
end if

' List all subkeys (Profiles)
oRegWMIProv.EnumKey HKCU, RegKeyProfiles, ArrAllSubKeys
For Each objSubKey In arrAllSubKeys ' looping profiles
 Wscript.echo "Profile " & objSubKey
 fullKeyPath = RegKeyProfiles & "\" & objSubKey & "\" & RegKeyName

 ' enum all values under the cached mode reg id key
 oRegWMIProv.EnumValues HKCU, fullKeyPath, arrValues, arrValueTypes

 for each subval in arrValues
  ' if the sub value is the one we are interested in then process it
  If subval = RegValueName Then
    oRegWMIProv.GetBinaryValue HKCU, fullKeyPath, subval, sValue
    If (sValue(0) = arrDisabledValue(0)) AND (sValue(1) = arrDisabledValue(1)) AND (sValue(2) = arrDisabledValue(2)) AND (sValue(3) = arrDisabledValue(3)) Then

      oLogFile.WriteLine sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Disabled"
      Wscript.echo sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Disabled"

      if  EnableCacheMode then
        Wscript.echo objSubKey & " Disabled (enabling)"
        iret = oRegWMIProv.SetBinaryValue (HKCU,fullKeyPath,RegValueName,arrEnabledValue)
        oLogFile.WriteLine sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Now Enabled"
      end if

    else

      oLogFile.WriteLine sComputerName & "," & sUserName& "," & objSubKey & "," &  "Cached Mode Enabled"
      Wscript.echo sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Enabled"

      if DisableCacheMode then 
         Wscript.echo objSubKey & "Enabled (disabling)"
         oLogFile.WriteLine sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Now Disabled"
         iret = oRegWMIProv.SetBinaryValue (HKCU,fullKeyPath,RegValueName,arrDisabledValue)
      End If

  End If

  End If

 Next

Next

oLogFile.Close()

Set oRegWMIProv = Nothing
Set oFso = Nothing
Set oLogFile = Nothing
Set objSubKey = Nothing
Set oRegWMIProv = Nothing

Open in new window


As it is, each time a client will run this script, it will overwrite the log file (line 17 of the code).  

I am trying to find out how to modify the script so that I can either:

a) save the log file based on PC name (computername.log)

or preferably:

b) append each output to the log file, rather than overwrite.

Can this script be easily modified to do either one of those?
0
Comment
Question by:jam21000
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
Psy053 earned 500 total points
ID: 35012745
It should be a quick simple change to ge the result you are after.
In your script, locate this line:

Const LogFile = "\\server1\share\computername.log"

Change it to:
Const LogFile = \\Server1\Share\" & sComputerName & ".log"

And then move that line underneath the following line:
sComputerName = oNet.ComputerName

Open in new window

0
 
LVL 14

Expert Comment

by:Psy053
ID: 35012789
In response to the second part (b), the script appears to already be setup to append to the log file, if that log file exists:

This can be seen at the following line:
Set oLogFile = oFso.OpenTextFile(LogFile, 8)

The 8 indicates that the file will be opened for appending.
0
 
LVL 1

Author Closing Comment

by:jam21000
ID: 35020136
Thanks!
0
 
LVL 1

Author Comment

by:jam21000
ID: 35028748
Psy053, I think I spoke too soon.  I made the change you specified (for the "save log file as PC name" solution to the a) part):

But the script errors out on line 61 character 77 (last character on the line code relating to the log file).  Any clue why?
' Script to enable or disable Outlook Cache Mode
'
' SCRIPT DISCLAIMER: 
' This script is provided "as is" and should work in most environments, however, every environment is different so there are no guarantees!  
' Every effort has been made to ensure the files work as stated.  ActiveDir.Org assume no responsibility for your use of these files.
'
' Author : Matty Holland
' Last Update : 04-Apr-2007 
' ------------------------------------------------
Option Explicit

'On error resume next

' Environment specific Consts

' Path to your log file
'Const LogFile = "\\Server1\Share\" & sComputerName & ".log"

' Whether to enable Cache Mode on clients where this is currently disabled
Const EnableCacheMode = false

' Whether to disable Cache Mode on clients where this is currently enabled
Const DisableCacheMode = false

' Setting both EnableCacheMode and DisableCacheMode to false will report the status of ol cached mode on the client only

' End Environment specific Consts


' Registry Constants
Const HKCU = &H80000001
Const RegKeyProfiles = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles"
Const RegKeyName = "13dbb0c8aa05101a9bb000aa002fc45a"
Const RegValueName = "00036601"

' Global Variables
dim arrDisabledValue : arrDisabledValue = Array(&H04,&H00,&H00,&H00) ' value to set disabled
dim arrEnabledValue : arrEnabledValue = Array(&H84,&H01,&H00,&H00) ' value to set enabled (w/o PF's)
dim arrEnabledValueWithPF : arrEnabledValueWithPF = Array(&H84,&H05,&H00,&H00) ' value to set enabled with Public Folder Favourites

Dim ArrAllSubKeys, arrValueTypes, arrValues, objSubKey, subval, sValue, iret
Dim fullKeyPath
Dim oRegWMIProv
Dim oFso
Dim oLogFile
Dim oNet
Dim sComputerName
Dim sUserName

if EnableCacheMode and DisableCacheMode then
    Wscript.echo "Invalid configuration!  You cannot have both EnableCacheMode and DisableCacheMode enabled at the same time.  Quitting.."
    Wscript.quit(-1)
end if

Set oNet = CreateObject("Wscript.Network")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set oRegWMIProv = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

sUserName = oNet.UserName
sComputerName = oNet.ComputerName
Const LogFile = "\\Server1\Share\" & sComputerName & ".log"

if oFso.FileExists(LogFile) then
    Set oLogFile = oFso.OpenTextFile(LogFile, 8)
    else
    Set oLogFile = oFso.CreateTextFile(LogFile, False)
    oLogFile.Writeline "ComputerName,UserName,ProfileName,OLCachedModeStatus"
end if

' List all subkeys (Profiles)
oRegWMIProv.EnumKey HKCU, RegKeyProfiles, ArrAllSubKeys
For Each objSubKey In arrAllSubKeys ' looping profiles
 Wscript.echo "Profile " & objSubKey
 fullKeyPath = RegKeyProfiles & "\" & objSubKey & "\" & RegKeyName

 ' enum all values under the cached mode reg id key
 oRegWMIProv.EnumValues HKCU, fullKeyPath, arrValues, arrValueTypes

 for each subval in arrValues
  ' if the sub value is the one we are interested in then process it
  If subval = RegValueName Then
    oRegWMIProv.GetBinaryValue HKCU, fullKeyPath, subval, sValue
    If (sValue(0) = arrDisabledValue(0)) AND (sValue(1) = arrDisabledValue(1)) AND (sValue(2) = arrDisabledValue(2)) AND (sValue(3) = arrDisabledValue(3)) Then

      oLogFile.WriteLine sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Disabled"
      Wscript.echo sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Disabled"

      if  EnableCacheMode then
        Wscript.echo objSubKey & " Disabled (enabling)"
        iret = oRegWMIProv.SetBinaryValue (HKCU,fullKeyPath,RegValueName,arrEnabledValue)
        oLogFile.WriteLine sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Now Enabled"
      end if

    else

      oLogFile.WriteLine sComputerName & "," & sUserName& "," & objSubKey & "," &  "Cached Mode Enabled"
      Wscript.echo sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Enabled"

      if DisableCacheMode then 
         Wscript.echo objSubKey & "Enabled (disabling)"
         oLogFile.WriteLine sComputerName& "," &  sUserName& "," &  objSubKey& "," &  "Cached Mode Now Disabled"
         iret = oRegWMIProv.SetBinaryValue (HKCU,fullKeyPath,RegValueName,arrDisabledValue)
      End If

  End If

  End If

 Next

Next

oLogFile.Close()

Set oRegWMIProv = Nothing
Set oFso = Nothing
Set oLogFile = Nothing
Set objSubKey = Nothing
Set oRegWMIProv = Nothing

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question