Welcome › Forums › General PowerShell Q&A › Writing Powershell script output to EventData in Event log
- This topic has 4 replies, 3 voices, and was last updated 3 weeks, 5 days ago by
Participant.
Viewing 3 reply threads
-
AuthorPosts
-
-
December 22, 2020 at 2:21 pm #281915
Hi all. I’m fairly new to Powershell scripting, and became stumped on this task based on experience & research. Any help would be greatly appreciated! Please see below.
Objective: Output the results of Name, OperatingSystem, OperatingSystemVersion, lastlogondate to an event log. Currently and reasonably, it’s only outputting the message “This is a test” below in the event log. Any suggestions of how to output the results of the script itself to the event log it generates?
`
PowerShell12345cd c:\scriptsGet-ADComputer -Filter * -Property * | Select-Object -Property Name,OperatingSystem,OperatingSystemVersion,lastlogondateWrite-EventLog -LogName Application -EventID 3000 -EntryType Information -Source ‘AD PC OS Information’ -Message ‘This is a test’ -
December 22, 2020 at 5:21 pm #281960PowerShell1234567891011121314151617181920cd c:\scripts$props = 'Name','OperatingSystem','OperatingSystemVersion','LastLogonDate'$Info = Get-ADComputer -Filter * -Property $props | Select-Object -Property $props$template = @"{0} : {1}{2} : {3}{4} : {5}{6} : {7}"@$info | ForEach-Object {$str = $template -f $($_.psobject.Properties | ForEach-Object {$_.name,$_.value})Write-EventLog -LogName Application -EventID 3000 -EntryType Information -Source ‘AD PC OS Information’ -Message $str}
-
December 23, 2020 at 7:54 am #282065
This is perfect, thanks Doug!
-
-
December 23, 2020 at 9:38 am #282074
@Doug Nice script!
-
December 23, 2020 at 11:31 am #282095
Lol thanks @Rob it was the only way I could make it look nice with varying value lengths
-
-
AuthorPosts
Viewing 3 reply threads
- You must be logged in to reply to this topic.