Solved

How to save log file based on PC name

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

930 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

10 Experts available now in Live!

Get 1:1 Help Now