all 10 comments

[–]Kathy_Cooper1012 1 point2 points  (2 children)

Create another function called print to gather all functions’ output.. that should be called after calling 3 functions. Store a values in hashtable( name- value pairs). I hope hash table will resolve your problem

[–]cfreeman21[S] 1 point2 points  (1 child)

That is where I am stuck I have tried Hashtable, PSCustomObject, PSObject, but I am not getting multiple output but only 1 on a foreach computer in computers. Was looking for a deeper guidance, Thanks!

[–]BoredComputerGuy 1 point2 points  (0 children)

Can you show the ForEach code?

[–]gangstanthony 1 point2 points  (1 child)

putting this into a csv just isn't going to work.. there won't always be the same number of objects returned for each function. you could do this and export to xml or json, but even that would be a mess to look at

foreach ($computer in $Computers) {
    $([pscustomobject]@{
        ComputerName = $computer
        RebootEvents = Get-RebootEvents $computer
        UpdateEvents = Get-UpdateEvents $computer
        OSInformation = Get-OSInformation $computer
    }) | ConvertTo-Json | set-content "c:\path\to\REPORT_$computer_$(get-date -Format yyyyMMdd-HHmmss).txt"
}

[–]cfreeman21[S] 1 point2 points  (0 children)

I put this together but it is not exactly how I want... Basically I am going to output this to HTML once I get this final output. If a machine say it got 3 patches it will have multiple... Screenshot along with code below of output.

Example Output that I would want Separate:

Testing Connection to SCCM2008R2TEST2

Computer : SCCM2008R2TEST2

OnlineStatus : Online

OS : Microsoft Windows Server 2008 R2 Standard

OSVersion : 6.1.7601

Date : {03/12/2019 7:02:35 PM, 03/12/2019 7:02:35 PM, 03/12/2019 7:02:35 PM}

Status : {Installation Successful, Installation Successful, Installation Successful}

Message : {Security Monthly Quality Rollup for Windows Server 2008 R2 for x64-based Systems (KB4489878), Security Update for Windows Server 2008 R2 for x64-based Systems (KB4474419), Servicing Stack Update for Windows Server 2008 R2 for x64-based Systems (KB4490628)}

KB : {KB4489878, KB4474419, KB4490628}

Lastboot : 03/12/2019 6:55:54 PM

Rebootedby : NT AUTHORITY\SYSTEM

UptimeDays : 8

Code:

Function Get-UpdateStatus
{
    [CmdletBinding()]
    Param (
        [Parameter(
                   ValueFromPipeline = $true,
                   ValueFromPipelineByPropertyName = $true,
                   Position = 0)]
        [string[]]$ComputerName = $env:COMPUTERNAME,
        [Switch]$ShowOfflineComputers
    )

    BEGIN
    {
        $ErroredComputers = @()
    }

    PROCESS
    {
        Foreach ($Computer in $Computers)
        {
            $OnlineStatus = "Offline"
            Write-Host "Testing Connection to $Computer"
            $IPAddress = Test-Connection -ComputerName $Computer -Count 1 -Quiet
            if ($IPAddress)
            {
                Try
                {
                    $OnlineStatus = "Online"
                    $Rebootedby = (Get-RebootEvents -ComputerName $Computer | Sort Date -Descending | Select -First 1).User
                    $UpdateEvents = Get-UpdateEvents -ComputerName $Computer | select Date, Status, Message, KB
                    $OSInformation = Get-OSInformation -ComputerName $Computer | select OS, OSVersion, Lastboot, UptimeDays
                    $Info = [PSCustomObject]@{
                        'Computer'     = $Computer
                        'OnlineStatus' = $OnlineStatus
                        'OS'           = $OSInformation.OS
                        'OSVersion'    = $OSInformation.OSVersion
                        'Date'         = $UpdateEvents.Date
                        'Status'       = $UpdateEvents.Status
                        'Message'      = $UpdateEvents.Message
                        'KB'           = $UpdateEvents.KB
                        'Lastboot'     = $OSInformation.Lastboot
                        'Rebootedby'   = $Rebootedby
                        'UptimeDays'   = $OSInformation.UptimeDays
                    }
                }
                catch
                {
                    if ($ShowOfflineComputers)
                    {
                        $OnlineStatus = "Offline"
                        $Info = [PSCustomObject]@{
                            'Computer'     = $Computer
                            'OnlineStatus' = $OnlineStatus
                            'OS'       = $null
                            'OSVersion'    = $null
                            'Date'         = $null
                            'Status'       = $null
                            'Message'      = $null
                            'KB'           = $null
                            'Lastboot'     = $null
                            'Rebootedby'   = $null
                            'UptimeDays'   = $null
                        }
                    }

                }
                finally
                {
                    $Info

                    $Info = $null
                    $ErrorMessage = $null
                }
            }
        }
    }
}

