Solved

How to save log file based on PC name

Posted on 2011-03-01
4
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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