Scepticalist
Scepticalist

Reputation: 3923

Adding "non-standard" value to iterated ArrayList

I'm currently writing a script to retrieve registry entries from a list of remote servers and export to CSV.

I've got this, which works (I'm goign to clean up the error handling later), but the output isn't quite what I want to produce as it doesn't export nicely to CSV.

$DataList = New-Object System.Collections.ArrayList
foreach ($Target in $ImportData) {
    Write-Host "Scanning" $Target.ComputerName ":" -NoNewline
    $RegData = Get-RegValue -ComputerName $($Target.ComputerName) -Hive $RegHive -Key $KeyName
    if (!$RegData) {
        Write-Host "No data found for" $Target.ComputerName
        [void]$DataList.Add([PSCustomObject]@{
            ComputerName = $Target.ComputerName
            Hive ="No Data"
            Key = ""
            Value = ""
            Data = ""
            Type = ""
        })
    } else {
        $DataList.Add($RegData)
        Write-Host "Done"
    }
}
$DataList

The output is as follows:

ComputerName Hive            Key                  Value                     Data
------------ ----            ---                  -----                     ---
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... AssetNumber               987
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... BuildDate                 04/
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... iLODefaultPwd             NA
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerContactDetails       S G
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerName                 MS
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerPhone                643
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... PatchAutoManual           Aut
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... ServerRole                Man
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... ServerType                Vir
SERVER1   LocalMachine    SOFTWARE\CUSTOM\Serv... WarrantyExpires           NA

Value        :
Key          :
Data         :
Hive         : None found
Type         :
ComputerName : SERVER2

SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... buildDate                 15/
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... OperatingSystem           Win
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerContactDetails       M/S
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerName                 GM
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... OwnerPhone                131
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... PatchAutoManual           -
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... ServerRole                SNB
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... ServerType                Phy
SERVER3     LocalMachine    SOFTWARE\CUSTOM\Serv... WarrantyExpires           -

I suspect the two object types are different but I don't script much and am having a complete mental block on how to get my custom entry to fit in with the ones retrieved automatically.

Upvotes: 0

Views: 38

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

Get-RegValue (assuming you're using the cmdlet from the RemoteRegistry module) returns a RegistryValue object while the PSCustomObject type accelerator obviously produces PSCustomObject objects. You can check the types for instance with the Get-Member cmdlet.

You can avoid the issue by piping the list through Select-Object. I also wouldn't use an ArrayList in the first place. Just collect the loop output in a variable.

$DataList = foreach ($Target in $ImportData) {
    $RegData = Get-RegValue ...
    if ($RegData) {
        $RegData
    } else {
        [PSCustomObject]@{
            ComputerName = $Target.ComputerName
            Hive         = 'No Data'
            Key          = ''
            Value        = ''
            Data         = ''
            Type         = ''
        }
    }
}

$DataList |
    Select-Object ComputerName, Hive, Key, Value, Data, Type |
    Export-Csv ...

The Select-Object step turns all input objects into custom objects with the selected properties.

Upvotes: 1

Related Questions