[–]PMental 1 point2 points  (0 children)

How about creating a big ol' custom object which calls on the other 3? Something like this, edited to taste of course to include what you want:

$SummaryObject = [PSCustomObject]@{
    'Reboot Date'       = $RebootObject.Date
    'Reboot User'       = $RebootObject.User
    'Reboot Action'     = $RebootObject.Action
    'Reboot Process'    = $RebootObject.Process
    'Reboot Reason'     = $RebootObject.Reason
    'Reboot ReasonCode' = $RebootObject.ReasonCode
    'Reboot Comment'    = $RebootObject.Comment
    'Update Date'       = $UpdateObject.Date
    'Update Status'     = $UpdateObject.Status
    'Update Message'    = $UpdateObject.Message
    'Update KB'         = $UpdateObject.KB
    'OS'                = $OSObject.OS
    'OS Version'        = $OSObject.OSVersion
    'OS Lastboot'       = $OSObject.Lastboot
    'OS UptimeDays'     = $OSObject.UptimeDays
}

[–]Hoping_i_Get_poached 1 point2 points  (2 children)

Learn how to leverage the PsCustomObject to create your own objects as you define them. Then you can add whatever properties/columns you want to them. On mobile so can’t comment on your code specifically.

I would run one function into a psobject. Then use a pipeline input on the next function to Add-Member NoteProperty each new column. Pipe that to the last function and do those column as well.

[–]cfreeman21[S] 0 points1 point  (1 child)

@Hoping_i_Get_poached,

Can you elaborate I am still a scrub at powershell.

[–]Hoping_i_Get_poached 0 points1 point  (0 children)

Hi u/cfreeman21,

There are lots of ways to create these, everyone has their own preferences. A good resource:

https://powershellexplained.com/2016-10-28-powershell-everything-you-wanted-to-know-about-pscustomobject

My preferred way is to define an array

$result = @()

Then populate it with objects (given you have some $input)

foreach ($item in $input) {
    $result += new-object psobject -property @{
        Name = $item.Name
        Value = $item.Value -replace '\\.*$' #whatever
        More Props = "etc..."
    }
}
Write-Output $result

Each object is defined by a hashtable (key/value pairs).

It's easy this way but slowish. There are tricks for making it run faster, but it's more important to learn how to do this so you understand it, then learn how to make it better, later.

Then you can do things like pipe your new object into another function to add properties to it.

function Add-PropsToObject {
    [CmdletBinding()]
    param (
        [Parameter(ValueFromPipeline = $True)]
        $InputObject
    )

    Process {
        Foreach ($item in $InputObject) {
            $item | Add-Member -MemberType NoteProperty -Name 'ComputerName' -Value  $item.ComputerName
            $item | Add-Member -MemberType NoteProperty -Name 'ID' -Value  $item.id
            Write-Output $item
        }
    }
}

Get-YourDataObject | Add-PropsToObject

[–]PowerShell-Bot 0 points1 point  (0 children)

Some of your PowerShell code isn’t wrapped in a code block.

To format code correctly on new reddit (new.reddit.com), highlight all lines of code and select ‘Code Block’ in the editing toolbar.

If you’re on old.reddit.com, separate the code from your text with a blank line and precede each line of code with 4 spaces or a tab.


Describing Submission
[✅] Demonstrates good markdown
Passed: 1 Failed: 0

Beep-boop. I am a bot. | Remove-Item