Author Posts

December 30, 2017 at 3:10 am

I'm trying to add a value that is stored as a variable to the -Message parameter when using Write-EventLog. I'm not quite understanding my the variable isn't displaying when the message is written. This variable does contain a value and displays fine when using Write-Host. Am I missing a silly formatting rule here?

December 30, 2017 at 4:30 am

Based on what you posted, this code should error out. As your final If statement denotes errors.

The PoSH ISE pops up errors

Missing '= ' operator after key in hash literal
The hash literal was incomplete.
Duplicate Keys 'if' not allowed in hash literals

Both the Write-EventLog cmdlets in that if statement, are reflected as non-functional. Meaning the PoSH syntax color codeing shows they are in error.

If you look at you code, you do not close the first hashtable block.
This ...

$MyEventWarn = @{
    LogName='Application';
    Source='QAS';
    EventID='56789';
    EntryType='Warning';
    Message="QAS data will expire soon and requires an update. Days remaining:$num" 
    #This doesn't write the value
$MyEventInfo = @{
    LogName='Application';
    Source='QAS';
    EventID='56788';
    EntryType='Information';
    Message="QAS data is current. No remediation required at this time. Days remaining:$num" 
    #This doesn't write the value
}

Vs this..

$MyEventWarn = @{
    LogName='Application';
    Source='QAS';
    EventID='56789';
    EntryType='Warning';
    Message="QAS data will expire soon and requires an update. Days remaining:$num" 
    }

$MyEventInfo = @{
    LogName='Application';
    Source='QAS';
    EventID='56788';
    EntryType='Information';
    Message="QAS data is current. No remediation required at this time. Days remaining:$num" 
}


December 30, 2017 at 2:01 pm

There's a copy paste error there. The source has the correct syntax. Even when I view this in VScode, using the normal variable syntax doesn't seem to work when placed in the Message array entry. I'm stumped.

December 30, 2017 at 3:33 pm

Am I wrong or do you use the variable before filling it? This way it should work actually:

# Variable containing extracted data for analysis
$data = Select-String -Pattern "Data Expiry" -Path $LogPath\$Log | ForEach-Object {$_.Line}

# Create substring data by trimming the excess info. We just need the number of days.
$start = $data.IndexOf(':') + 1
$end   = $data.IndexOf("days")
$length = $end - $start
[int]$num = $data.Substring($start,$length).TrimStart(" ")

# Splatting technique used here to pre-define the events that will be written to the systems Application Log
$MyEventWarn = @{
    LogName='Application';
    Source='QAS';
    EventID='56789';
    EntryType='Warning';
    Message="QAS data will expire soon and requires an update. Days remaining: $num" #This doesn't write the value
}
$MyEventInfo = @{
    LogName='Application';
    Source='QAS';
    EventID='56788';
    EntryType='Information';
    Message="QAS data is current. No remediation required at this time. Days remaining: $num" #This doesn't write the value
}

# Once the value is known, we have to decide which message to write to the event log so that System Center
# can pick up the entry and perform remediation tasks.
if ($num -le 10) {
    Write-EventLog @MyEventWarn
}
else {
    Write-EventLog @MyEventInfo
}

December 30, 2017 at 7:20 pm

I don't use the variable anywhere else other than the splatting arrays but if this is just a matter of where I have declared my splatting array, I will feel pretty silly.
I typically put all that type of info (splats, static variables) at the very top of my script so it makes for easier editing by others on my team. I'll re-arrange the splatting and re-test.

[UPDATE]
Yeah now I feel silly. Took your suggestion and moved the splatting arrays to just after when $num is declared and it worked. Thank you!