Pages

8/31/2007

Windows::Inventory::Report files of particular extension::UPDATE


I seem to make this mistake a lot. My initial goal was to create a csv format file and import to excel or someplace. But I did not account for the case where a comma is in the data. It is never in the front of my mind that a comma is a valid character in a filename.
The corrected script is below.

'==========================================================================
' NAME: Script to search for files with listed extensions
'
'
'==========================================================================

Option Explicit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Const PATH_TO_INPUT = "in.txt"
Const PATH_TO_OUTPUT = "out.txt"

Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Dim shl
Set shl = WScript.CreateObject("WScript.Shell")

Dim input
Set input = fso.OpenTextFile(PATH_TO_INPUT)

Dim output
Set output = fso.CreateTextFile(PATH_TO_OUTPUT, True)

Dim wmiService
Dim wmiResults
Dim objwMIService
Dim colFiles
Dim objFile

Dim hostname

Dim line
Dim exec
Dim pingResults
Dim strFileName


While Not input.AtEndOfStream
line = input.ReadLine
hostname = ""
Set exec = shl.Exec("ping -n 2 -w 500 " & line)
pingResults = LCase(exec.StdOut.ReadAll)

If InStr(pingResults, "reply from") Then

WScript.Echo "Reply From: " & line
hostname = line

