I've been working on this a while and have realised the issue is mainly down to how to manipulate data.
I have a command, which retrieves data from remote registries for a list of servers
$RegData = Get-RegValue -ComputerName $($Target.ComputerName) -Hive $RegHive -Key $KeyName -ping -ErrorAction Stop -WarningAction Stop
Problem here is obvious to me now that I've taken a look at the data - the number of keys/values returned from the registry query is variable.
ComputerName Hive Key Value Data
------------ ---- --- ----- ----
SERVER1 LocalMachine SOFTWARE\NSS\Serv... AssetNumber 987412
SERVER1 LocalMachine SOFTWARE\NSS\Serv... BuildDate 02/02/2017
SERVER1 LocalMachine SOFTWARE\NSS\Serv... iLODefaultPwd NA
ComputerName Hive Key Value Data
------------ ---- --- ----- ----
SERVER2 LocalMachine SOFTWARE\Serv... AssetNumber 984213
SERVER2 LocalMachine SOFTWARE\Serv... BuildDate 02/02/2017
SERVER2 LocalMachine SOFTWARE\Serv... iLODefaultPwd NA
SERVER2 LocalMachine SOFTWARE\Serv... ContactTel 8237
The registry key name is constant but there can be many Data and Value pairs within:
This means that feeding it into an array/arraylist like this won't work:
Function GetRegistryData {
ForEach ($Target in $ImportData) {
$RegData = Get-RegValue -ComputerName $($Target.ComputerName) -Hive $RegHive -Key $KeyName -ping -ErrorAction Stop -WarningAction Stop
$NewObject = New-Object PSObject
$NewObject | Add-Member -NotePropertyName 'ComputerName' -NotePropertyValue $Target.ComputerName
$NewObject | Add-Member -NotePropertyName 'Key' -NotePropertyValue $KeyName
$RegData | ForEach {
$NewObject | Add-Member -NotePropertyName $_.Value -NotePropertyValue $_.Data
}
$NewObject | Add-Member -NotePropertyName 'Hive' -NotePropertyValue $RegHive
$NewObject | Add-Member -NotePropertyName 'Type' -NotePropertyValue $RegData[0].Type
$NewObject
}
}
The array will be defined by the first set of values I send (or if there's an error in collecting data it'll default to the 4 specific properties I've set) and I'll lose additional values on subsequent entries with more values. In this example it won't append "SERVER2" values because there is a new field and if there's an error in retrieving it'll set ComputerName, Key, Hive, Type as object fields and ignore any others
Any pointers as to how I can collate this data easily into an object - I'm trying to get a per server entry with Value, Data fields as below:
ComputerName Hive Key AssetNumber BuildDate
------------ ---- --- ----- ----
SERVER1 LocalMachine SOFTWARE\Serv... 987412 02/02/2017
SERVER2 LocalMachine SOFTWARE\Serv... 984123 02/02/2017
The fact that I don't know all the noteproperty names beforehand is giving me a block on this right now. I could scan ALL the server registries beforehand and get all the unique names but it seems a pretty wasteful solution.