Hey all,
Im trying to out results of a scheduled task to a file but I also need to include a column for LastBootUpTime (in days) that doesn’t exist as property of the task. Ive added the cmdlet to the if statement and it returns the results I’m looking for but its in a 2nd table and selecting or formatting is just adding it as its own separate line. I have all of these values in a variable called $NeedsRestart and the output looks like below.
PSComputerName Running(Days) TaskName State
ADS-5435017-02.Abacus.local AutoReboot Ready
ADS-5435017-02.Abacus.local 41
Id like for each server to only have one line of data but Im not sure how to effectively merge the two. Im still really new to PS so I havent had multiple commands within a single if statement before but it seems like it shouldnt be an issue since its returning the right results. Does anyone know from a formatting perspective how I can get this working right? Script is below
$NeedsRestart = Invoke-Command -ComputerName $Sample -ScriptBlock { $LastReboot = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime $ActiveUser = Quser If ($LastReboot -le (Get-Date).AddDays(-30) -and $ActiveUser) { [Int]$WaitSeconds = (([DateTime]'1AM').AddHours(24) - (Get-Date)).TotalSeconds $Action = New-ScheduledTaskAction -Execute ‘%ComSpec%’ -Argument ‘/c C:\Windows\System32\Shutdown.exe /r /t 300 /c /f && C:\Windows\System32\Schtasks.exe /delete /TN AutoReboot /f’ $Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds($WaitSeconds) $Principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest $Settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel Register-ScheduledTask -TaskName AutoReboot -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings [PSCustomObject]@{ 'Running(Days)' = [Int]((Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime).TotalDays} } # If Up time > 30 days and user NOT found schedule reboot task for 1 minute from current time ElseIf ($LastReboot -le (Get-Date).AddDays(-30) -and $ActiveUser -eq $Null) { $Action = New-ScheduledTaskAction -Execute ‘%ComSpec%’ -Argument ‘/c C:\Windows\System32\Shutdown.exe /r /t 0 /c /f && C:\Windows\System32\Schtasks.exe /delete /TN AutoReboot /f’ $Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1) $Principal = New-ScheduledTaskPrincipal -UserID "NT AUTHORITY\SYSTEM" -LogonType ServiceAccount -RunLevel Highest $Settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel Register-ScheduledTask -TaskName AutoReboot -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings [PSCustomObject]@{ 'Running(Days)' = [Int]((Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime).TotalDays} } # If Up time < 30 days ignore Else { Out-Null } } Write-Host 'Saving results to your Desktop...' # Output results to local user Desktop If ($NeedsRestart -eq $Null) { "No Results Found" | Out-File "$env:USERPROFILE\Desktop\APC Server Up Time Report $(Get-Date -Format M-dd-yyyy).txt" } Else {$NeedsRestart | Ft PSComputerName,'Running(Days)',TaskName,State -AutoSize | Out-File "$env:USERPROFILE\Desktop\APC Server Up Time Report $(Get-Date -Format M-dd-yyyy).txt" }