Public/Get-PdqDeployments.ps1

<#
.SYNOPSIS
Outputs all deployments from the Deploy database.
 
.INPUTS
None.
 
.OUTPUTS
System.Management.Automation.PSCustomObject
System.Object[]
 
.EXAMPLE
Get-PdqDeployments | Where-Object 'Status' -eq 'Running' | Select-Object 'DeploymentId', 'PackageName'
Returns the Deployment ID and Package Name for all running deployments.
 
.EXAMPLE
(Get-PdqDeployments -IncludeComputers | Where-Object 'DeploymentId' -eq 12).Computers.Name
Returns the names of the computers in Deployment 12.
#>


function Get-PdqDeployments {

    [CmdletBinding()]
    param (
        # Add the Computers property.
        # The objects in this array come from the DeploymentComputers table.
        [Switch]$IncludeComputers,

        # Add the Steps property to each object in the Computers property.
        # The objects in this array come from the DeploymentComputerSteps table.
        [Switch]$IncludeSteps,

        # The path to the currently active database will be retrieved by default.
        # You can use this parameter if you wish to run this function against a different database.
        [String]$DatabasePath
    )

    try {

        $CloseConnection = Open-PdqSqlConnection -Product 'Deploy' -DatabasePath $DatabasePath

        # Get all deployments.
        Invoke-SqlQuery -Query 'SELECT * FROM Deployments;' -ConnectionName 'Deploy' -Stream | ForEach-Object {

            $Deployment = $_

            if ( $IncludeComputers ) {

                $Computers = New-Object System.Collections.Generic.List[Object]

                # Get all computers in the deployment.
                $ComputersQuery = "SELECT * FROM DeploymentComputers WHERE DeploymentId = $($Deployment.DeploymentId);"
                Invoke-SqlQuery -Query $ComputersQuery -ConnectionName 'Deploy' -Stream | ForEach-Object {

                    $Computer = $_

                    if ( $IncludeSteps ) {

                        # Get every step of this computer's deployment.
                        $StepsQuery = "SELECT * FROM DeploymentComputerSteps WHERE DeploymentComputerId = $($Computer.DeploymentComputerId);"
                        $Steps = Invoke-SqlQuery -Query $StepsQuery -ConnectionName 'Deploy' -Stream

                        # Add the steps to the $Computer object.
                        $Computer | Add-Member -MemberType 'NoteProperty' -Name 'Steps' -Value $Steps

                    }

                    # Add this computer to the list of computers, so they can be added to the deployment.
                    $Computers.Add($Computer)

                }

                # Add the computers to the $Deployment object.
                # .ToArray() converts the List into a standard System.Object[].
                # I did this so the data type matches $Steps. It's probably unnecessary, but might prevent unexpected behavior.
                $Deployment | Add-Member -MemberType 'NoteProperty' -Name 'Computers' -Value $Computers.ToArray() -PassThru

            } else {

                $Deployment

            }

        }

    } finally {

        Close-PdqSqlConnection -Product 'Deploy' -CloseConnection $CloseConnection

    }

}