Set objWMIService = GetObject("winmgmts:\\" & hostname & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where Extension = 'pst' OR Extension = 'pdf' OR Extension = 'doc' OR Extension = 'xls'")

For Each objFile in colFiles
strFileName = Replace(objFile.Name, "," , " ")
output.WriteLine hostname & "," & strFileName & "," & objFile.FileSize
Next

Else
WScript.Echo line & " no response"
End If
Wend

output.Close
input.Close

Set wmiService = Nothing
Set wmiresults = Nothing

8/29/2007

Batch Processes


I recently have found myself running batch processes scanning inventory on long lists of machines. To have more control I usually generate a list of IP's or machine names and use it as an input file to my process.
It is obvious after the fact, but I often don't think of it until I've wasted time on something taking too long -- things go faster if they are broken up into groups and processed in parallel.
I need to spend some time to make up a script to automate this, but what I do is:
- Create folders 0 - 9 beneath a process folder.
- Copy the script to each folder.
- Break up my input file of items to process into 10 equal in.txt files and put one in each folder.
- I generally have the script create an output file such as out.txt
- Run the script redirecting output to stdout.txt
- Tile them all on my second monitor and watch each for errors or a Complete! message.
- Run a script like the one below to consolidate the logs.

REM collectLOG.cmd
del temp\*.* /y
For /d %%p in (*) do copy "%%p\stdout.txt" "temp\%%p.log"
copy temp\*.log final\discovery.log

- Run a script like the one below to consolidate the output.

REM collectCSV.cmd
For /d %%p in (*) do copy "%%p\out.txt" "final\%%p.csv"

Windows::Inventory::Report files of particular extension


For desired file extensions this script will report the filename and file size for all machines listed in input file.

'==========================================================================
' Script to search for files with listed extensions
'==========================================================================

Option Explicit

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Const PATH_TO_INPUT = "in.txt"
Const PATH_TO_OUTPUT = "out.txt"

Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Dim shl
Set shl = WScript.CreateObject("WScript.Shell")

Dim input
Set input = fso.OpenTextFile(PATH_TO_INPUT)

Dim output
Set output = fso.CreateTextFile(PATH_TO_OUTPUT, True)

Dim wmiService
Dim wmiResults
Dim objwMIService
Dim colFiles
Dim objFile
Dim hostname
Dim line
Dim exec
Dim pingResults

While Not input.AtEndOfStream
line = input.ReadLine
hostname = ""
Set exec = shl.Exec("ping -n 2 -w 500 " & line)
pingResults = LCase(exec.StdOut.ReadAll)

If InStr(pingResults, "reply from") Then

WScript.Echo "Reply From: " & line
hostname = line

Set objWMIService = GetObject("winmgmts:\\" & hostname & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where Extension = 'pst' OR Extension = 'pdf' OR Extension = 'doc' OR Extension = 'xls'")

For Each objFile in colFiles
output.WriteLine hostname & "," & objFile.Name & "," & objFile.FileSize
Next

Else
WScript.Echo line & " no response"
End If
Wend

output.Close
input.Close

Set wmiService = Nothing
Set wmiresults = Nothing

8/27/2007

Windows::File Locking Issues


Client Settings for Windows 2000, XP, 2003 Acting as a Workstation or Client

To modify the settings for the Workstation service, it is necessary to edit the registry, since Microsoft does not provide any method of configuring these options in their client setup. The registry key path is:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\
Services\LanmanWorkstation\Parameters


It will be necessary to add the following values as they are listed below, with the proper data type and the value listed (0 in all cases):

Setting Data Type Should be
UseLockReadUnlock
REG_DWORD
0


Indicates whether the redirector uses the lock-and-read and
write-and-unlock performance enhancements.

When this value is enabled, it generally provides a significant
performance benefit. However, database applications that lock
a range and don’t allow data within that range to be read will
suffer performance degradation unless this parameter is disabled.

UtilizeNtCaching
REG_DWORD
0


Indicates whether the redirector uses the cache manager to cache
the contents of files. Disable this parameter only to guarantee that
all data is flushed to the server immediately after it is written by the application.


Opportunistic locking is controlled differently in the newer versions of Windows than was done in Windows NT. The following registry key path is the location of the desired entry and that must be present and set to the associated value.

HKEY_LOCAL_MACHINE\System\CurrentControlSet\
Services\MRXSmb\Parameters


Setting Data Type Should be
OplocksDisabled
REG_DWORD
1


The OplocksDisabled registry value configures Windows clients to either request or not request opportunistic locks on a remote file.

8/23/2007

Opportunistic File Locking

http://www.jsifaq.com/SF/Tips/Tip.aspx?id=3108

http://support.microsoft.com/kb/129202

Disabling Read Caching on Windows Workstations

The Windows registry entry that controls read caching on Windows network clients is:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\VxD\VREDIR
DiscardCacheOnOpen REG_BINARY 0 or 1
Default: 0 (not disabled)


To disable read caching, the value of DiscardCacheOnOpen must be set to 1.

If you do change this Registry value, you will have to reboot the PC to ensure that the new setting goes into effect.

Disabling Opportunistic Locking on Windows Servers

There are 2 Windows registry entries that control opportunistic locking (oplocks) on Windows network servers:
1. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters EnableOpLockForceClose
2. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters EnableOplocks

1. EnableOpLockForceClose

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
EnableOpLockForceClose REG_DWORD 0 or 1
Default: 0 (not disabled)

To disable oplocks, the value of EnableOpLockForceClose must be set to 1.

2. EnableOplocks

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters
EnableOplocks REG_DWORD 0 or 1
Default: 1 (true)
To disable oplocks, the value of EnableOplocks must be set to 0.


Note: The location of the registry entry for opportunistic locking has changed in Windows 2000 from the earlier location in Microsoft Windows NT. In Windows 2000, the registry entry that disables opportunistic locking is:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MRXSmb\Parameters\
OplocksDisabled REG_DWORD 0 or 1
Default: 0 (not disabled)
To disable oplocks, the value of OplocksDisabled must be set to 1.


Note: Windows 2000 will still respect the EnableOplocks registry value used to disable oplocks in earlier versions of Windows.

Disabling Opportunistic Locking on Windows Workstations

If you use a Windows NT family workstation in place of a server, you must also disable opportunistic locking (oplocks) on that workstation. For example, if you use a PC with the Windows NT Workstation operating system instead of Windows NT Server, Windows 2000 Professional instead of Windows 2000 Server, or Windows XP Home instead of Windows XP Professional you will need to disable oplocks on that system.

The major difference is the location in the Windows registry where the values for disabling oplocks are entered. Instead of the LanManServer location, the LanManWorkstation location is used here.

There are 2 Windows registry entries that control opportunistic locking (oplocks) on Windows network workstations:

1. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters EnableOpLockForceClose
2. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters EnableOplocks

1. EnableOpLockForceClose
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters
EnableOpLockForceClose REG_DWORD 0 or 1
Default: 0 (not disabled)

To disable oplocks, the value of EnableOpLockForceClose must be set to 1.

2. EnableOplocks
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanManWorkstation\Parameters
EnableOplocks REG_DWORD 0 or 1
Default: 1 (true)
To disable oplocks, the value of EnableOplocks must be set to 0.

8/21/2007

Perl::Regular Expression::Matching Expression Variables



Match Variables
If a =~ match expression is true, the special variables $1, $2, ... will be the substrings that matched parts of the pattern in parenthesis -- $1 matches the first left parenthesis, $2 the second left parenthesis, and so on. The following pattern picks out three words separated by whitespace...

if ("this and that" =~ /(\w+)\s+(\w+)\s+(\w+)/) {

## if the above matches, $1=="this", $2=="and", $3=="that"

This is a nice way to parse a string -- write a regular expression for the pattern you expect putting parenthesis around the parts you want to pull out. Only use $1, $2, etc. when the if =~ returns true. Other regular-expression systems use \1 and \2 instead of $1 $2, and Perl supports that syntax as well. There are three other special variables: $& (dollar-ampersand) = the matched string, $` (dollar-back-quote) = the string before what was matched, and $' (dollar-quote) = the string following what was matched.

The following loop rips through a string and pulls out all the email addresses. It demonstrates using a character class, using $1 etc. to pull out parts of the match string, and using $' after the match.

$str = 'blah blah nick@cs.stanford.edu, blah blah balh billg@microsoft.com blah blah';

while ($str =~ /(([\w._-]+)\@([\w._-]+))/) { ## look for an email addr
print "user:$2 host:$3 all:$1\n"; ## parts of the addr
$str = $'; ## set the str to be the "rest" of the string
}

output:
user:nick host:cs.stanford.edu all:nick@cs.stanford.edu
user:billg host:microsoft.com all:billg@microsoft.com


Thanks to: http://cslibrary.stanford.edu/108/EssentialPerl.html