Sign in
|
Join PowerShell.com!
|
Help
Home
PowerTips
Ask the Experts
Forums
Webcasts
Blogs
eBookV2
Script Library
Twitter Grid
Modules
QuickClick
Scripts
Snippets
Videos
Library
»
Script Library
»
Logs
»
Slideshow
Logs Slideshow
Share
|
Browse Library
Module Library
QuickClick Library
Script Library
Active Directory
BizTalk
Citrix
Clustering
Desktop
Exchange Server 2003
Exchange Server 2007
File System
Group Policy
Internet Information Server (IIS)
Local Accounts
Logs
Microsoft Office
Microsoft Team Foundation Server
MySQL
Networking
Registry
Remote Desktop Services
Remoting
Security
SharePoint
SQL Server
System Center Virtual Machine Manager
System Center Configuration Manager
System Center Operations Manager
Tutorial
Terminal Server
Using .Net
Virtual Server
VMware
Windows 7
Windows HPC
Windows Server 2000
Windows Server 2003
Windows Server 2008
Windows XP
WMI
Misc
Snippet Library
Video Library
Members Only
Tags
gprs
log
modem
monitor
online
View more
PowerShell log file system.
<
#
.
SYNOPSIS
Get-BCLog
retrieves a log file which is a collection of BCLogEntries
which have been stored
in
an
XML
file.
.
DESCRIPTION
There are a collection of cmdlets for supporting log functionality.
The basis of the system is a custom PowerShell object which contains
five parameters:
TimeStamp
Sources
Category
Message
Level
The entries are returned latest first.
.
PARAMETER
logFileName
The full name of the
xml
file that stores the
xml
structure.
.
EXAMPLE
Get-BCLog
c:\data\logfile.
xml
#>
function
Get-BCLog
{
PARAM
(
[parameter(Mandatory
=
$true
)]
[
String
]
$logFileName
)
if
(
Test-Path
-Path
$logFileName
)
{
$tempLog
=
@(
Import-Clixml
$logFileName
)
}
else
{
$tempLog
=
$null
}
return
$tempLog
}
<
#
.
SYNOPSIS
Get-BCLogEntry
retrieves a collection of BCLogEntry objects from a
log file that meet specified criteria.
.
DESCRIPTION
Get-BCLogEntry
retrieves log entries from a log file.
Command line
parameters define how many entries to retrieve and the entries to
retrieve based on the the parameters specified. With the exception
of the level and minimum level parameters, all the parameters are
'anded'
together.
The level and minimumLevel parameters are also
'anded'
, the level parameter takes precedence over the
minimumLevel parameter.
.
PARAMETER
logFileName
This parameter contains the path to the file that stores the log.
.
PARAMETER
count
The maximum number of log entries to retrieve.
The default retrieves
all entries.
.
PARAMETER
level
The level number of log entries to retrieve.
The default will
retrieve all entries.
This parameter takes precedence over the minimumLevel parameter.
.
PARAMETER
minimumLevel
Log entries with a level setting greater than or equal to this number
will be retrieved.
The default will retrieve all entries. This parameter will be
overridden by the level parameter.
.
PARAMETER
taskCategory
Restrict the entries retrieved to those from a specific category.
This
parameter accepts wildcards.
.
PARAMETER
source
Restrict the entries retrieved to those from a specific source.
This
parameter accepts wildcards.
.
PARAMETER
startTimeStamp
This parameter restricts the entries returned to those with a
timeStamp greater than or equal to the value specified.
.
PARAMETER
endTimeStamp
This parameter restricts the entries returned to those with a
timeStamp less than or equal to the value specified.
.
EXAMPLE
Get-BCLogEntry
c:\data\mylog.
xml
.
EXAMPLE
Get-BCLogEntry
c:\data\mylog.
xml
-count
1
.
EXAMPLE
Get-BCLogEntry
c:\data\mylog.
xml
-count
1
-source
inst
*
.
EXAMPLE
Get-BCLogEntry
c:\data\mylog.
xml
-source
Inst
*
-taskCategory
Archive
-minimumLevel
2
.
EXAMPLE
Get-BCLogEntry
c:\data\mylog.
xml
-count
10
-startTimeStamp
(
get-Date
-hour
13
-minute
5)
-endTimeStamp
(
Get-Date
-hour
13
-minute
12)
#>
function
Get-BCLogEntry
{
PARAM
(
[parameter(Mandatory
=
$true
)]
[
string
]
$logFileName
,
[parameter()]
[
int
]
$count
=
0,
[parameter()]
[
int
]
$level
=
-
1,
[parameter()]
[
int
]
$minimumLevel
=
0,
[parameter()]
[
string
]
$taskCategory
=
"*"
,
[parameter()]
[
string
]
$source
=
"*"
,
[parameter()]
[
DateTime
]
$startTimeStamp
,
[parameter()]
[
DateTime
]
$endTimeStamp
)
$log
=
Get-BCLog
$logFilename
if
(
$log
-ne
$null
)
{
$selectedEntries
=
@()
if
(
$startTimeStamp
-eq
$null
)
{
$startTimeStamp
=
(
Get-Date
-Year
1900)
}
if
(
$endTimeStamp
-eq
$null
)
{
$endTimeStamp
=
(
Get-Date
)
}
if
(
$log
-eq
$null
)
{
Write-Warning
"Log file $logFileName was not found"
}
else
{
foreach
(
$logEntry
in
$log
)
{
if
(
$logEntry
-ne
$null
)
{
if
(
$logEntry
.
source
-like
$source
-and
$logEntry
.
TaskCategory
-like
$taskCategory
-and
$logEntry
.
TimeStamp
-ge
$startTimeStamp
-and
$logEntry
.
TimeStamp
-le
$endTimeStamp
-and
(
$logEntry
.
Level
-eq
$level
-or
$level
-lt
0)
-and
$logEntry
.
Level
-ge
$minimumLevel
)
{
$selectedEntries
+=
$logEntry
if
(
$selectedEntries
.
Count
-ge
$count
-and
$count
-gt
0)
{
break
;
}
}
}
}
Write-Output
$selectedEntries
}
}
else
{
throw
"$logFileName was not found."
}
}
<
#
.
SYNOPSIS
Add-BCLogEntry
creates and adds a log entry object to the identified log
file.
.
DESCRIPTION
Add-BCLogEntry
needs the name of the logfile and details of the log entry.
It creates a log object and adds it to the file.
The entry will have a
DateTime stamp added to the entry.
.
PARAMETER
logFile
The utility requires the name of the log file to be used.
It uses the
Get-BCLog
utility to retrieve or create the log file.
.
PARAMETER
level
The level parameter is used to identify the level of the message.
The acceptable range for
this parameter is 0
-
4.
The
Get-BCLogLevelMessage
cmdlet can convert the number to a text message.
The intention of the level is to indicate the severity of the message:
0 Information
1 Warning
2 Error
3 Critical
4 Fatal
.
PARAMETER
source
The source parameter is used to identify the application that is making the log
entry.
.
PARAMETER
category
The category parameter is used to provide a categorisation for the log entry.
.
PARAMETER
message
The message to be recorded.
.
EXAMPLE
Add-BCLogEntry
c:\data\MyLog.
xml
1 InstCal SendEmail
"Email sent to fbloggs"
#>
function
Add-BCLogEntry
{
PARAM
(
[parameter(Mandatory
=
$true
)]
[
String
]
$logFile
,
[parameter(Mandatory
=
$true
)]
[
int
]
$level
,
[parameter(Mandatory
=
$true
)]
[
String
]
$source
,
[parameter(Mandatory
=
$true
)]
[
String
]
$taskCategory
,
[parameter(Mandatory
=
$true
)]
[
String
]
$message
)
$LogEntry
=
New-Object
PSObject
-Property
@{
TimeStamp
=
""
Level
=
0
Source
=
""
TaskCategory
=
""
Message
=
""
}
$LogEntry
.
TimeStamp
=
Get-Date
$LogEntry
.
Level
=
$level
$LogEntry
.
Source
=
$source
$LogEntry
.
TaskCategory
=
$taskCategory
$LogEntry
.
Message
=
$message
$log
=
@(
$LogEntry
)
if
(
Test-Path
-Path
$logFile
)
{
$log
+=
Get-BCLog
$logFile
}
$log
|
Export-Clixml
-path
$logFile
-Depth
5
-Force
}
function
Get-BCLogLevelMessage
{
Param
(
[parameter(Mandatory
=
$true
)]
[ValidateRange(0,4)]
[
int
]
$logLevel
)
$levels
=
@(“Information”, “Warning”, “Error”,
"Critical"
,
"Fatal"
)
return
$levels
[
$logLevel
]
}
function
Get-BCLogLevelFromMessage
{
Param
(
[parameter(Mandatory
=
$true
)]
[
string
]
$logLevelMessage
)
$levels
=
@(“Information”, “Warning”, “Error”,
"Critical"
,
"Fatal"
)
$index
=
-
1
for (
$i
=
0;
$i
-lt
$levels
.
Count
;
$i
++
)
{
if
(
$levels
[
$i
]
-eq
$logLevelMessage
)
{
$index
=
$i
}
}
if
(
$index
-ge
0)
{
return
$index
}
else
{
throw
"$logLevelMessage is not a valid message."
}
}
Loading...
PowerShell...
GPRS Online...
Script Template...
GET Remote...
GPRS Online...
GPRS Online...
View all files
Copyright 2011 PowerShell.com. All rights reserved.