Search code examples
powershellevent-log

Powershell Write-Eventlog Linebreak


I'm making a script which searches all Computer with specific Operating systems, run them through a Whitelist and, if there are Computer that are not on the Whitelist, I write an errorlog. The log looks like this right now:

@{Name=Computername1; Operatingsystem=Windows 10 Enterprise; DistinguishedName=CN=la,OU=computers,OU=lu,OU=Hosting,DC=a,DC=b,DC=ch; OperatingSystemVersion=10.0 (10586)}

But it should look like this:

Name=Computername1
Operatingsystem=Windows 10 Enterprise
DistinguishedName=CN=la,OU=computers,OU=lu,OU=Hosting,DC=a,DC=b,DC=ch
OperatingSystemVersion=10.0 (10586)

I want to delete the @{} and the 4 informations should be separated by line breaks.

My code:

$username = $env:UserName

$getad = Get-ADComputer -Filter {(operatingsystem -like "*Windows 10*" -and OperatingSystemVersion -notlike "*16299*" -and OperatingSystemVersion -notlike "*14393*" -and OperatingSystemVersion -notlike "*14279*" -and OperatingSystemVersion -notlike "*15063*" -and OperatingSystemVersion -notlike "*10159*" -and OperatingSystemVersion -notlike "*16193*" -and OperatingSystemVersion -notlike "*17025*" -and OperatingSystemVersion -notlike "*10074*" -and OperatingSystem -notlike "*LTSB") -or (operatingsystem -like "*Windows Vista*") -or (operatingsystem -like "*Windows XP*") -or  (operatingsystem -like "*95*") -or (operatingsystem -like "*94*") -or ( operatingsystem -like "*Windows 8*" -and OperatingSystemVersion -notlike "*9600*" -and OperatingSystem -notlike "*LTSB") -or (operatingsystem -like "*2000 Professional*") -or (operatingsystem -like "*2000 Server*") -or (operatingsystem -like "*2003*") -or (operatingsystem -like "*Windows NT*") -or (operatingsystem -like "*Windows 7*" -and OperatingSystemVersion -notlike "*7601*" -and OperatingSystem -notlike "*LTSB")} -Properties ('Name', 'operatingsystem', 'DistinguishedName', 'OperatingsystemVersion') | ? {$_.distinguishedname -notlike "*OU=Oldwin10-Test,OU=a,OU=b,OU=c,OU=d,DC=e,DC=f,DC=ch"} 

$whitelisted = Get-Content "C:\Users\$username\Desktop\whitelistedpcs.txt"

$getad | Select-Object Name, Operatingsystem, DistinguishedName, 
OperatingSystemVersion | ForEach-Object {

   if ($whitelisted -match $_.DistinguishedName) {

   }

      else{
        Write-EventLog -LogName Application -Source "OldWinalert" -EntryType Error -EventId 1 -Message "$_"
      }
}   

Solution

  • You need to convert the output to string before sending it to the -Message parameter using Out-String will do:

    for your case I would try this:

    [...] -EntryType Error -EventId 1 -Message ($_ | Format-List | Out-String)
    

    Another option:

    else{
    
    $Message = @"
    Name: $($_.name)
    Operating System: $($_.Operatingsystem)
    Distinguished Name: $($_.DistinguishedName)
    Operating System Version: $($_.OperatingSystemVersion)
    
    "@
    
    Write-EventLog -LogName Application -Source "OldWinalert" -EntryType Error -EventId 1 -Message $Message
    }
    

    and if you want extra line space add `n (for new line) on each line end after the brackets