Friday, September 20, 2013

Powershell Script - Tip: Delimited String "Contants" & Multi-server Logparser scripts

9/19/2013: This is a useful tip that I get some solid routine mileage out of: Using a delimited string variable, to function as an Array Constant. Primary usage is when I want to loop through a set of variant values, and execute the same commands against each value.

In the code below (which returns some sample sorted information on the various Exchange versions, and roles) I'm setting up a $Sites variable, containing a semi-colon-delimited string, that is then split and reassigned to the variable as an Array. The resulting array is then fed through a simple foreach loop to output information about the servers and roles in each AD Site.


Array Stored as Semi-colon-delimited string. 

Language/Tools: Powershell
Target: Exchange 2007/2010 Management Console

$Sites = "US;AU;EU" ; 
$Sites=$Sites.split(";") ; 
foreach ($Site in $Sites) {
  write-host -foregroundcolor yellow "SITE: " $Site; get-exchangeserver | 
    where { $_.Site -like "*$Site" } | 
    sort AdminDisplayVersion,ServerRole,Name | 
    select Name,ServerRole,AdminDisplayVersion 
}


 
Typical output:
SITE:  US

Name                                                                 ServerRole AdminDisplayVersion
----                                                                 ---------- -------------------
US-ServerUM1                                                   UnifiedMessaging Version 14.2 (Build 247.5)
US-MbxServer2                                                           Mailbox Version 14.3 (Build 123.4)
US-MbxServer2                                                           Mailbox Version 14.3 (Build 123.4)
US-HUBCAS1                                           ClientAccess, HubTransport Version 14.3 (Build 123.4)
US-HUBCAS2                                           ClientAccess, HubTransport Version 14.3 (Build 123.4)
US-PFServer1                                                            Mailbox Version 8.3 (Build 83.6)
US-MbxServer1                                                           Mailbox Version 8.3 (Build 83.6)




Multi-server Logparser scripts (using Delimited-String Constants):

The above is a simple contrived example to get the concept across. More frequently I use delimited-variable strings for storing and looping through arrays of log directories (for get-messagetracking or logparser processing). For example: Below I have a script that runs a Microsoft Logparser query (summarizing hourly EWS hits) from the IIS logs matching today's date -- pretty common stuff.
But this version has the added benefit of executing the command against a series of different CAS servers (by walking a delimited-string constant containing the UNC paths to each server's logs)...

Logparsing multiple CAS server IIS log directories for EWS traffic per hour. 

Language: Powershell /MS LogParser
Target: EMS or Powershell, parsing Exchange 2007/2010 CAS Server IIS logs

# Array of IIS paths, stored as semi-colon-delimited string
$LogDirs="\\US-HUBCAS1\e$\Weblogs\W3SVC1\;\\US-HUBCAS2\e$\Weblogs\W3SVC1\" ; $LogDirs=$LogDirs.split(";") ; 
# constants for single & double quotes
$sQuot = [char]34 ; $sQuotS = [char]39
foreach ($LogDir in $LogDirs) {
  # build the log path to today's logs
  $sTodaysLogsStr = $LogDir
  # Ex2007 IIS log filename variant
  #$sTodaysLogsStr += "ex" 
  # Ex2010 variant
  $sTodaysLogsStr += "u_ex" 
  $sTodaysLogsStr += (Get-Date -format "yMMdd") + "*.log"
  # build the logparser sql command
  $strSQL =  "SELECT QUANTIZE(TO_LOCALTIME(time),3600) AS Hour, count(*) AS Hits" ;    
  $strSQL += " FROM " + ($sQuotS + $sTodaysLogsStr + $sQuotS) 
  $strSQL += " WHERE cs-uri-stem LIKE '%/EWS/%' GROUP BY Hour ORDER BY Hour" ; 
  # assemble the logparser commandline
  $strExecCmd = ". logparser.exe " + $sQuot + $strSQL + $sQuot + " -rtp:-1" ;
  $strExecCmd
  # execute the assembled command
  Invoke-Expression  $strExecCmd 
}
 
Which produces two passes of Logparser output like so:
[PS] C:\usr\local\bin>c:\tmp\tmp1.ps1
. logparser.exe "SELECT QUANTIZE(TO_LOCALTIME(time),3600) AS Hour, count(*) AS Hits FROM '\\US-HUBCAS1\e$\IIS Weblog\W3SVC1\u_ex130920*.log' WHERE cs-uri-stem LIKE '%/EWS/%' GROUP BY Hour ORDER BY Hour" -rtp:-1
Hour     Hits
-------- ----
00:00:00 477
01:00:00 2289
02:00:00 2809
03:00:00 3300
04:00:00 3655
05:00:00 5657
06:00:00 7350
07:00:00 9135
08:00:00 9340
09:00:00 9053
10:00:00 8615
11:00:00 8894
12:00:00 6568
18:00:00 1132
19:00:00 758
20:00:00 749
21:00:00 612
22:00:00 661
23:00:00 571

Statistics:
-----------
Elements processed: 392382
Elements output:    19
Execution time:     12.12 seconds

. logparser.exe "SELECT QUANTIZE(TO_LOCALTIME(time),3600) AS Hour, count(*) AS Hits FROM '\\US-HUBCAS2\e$\IIS Weblog\W3SVC1\u_ex130920*.log' WHERE cs-uri-stem LIKE '%/EWS/%' GROUP BY Hour ORDER BY Hour" -rtp:-1
Hour     Hits
-------- -----
00:00:00 3205
01:00:00 2979
02:00:00 3133
03:00:00 3554
04:00:00 3833
05:00:00 5564
06:00:00 9284
07:00:00 10352
08:00:00 10016
09:00:00 9733
10:00:00 9869
11:00:00 10096
12:00:00 7526
18:00:00 5183
19:00:00 3445
20:00:00 2850
21:00:00 3447
22:00:00 3367
23:00:00 3387

Statistics:
-----------
Elements processed: 495761
Elements output:    19
Execution time:     14.72 seconds

No comments:

Post a Comment