Powershell:- Import-CSV And Array Subexpression

Background

Experienced a little error using Import-CSV.

Error

PropertyNotFoundException

Error Image

Error Text


The property 'length' cannot be found on this object. Verify that the property exists.

At line:1 char:1

+ ./readFile.person.original.ps1 datafile_01.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [readFile.person.original.ps1], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict,readFile.person.original.ps1

 

Datafiles

Datafile – Number of Lines – 01

fullname
Joe Smith

 

Datafile – Number of Lines – 02

fullname
Joe Smith
Aaron Hall

 

Code

Original

 


<#
 
#> 
param ( `
		  [Parameter(Mandatory)] [string] $filename
      )
Set-StrictMode -Version Latest

$script:ErrorActionPreference = "Stop"

<#
	Declare variables
#>
[string] $log = "";
[int]    $iPersonID =0;
[int]    $iPersonCount = 0;

[string] $personFullName = "";


<#
	Declare $listofPersons as an array of string
#>
$listofPersons= @();

$bDebug = $true;

<#
    Server Count
#>
$iPersonID =0;

$listofPersons= Import-CSV -Path $filename -Delimiter "|" ;

$iPersonCount = $listofPersons.length;

Write-Host "";

$log = "Number of Persons :- {0}" -f $iPersonCount;

Write-Host $log;

Import-CSV -Path $filename -Delimiter "|" | ForEach {

    #Write-Host $_;
	
	<#
		set local variables
	#>
	
	$personFullName = $_.fullname
	
	Write-Host "";
	
	$log = "`t Fullname:- {0}" -f $personFullName;
	
	Write-Host $log;
	
}

Revision

 


<#
 
#> 
param ( `
		  [Parameter(Mandatory)] [string] $filename
      )
Set-StrictMode -Version Latest

$script:ErrorActionPreference = "Stop"

<#
	Declare variables
#>
[string] $log = "";
[int]    $iPersonID =0;
[int]    $iPersonCount = 0;

[string] $personFullName = "";

[string] $objectType1 = "";
[string] $objectType2 = "";

<#
	Declare $listofPersons as an array of string
#>
$listofPersons= @();

$listofPersons1 = @();
$listofPersons2 = @();

$bDebug = $true;

<#
    Server Count
#>
$iPersonID =0;


<#
	$listofPersons= Import-CSV -Path $filename -Delimiter "|" ;
#>

<#

	Original
	--------
		$listofPersons= Import-CSV -Path $filename -Delimiter "|" ;
		
	Revision
	--------
		$listofPersons= @( Import-CSV -Path $filename -Delimiter "|" );
		
#>

Write-Host "";

$log = "Filename:- {0}" -f $filename;

Write-Host $log;

<#
	Read file using
		 Import-CSV -Path $filename -Delimiter "|";
#>

$listofPersons1= Import-CSV -Path $filename -Delimiter "|";


<#
	Get Object's Type ( $listofPersons1 )
#>
$objectType1 = $listofPersons1.GetType();

<#

	Read file using
	
		 @(Import-CSV -Path $filename -Delimiter "|");
#>

$listofPersons2= @(Import-CSV -Path $filename -Delimiter "|");

<#
	Get Object's Type ( $listofPersons2 )
#>
$objectType2 = $listofPersons2.GetType();


<#
	Display Object's Type ( $listofPersons1 )
#>
Write-Host "";

$log = "Object Type (`$listofPersons1):- {0}" -f $objectType1;

Write-Host $log;

<#
	Display Object's Type ( $listofPersons2 )
#>
Write-Host "";

$log = "Object Type (`$listofPersons2):- {0}" -f $objectType2;

Write-Host $log;

$iPersonCount = $listofPersons2.length;

Write-Host "";

$log = "Number of Persons:- {0}" -f $iPersonCount;

Write-Host $log;


Import-CSV -Path $filename -Delimiter "|" | ForEach {

    #Write-Host $_;
	
	<#
		set local variables
	#>
	
	$personFullName = $_.fullname
	
	Write-Host "";
	
	$log = "`t Fullname:- {0}" -f $personFullName;
	
	Write-Host $log;
	
}

Use Scenario

Original Code

File – Number of Lines – 2

Invoke


powershell ./readFile.person.original.ps1 datafile_02.txt

Output

Output – Image

Output – Text

>powershell ./readFile.person.original.ps1 datafile_02.txt

Number of Persons :- 2

         Fullname:- Joe Smith

         Fullname:- Aaron Hall

>

File – Number of Lines – 1

Invoke


powershell ./readFile.person.original.ps1 datafile_01.txt

Output

Output – Image

Output – Text

>powershell ./readFile.person.original.ps1 datafile_01.txt

readfilepersonreadFile.person.original.ps1 : The property 'length' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ ./readFile.person.original.ps1 datafile_01.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [readFile.person.original.ps1], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict,readFile.person.original.ps1

>

Revised Code

File – Number of Lines – 2

Invoke


powershell ./readFile.person.revised.ps1 datafile_02.txt

Output

Output – Image

Output – Text


>powershell ./readFile.person.revised.ps1 datafile_02.txt

Filename:- datafile_02.txt

Object Type ($listofPersons1):- System.Object[]

Object Type ($listofPersons2):- System.Object[]

Number of Persons:- 2

         Fullname:- Joe Smith

         Fullname:- Aaron Hall

>

File – Number of Lines – 1

Invoke


powershell ./readFile.person.revised.ps1 datafile_01.txt

Output

Output – Image

Output – Text

>powershell ./readFile.person.revised.ps1 datafile_01.txt
Filename:- datafile_01.txt

Object Type ($listofPersons1):- System.Management.Automation.PSCustomObject

Object Type ($listofPersons2):- System.Object[]

Number of Persons:- 1

Fullname:- Joe Smith

>

Troubleshooting

Import-CSV

  1. Number of Records in Source File
    • Factor:- Number of records in source file
      • Returned Object
        • Number of records is 1
          • System.Management.Automation.PSCustomObject
        • Number of records is 2
          • System.Object[]
      • Implication
        • Functions that expects for the returned object to be a collection will fail
        • i.e.
          • Length

Workaround

How to force a method to return an object array.

Well try the @() Array Subexpression operator.

 

SS64.COM

@( ) Array Subexpression operator.

 

@( ) Array Subexpression operator.
An array subexpression behaves just like a subexpression except that it guarantees that the output will be an array.
This works even if there is no output at all (gives an empty array.)
If the result is a scalar value then the result will be a single element array containing the scalar value.
(If the output is already an array then the use of an array subexpression will have no effect, it won’t wrap one array inside of another array.)
PS C:> @(Get-WMIObject win32_logicalDisk)

 

Source Code Control

GitLab

Powershell – Import-CSV – Return Object’s Type

Link

Referenced Work

  1. Microsoft
    • Microsoft.PowerShell.Utility
  2. Anees Asghar
    • What does @() mean in PowerShell Script?
      Link
  3.  SS64
    • Syntax Operators
      Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s