Hector Marcia
Hector Marcia

Reputation: 103

Append Powershell Variable result to Event Log

I came across this very useful and simple powershell script to write an event log. My issue is that I want to write the result as the event message. I get something that looks similar to the $result variable but not exactly as printed on the screen. Can someone point me to the correct direction?

begin {
    $EventHashInformation = @{
        LogName   = 'Application'
        Source    = 'My Script'
        EventId   = 2783
        EntryType = 'Information'
    }
    $EventHashWarning     = @{
        LogName   = 'Application'
        Source    = 'My Script'
        EventId   = 2784
        EntryType = 'Warning'   
    }
  Write-EventLog @EventHashInformation -Message 'Script started'
}

process {
    try {
        $result = Get-CimInstance -ClassName Win32_Bios -ErrorAction Stop
        Write-EventLog @EventHashInformation -Message $result 
    } catch {
        Write-EventLog @EventHashWarning -Message 'Error occurred while  queried Win32_Bios'
    }
}

end {
    Write-EventLog @EventHashInformation -Message 'Script finished'
}

This is what I get in the event log

Win32_BIOS: BIOS Date: 05/23/12 17:15:53  Ver: 09.00.06 (Name = "BIOS Date: 05/23/12 17:15:53  Ver: 09.0..., SoftwareElementID = "BIOS Date: 05/23/12 17:15:53  Ver: 09.0..., SoftwareElementState = 3, TargetOperatingSystem = 0, Version = "VRTUAL - 5001223") : %2

This is what I need (screen print in powershell)

SMBIOSBIOSVersion : 60KT38AUS
Manufacturer      : LENOVO
Name              : Lenovo ThinkStation BIOS Ver 60KT38.0
SerialNumber      : MJWNY54
Version           : LENOVO - 60400d0

Upvotes: 0

Views: 1489

Answers (1)

Vincent K
Vincent K

Reputation: 1346

$result = (Get-CimInstance -ClassName Win32_Bios -ErrorAction Stop | Out-String).Trim()

Convert the output to string

Upvotes: 1

Related Questions