Solved

How to save log file based on PC name

Posted on 2011-03-01
4
466 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now