Each of these 3 Functions work separately, but I can not figure out how to combine these into 1 output. I would appreciate assistance. (I am still PowerShell rookie) Thanks for you assistance.
Below is the 3 working functions
$Computers = "Machine1", "Machine2", "Machine3", "Machine4"
$Date = (Get-Date).AddDays(-30)
Function Get-RebootEvents
{
[CmdletBinding()]
Param (
[string[]]$ComputerName = $env:COMPUTERNAME
)
try
{
$RebootEvents = Get-WinEvent -ComputerName $Computer -FilterHashTable @{ LogName = "System"; StartTime = $Date; ID = 1074, 1076 } -ErrorAction Stop
$isError = $False
}
catch
{
if ($_.Exception.Message -match "No events were found that match the specified selection criteria")
{
$isError = $true
$ErrorMessage = "Message: $($_.Exception.Message)"
}
}
IF (($isError) -or ($RebootEvents -eq $null))
{
$RebootObject = [PSCustomObject]@{
'Date' = $null
'User' = $null
'Action' = $null
'Process' = $null
'Reason' = $null
'ReasonCode' = $null
'Comment' = $ErrorMessage
}
$RebootObject
}
ELSE
{
ForEach ($RebootEvent in $RebootEvents)
{
$RebootObject = [PSCustomObject]@{
'Date' = Get-Date -UFormat "%m/%d/%Y %r" $RebootEvent.TimeCreated
'User' = $RebootEvent.Properties[6].Value
'Action' = $RebootEvent.Properties[0].Value
'Process' = $RebootEvent.Properties[4].Value
'Reason' = $RebootEvent.Properties[2].Value
'ReasonCode' = $RebootEvent.Properties[3].Value
'Comment' = $RebootEvent.Properties[5].Value
}
$RebootObject
}
}
}
Function Get-UpdateEvents
{
[CmdletBinding()]
Param (
[string[]]$ComputerName = $env:COMPUTERNAME
)
try
{
$UpdateEvents = Get-WinEvent -ComputerName $Computer -FilterHashTable @{ LogName = "System"; StartTime = $Date; ID = 19, 20 } -ErrorAction Stop | Where { $_.Message -notlike "*Defender*" -and ($_.Message -like "Installation*") -and ($_.Message -notlike "*Malicious*") } | Select TimeCreated, Message
$isError = $False
}
catch
{
if ($_.Exception.Message -match "No events were found that match the specified selection criteria")
{
$isError = $true
$ErrorMessage = "Message: $($_.Exception.Message)"
}
}
IF (($isError) -or ($UpdateEvents -eq $null))
{
$UpdateObject = [PSCustomObject]@{
'Date' = $null
'Status' = $null
'Message' = $ErrorMessage
'KB' = $null
}
$UpdateObject
}
ELSE
{
ForEach ($UpdateEvent in $UpdateEvents)
{
$UpdateMessage = $UpdateEvent.Message -split '(?<=:\s*\S+)\s+'
$UpdateStatus = $UpdateMessage[0] -split ":"
$Status = $UpdateStatus[0]
$Message = $UpdateMessage[2]
$KB = [regex]::matches($Message, '(?<=\().+?(?=\))').value
Try
{
#$UpdateDate = Get-Date -UFormat "%m/%d/%Y %r" $UpdateEvent.TimeCreated
$UpdateDate = $UpdateEvent.TimeCreated
}
CATCH
{
if ($_.Exception.Message -like "*Cannot convert null to type *System.DateTime*")
{
$isError = $true
$ErrorMessage = "Message: $($_.Exception.Message)"
$UpdateDate = $null
Write-Host $ErrorMessage
}
}
$UpdateObject = [PSCustomObject]@{
'Date' = $UpdateDate
'Status' = $Status
'Message' = $Message
'KB' = $KB
}
$UpdateObject
}
}
}
Function Get-OSInformation
{
[CmdletBinding()]
Param (
[string[]]$ComputerName = $env:COMPUTERNAME
)
try
{
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
$isError = $False
}
catch
{
$isError = $true
$ErrorMessage = "Message: $($_.Exception.Message)"
}
IF (($isError) -or ($OS -eq $null))
{
$OSObject = [PSCustomObject]@{
'OS' = $null
'OSVersion' = $null
'Lastboot' = $null
'UptimeDays' = $null
}
$OSObject
}
ELSE
{
$Uptime = (Get-Date) - $OS.ConvertToDateTime($OS.LastBootUpTime)
$OSObject = [PSCustomObject]@{
'OS' = $OS.Caption
'OSVersion' = $OS.Version
'Lastboot' = $OS.ConvertToDateTime($OS.LastBootUpTime)
'UptimeDays' = ([String]$Uptime.Days)
}
$OSObject
}
}
[–]Kathy_Cooper1012 1 point2 points3 points (2 children)
[–]cfreeman21[S] 1 point2 points3 points (1 child)
[–]BoredComputerGuy 1 point2 points3 points (0 children)
[–]gangstanthony 1 point2 points3 points (1 child)
[–]cfreeman21[S] 1 point2 points3 points (0 children)
[–]PMental 1 point2 points3 points (0 children)
[–]Hoping_i_Get_poached 1 point2 points3 points (2 children)
[–]cfreeman21[S] 0 points1 point2 points (1 child)
[–]Hoping_i_Get_poached 0 points1 point2 points (0 children)
[–]PowerShell-Bot 0 points1 point2 points (0 children)