I am having a script which takes list of servers, goes through their registry and looks for registry values using wildcard. For each hostname it produces object, but it's properties vary. These objects are added into collection and then I export-csv this collection. I would love to have all the possible column headers there but it seems that I get always just those from the first object of the collection. What better way to do it if I don't know all the possible object properties in advance?
$res=@()
$hosts = Get-Content hosts.txt
foreach ($hostname in $hosts){
$server = New-Object -TypeName psobject
$server | Add-Member -MemberType NoteProperty -Name hostname -Value $hostname
$server | Add-Member -MemberType NoteProperty -Name online -Value ""
$server | Add-Member -MemberType NoteProperty -Name IP -Value ""
$server | Add-Member -MemberType NoteProperty -Name OS -value ""
$rtn=test-connection $hostname -Count 2 -BufferSize 16 -erroraction silentlycontinue
if(!$rtn){
$server.online=$false
write-host "$hostname offline" -ForegroundColor red;
}else{
$server.online=$true
$server.IP=(($rtn.properties|? name -eq "ProtocolAddress").value|select -first 1)
$wmi=get-wmiobject -computer $hostname win32_operatingsystem -ErrorAction SilentlyContinue
$server.OS=$wmi.caption
$s=Invoke-Command -cn $hostname -ScriptBlock {
Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*|where {$_.displayname -like "*McAfee*"}|select displayname,displayversion
}
$s|foreach{
$server | Add-Member -MemberType NoteProperty -Name $_.displayname -Value $_.DisplayVersion
}
}
$res+=$server
}
$res
$res|export-csv "versions.csv" -NoTypeInformation
notepad "versions.csv"
just test it for yourself with a few hostnames and search for something in registry preferably in different OSes and put there also some hostname which is unreachable.
an example output is here:
hostname : server1
online : True
IP : secretip
OS : Microsoft Windows Server 2008 R2 Standard
McAfee Agent : 5.0.5.658
hostname : server2
online : True
IP : secretip
OS : Microsoft Windows Server 2016 Standard
McAfee Agent : 5.0.5.658
McAfee Endpoint Security Threat Prevention : 10.5.1
McAfee_EndpointSecurityForServer_10.5.1_0_x64_P0_EN : 10.5.1
hostname : server3
online : True
IP : secretip
OS : Microsoft Windows Server 2008 R2 Standard
McAfee Agent : 5.0.5.658
hostname : server4
online : True
IP : secretip
OS : Microsoft Windows Server 2008 R2 Standard
McAfee Agent : 5.0.5.658
hostname : server5
online : False
IP :
OS :
hostname : server6
online : True
IP : secretip
OS : Microsoft Windows Server 2012 R2 Standard
McAfee Agent : 5.0.5.658
McAfee Endpoint Security Threat Prevention : 10.5.1
McAfee_EndpointSecurityForServer_10.5.1_0_x64_P0_EN : 10.5.1
hostname : server7
online : True
IP : secretip
OS : Microsoft Windows Server 2016 Standard
McAfee Agent : 5.0.5.658
McAfee Endpoint Security Threat Prevention : 10.5.1
McAfee Endpoint Security Platform : 10.5.1
the resulting CSV from the above has only columns from the first object in the collection, but I want all the possible variants of those strings McAfee something