Jeffrey Renfroe
asked on
Question about adding to output with PowerShell
Hello. I created a PowerShell script that searches the event viewer for Service Pack information in the event viewer. It reads from a text file and outputs what it finds. I am still learning my way around PowerShell. I want to add the name of each computer before it searches the next to my output.
For example, the output currently looks like the below.
Error Microsoft-Windows-Service Pack Installer 8 Service Pack installation failed with error code 0x800f0828.
Error Microsoft-Windows-Service Pack Installer 5 There is not enough free disk space to install the Service Pack. Required=4623 MB.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Error Microsoft-Windows-Service Pack Installer 8 Service Pack installation failed with error code 0x800f0828.
Information Microsoft-Windows-Service Pack Installer 9 Service Pack 1 installation succeeded.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
I would like it to look like the below. I assume I could do something with $Computer but I am not sure how to do so.
Computer 1
Error Microsoft-Windows-Service Pack Installer 8 Service Pack installation failed with error code 0x800f0828.
Error Microsoft-Windows-Service Pack Installer 5 There is not enough free disk space to install the Service Pack. Required=4623 MB.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Error Microsoft-Windows-Service Pack Installer 8 Service Pack installation failed with error code 0x800f0828.
Computer 2
Information Microsoft-Windows-Service Pack Installer 9 Service Pack 1 installation succeeded.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
I know it is something simple that I am missing but I cannot see to find it. Any help is appreciated.
$Computer = Get-Content c:\temp\machines.txt
If (Test-Connection -comp $computer -count 1 -quiet) {
"Searching Event Viewer"
Get-EventLog System -ComputerName $Computer -Newest 4 -Source '*Service Pack*' | Format-Table Time, EntryType, Source, InstanceID, Message -AutoSize | Out-File C:\temp\ServicePack.log
}
Else {
"Machine is offline."
Exit
}
For example, the output currently looks like the below.
Error Microsoft-Windows-Service Pack Installer 8 Service Pack installation failed with error code 0x800f0828.
Error Microsoft-Windows-Service Pack Installer 5 There is not enough free disk space to install the Service Pack. Required=4623 MB.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Error Microsoft-Windows-Service Pack Installer 8 Service Pack installation failed with error code 0x800f0828.
Information Microsoft-Windows-Service Pack Installer 9 Service Pack 1 installation succeeded.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
I would like it to look like the below. I assume I could do something with $Computer but I am not sure how to do so.
Computer 1
Error Microsoft-Windows-Service Pack Installer 8 Service Pack installation failed with error code 0x800f0828.
Error Microsoft-Windows-Service Pack Installer 5 There is not enough free disk space to install the Service Pack. Required=4623 MB.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Error Microsoft-Windows-Service Pack Installer 8 Service Pack installation failed with error code 0x800f0828.
Computer 2
Information Microsoft-Windows-Service Pack Installer 9 Service Pack 1 installation succeeded.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
Information Microsoft-Windows-Service Pack Installer 1 Service Pack 1 installation started.
I know it is something simple that I am missing but I cannot see to find it. Any help is appreciated.
$Computer = Get-Content c:\temp\machines.txt
If (Test-Connection -comp $computer -count 1 -quiet) {
"Searching Event Viewer"
Get-EventLog System -ComputerName $Computer -Newest 4 -Source '*Service Pack*' | Format-Table Time, EntryType, Source, InstanceID, Message -AutoSize | Out-File C:\temp\ServicePack.log
}
Else {
"Machine is offline."
Exit
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the quick response.
ASKER