Public/ImportExport/Export-HardwareCsv.ps1

function Export-HardwareCsv {
    <#
    .SYNOPSIS
        Exports basic hardware information from a Milestone VMS to a CSV file
 
    .DESCRIPTION
        Exports hardware information to a CSV file. The following fields are included by default:
 
        - HardwareName
        - HardwareAddress
        - MacAddress
        - UserName
        - Password
        - DriverNumber
        - DriverDisplayName
        - RecordingServerName
        - RecordingServerId
 
        Exporting with -Full will also retrieve all hardware information available through
        the Configuration API and save a JSON object for each hardware in a file adjacent to the CSV.
 
        The name of the *.JSON files will be the name of the CSV, plus the hardware ID, and a column
        named "ConfigurationId" will be added to the CSV as a reference to the configuration corresponding
        to each row in the CSV.
 
    .PARAMETER InputObject
        Array of Hardware objects to be exported to CSV
 
    .PARAMETER Path
        Full path, including file name, where the CSV will be saved
 
    .PARAMETER Full
        Export full hardware configuration data into an adjacent JSON file
 
    .EXAMPLE
        Connect-ManagementServer -Server localhost
        Get-Hardware | Export-HardwareToCsv -Path C:\hardware.csv
 
        Logs into the local Management Server as the current Windows user and exports all hardware information to C:\hardware.csv
 
    .EXAMPLE
        Connect-ManagementServer -Server localhost
        Get-RecordingServer -Name East* | Get-Hardware | Export-HardwareToCsv -Path C:\hardware.csv -Full
 
        Logs into the local Management Server as the current Windows user and exports all hardware information from all Recording
        Servers with names beginning with 'East', to C:\hardware.csv, and includes full hardware configuration details which will
        be saved to C:\hardware_*.json
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory, ValueFromPipeline)]
        [VideoOS.Platform.ConfigurationItems.Hardware[]]
        $InputObject,
        [Parameter(Mandatory, Position = 1)]
        [string]
        $Path,
        [Parameter()]
        [switch]
        $Full
    )

    begin {
        $exportDirectory = Split-Path -Path $Path -Parent
        if (!(Test-Path $exportDirectory)) {
            $null = New-Item -Path $exportDirectory -ItemType Directory
        }
        $recorderMap = @{}
        Write-Verbose "Caching Recording Server names and IDs"
        foreach ($recorder in Get-ConfigurationItem -Path /RecordingServerFolder -ChildItems) {
            $id = $recorder.Properties | Where-Object { $_.Key -eq "Id" } | Select-Object -ExpandProperty Value -First 1
            $name = $recorder.Properties | Where-Object { $_.Key -eq "Name" } | Select-Object -ExpandProperty Value -First 1
            $recorderMap.Add($recorder.Path, [pscustomobject]@{Id=$id; Name=$name})
        }

        $rows = New-Object System.Collections.ArrayList
    }

    process {
        foreach ($hardware in $InputObject) {
            Write-Verbose "Retrieving info for $($hardware.Name)"
            try {
                $hardwareSettings = $hardware | Get-HardwareSetting -ErrorAction Ignore
                $mac = if ($hardwareSettings) { $hardwareSettings.MacAddress } else { 'error' }
                $driver = $hardware | Get-HardwareDriver
                $row = New-Object System.Management.Automation.PSObject
                $row | Add-Member -MemberType NoteProperty -Name HardwareName -Value $hardware.Name
                $row | Add-Member -MemberType NoteProperty -Name HardwareAddress -Value $hardware.Address
                $row | Add-Member -MemberType NoteProperty -Name MacAddress -Value $mac
                $row | Add-Member -MemberType NoteProperty -Name UserName -Value $hardware.UserName
                $row | Add-Member -MemberType NoteProperty -Name Password -Value ($hardware | Get-HardwarePassword)
                $row | Add-Member -MemberType NoteProperty -Name DriverNumber -Value $driver.Number
                $row | Add-Member -MemberType NoteProperty -Name DriverDisplayName -Value $driver.DisplayName
                $row | Add-Member -MemberType NoteProperty -Name RecordingServerName -Value $recorderMap[$hardware.ParentItemPath].Name
                $row | Add-Member -MemberType NoteProperty -Name RecordingServerId -Value $recorderMap[$hardware.ParentItemPath].Id

                if ($Full) {
                    $row | Add-Member -MemberType NoteProperty -Name ConfigurationId -Value $hardware.Id
                    $content = $hardware | Get-ConfigurationItem -Recurse -Sort | ConvertTo-Json -Depth 100 -Compress
                    $configPath = Join-Path -Path $exportDirectory -ChildPath "$([System.IO.Path]::GetFileNameWithoutExtension($Path))_$($hardware.Id).json"
                    $content | Set-Content $configPath -Force
                }
                $null = $rows.Add($row)
            } catch {
                Write-Error "Failed to retrieve info for $($hardware.Name). Error: $_"
            }
        }
    }

    end {
        $rows | Export-Csv -Path $Path -NoTypeInformation
    }
}