Hi,
I have a requirement to build a custom event log file that has two sources.
In Powershell, I do this:
New-EventLog -LogName GFWM_LOG -Source DBLogger
Limit-EventLog -OverflowAction OverWriteAsNeeded -MaximumSize 20480KB -LogName GFWM_LOG
New-EventLog -LogName GFWM_LOG -Source LogginService
However, in DSC if I try to reference the same log file it complains about duplicates and errors.
I have looked at the examples and the help file, but they don’t address the issue of multiple sources.
As I look further into my project I see some log file with as many as five sources.
Here is how I build the log file in my DSC script:
cCreateLogFile GFWMF-DBLogger-EventLogs {
Name = ‘GFWM_LOG’
Source = ‘DBLogger’
Limit = 20480KB
Id = 411
Ensure = ‘Present’
}
And the module cCreateLogFile looks like this: (I have xx out the company name)
function Get-TargetResource
{
[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Name
)
$Configuration = @{
Name = $Name
Source = $Source
limit = $limit
Id = $Id
}
Try{
write-verbose “Get: Getting current Logfile $name status”
$Status = [System.Diagnostics.EventLog]::Exists($Name)
If ($Status -eq $true) {
write-verbose "Test: Logfile $name Present"
$Configuration.Add('Ensure','Present')
} else {
write-verbose 'Test: Logfile $name Not present'
$Configuration.Add('Ensure','Absent')
}
return $Configuration
}
Catch{
$exception = $_
Write-Verbose (“An Error Occurred: $exception.message”)
}
}
function Set-TargetResource
{
[CmdletBinding()]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Name,
[System.String]
$Source,
[System.UInt32]
$limit,
[System.UInt32]
$Id,
[ValidateSet('Present','Absent')]
[System.String]
$Ensure
)
try {
if ($Ensure -like ‘Present’)
{
write-verbose “Set: Creating Logfile $Name $Source”
new-eventlog -logname $Name -Source $Source
limit-eventLog -logname $Name -MaximumSize $limit -overflowaction OverwriteOlder
write-eventlog -logname $Name -Source $Source -Message ‘Eventlog created by DSC Configuration=EWM-WebRole,`
Module=xxxxxxxxxx -Resource=XX_CreateLogFile’ -id $Id
}
else
{
write-verbose “Set: Removing Logfile $Name”
Remove-EventLog -LogName $name
}
}
Catch{
$exception = $_
Write-Verbose (“An Error Occurred: $exception.message”)
}
}
function Test-TargetResource
{
[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[parameter(Mandatory = $true)]
[System.String]
$Name,
[System.String]
$Source,
[System.UInt32]
$limit,
[System.UInt32]
$Id,
[ValidateSet('Present','Absent')]
[System.String]
$Ensure
)
try {
write-verbose “Test: Getting current Logfile $name status”
$Status = [System.Diagnostics.EventLog]::Exists($Name)
if ($Ensure -like 'Present')
{
if (($Status -eq $true))
{
return $true
}
else
{
return $false
}
}
else
{
if ($Status -eq $true)
{
return $false
}
else
{
return $true
}
}
}
Catch {
$exception = $_
Write-Verbose (“An Error Occurred: $exception.message”)
}
}
Export-ModuleMember -Function *-TargetResource