Merging multiple outputs into a single table

Welcome Forums General PowerShell Q&A Merging multiple outputs into a single table

Viewing 2 reply threads
  • Author
    Posts
    • #210189
      Participant
      Topics: 5
      Replies: 14
      Points: 73
      Rank: Member

      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"
          }
              
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    • #210327
      Participant
      Topics: 5
      Replies: 14
      Points: 73
      Rank: Member

      Ok think I got it. This seems to return all results into a single table row the way I like but had to add some additional variables. I’m also curious to test this against multiple servers with both active and non active users to see what the final output will look like. Hopefully it doesnt end up as multiple rows each with their own set of columns.

      Testing against my local machine is returning results like

      https://imgur.com/a/WqBy5Cv

           New Changes:
      
           $ScheduledTask = Register-ScheduledTask -TaskName AutoReboot -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings 
           $Properties = ($ScheduledTask).pscomputername,($ScheduledTask).taskname,($ScheduledTask).state
           $RunningDays = [Int]((Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime).TotalDays
           [PSCustomObject]@{
             PSComputerName = $Properties[0]
             'Running(Days)' = $RunningDays
             TaskName = $Properties[1]
             State = $Properties[2] }
      • This reply was modified 2 months, 2 weeks ago by Vdominguez.
      • This reply was modified 2 months, 2 weeks ago by Vdominguez.
      • This reply was modified 2 months, 2 weeks ago by Vdominguez.
      • This reply was modified 2 months, 2 weeks ago by Vdominguez.
      • This reply was modified 2 months, 2 weeks ago by Vdominguez.
    • #210357
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      Looks like you got it figured out, but since you mentioned “but had to add some additional variables”. I’m adding this comment.  What you have is not wrong in any way and is perfectly fine, but you don’t have to use the additional variable.  You can just add the values to your Customer PowerShell Object without the variables.

      IE

      $ScheduledTask = Register-ScheduledTask -TaskName AutoReboot -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings
      [PSCustomObject]@{
        ComputerName = $ScheduledTask.pscomputername
        'Running(Days)' = [Int]((Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime).TotalDays
        TaskName = $ScheduledTask.taskname
        State = $ScheduledTask.state
      }
Viewing 2 reply threads
  • You must be logged in to reply to this topic.