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.
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 }