1

I have a weird issues with Powershell Version 2.0.

The following works on newer versions but its not working as expected on this version. Any help is appreciated.

$DB = Import-Csv -Path "$($path)\DBExtrat.csv"

which is fine. Headers in DBExtrat.csv ('hostname','hostip','name','type') all 4 headers are reorganized and show up if i run

$DB

But if I try $DB.name or $DB.hostname it returns noting. I need to be able to call it like this because my whole logic is tied to those specific variables names.

I've already tried adding the -header option:

$DB = Import-Csv -Path "$($path)\DBExtrat.csv" -Header 'hostname','hostip','name','type'

but it doesn't work and also creates unnecessary extra row with header data.

mklement0
  • 382,024
  • 64
  • 607
  • 775
Evo
  • 41
  • 5
  • can you run a `$DB | Get-Member` and seeing what NoteProperty it returns for your Column Names? – Abraham Zinala Jan 06 '21 at 22:54
  • TypeName: System.Management.Automation.PSCustomObject Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() hostip NoteProperty System.String hostip=1.1.1.1 hostname NoteProperty System.String hostname=bla.bla name NoteProperty System.String name=Default type NoteProperty System.String type=I – Evo Jan 06 '21 at 23:21
  • Does piping it make any difference? `| select-object name` – Abraham Zinala Jan 06 '21 at 23:33
  • Hi Abraham, Thanks for the help this works | select-object name or any other header value. I don't understand why. Do you think there is a way to still get it to work with $DB.name? Thank you again – Evo Jan 06 '21 at 23:55
  • 1
    @Evo Are you using PowerShell 2.0? Upgrade. Property Enumeration was introduced in 3.0 or 4.0 – Mathias R. Jessen Jan 07 '21 at 11:44
  • Thanks Mathias, yes it's powershell 2.0 but I can't upgrade. Thanks for the help. – Evo Jan 07 '21 at 13:51

2 Answers2

1

With an expression such as $DB.name, you're trying to get the name property values of all elements of collection $DB, by performing the property access on the collection as a whole.

This feature is called member-access enumeration, and it is only available in PowerShell v3+.

The PowerShell v2 equivalent requires use of either Select-Object or ForEach-Object:

# Note the use of -ExpandProperty to ensure that only the property *value*
# is returned (without it, you get a *custom object* with a 'name' property).
$DB | Select-Object -ExpandProperty name

# Slower alternative, but can provide more flexibility
$DB | ForEach-Object { $_.name }
mklement0
  • 382,024
  • 64
  • 607
  • 775
0

I'd recommend going with @mklement0 answer; short and simple. Alternatively, going off the question you asked in the comments, you may try working with a custom object and seeing if the below works.

$import = Import-Csv -Path "$($path)\DBExtrat.csv"
$Object = New-Object psobject -Property @{
    'hostname' = $import | Select-Object -ExpandProperty hostname
    'hostip'   = $import | Select-Object -ExpandProperty hostip
    'name'     = $import | Select-Object -ExpandProperty name
    'type'     = $import | Select-Object -ExpandProperty type   
}
    "$Object.hostname - $Object.hostip"
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24