PowerShell – Error – “WARNING: Cannot process argument transformation on parameter ‘Hash’. Cannot convert the “System.Collections.Generic.List`1[System.String]” value of type “System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]” to type “System.Collections.Hashtable”.”

Background

Once again, I found myself with stolen code.

I tried to add to it.

But, per usual, ran into ditch.

Error

Here is the error message:-

Textual


WARNING: Cannot process argument transformation on parameter 'Hash'. Cannot convert the "System.Collections.Generic.List`1[System.String]"
value of type "System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]" to type "System.Collections.Hashtable".

Image

 

Code

PowerShell

Script


[System.Collections.Generic.List[System.String]] $objList = $null;
[string] $strBuffer = $null;
[object] $e = $null;
[int] $line =0;  
[string] $msg = ""; 
[string] $exceptionMessageFull = "";
[string] $CHAR_SEPARATOR = "| ";
[string] $objectType = $null;
[string] $log = $null;


function Convert-HashToString
{
	param
	(
		[Parameter(Mandatory = $true)]
		[System.Collections.Hashtable]
		$Hash
	)
	$hashstr = "@{"
	$keys = $Hash.keys
	foreach ($key in $keys)
	{
		$v = $Hash[$key]
		if ($key -match "\s")
		{
			$hashstr += "`"$key`"" + "=" + "`"$v`"" + ";"
		}
		else
		{
			$hashstr += $key + "=" + "`"$v`"" + ";"
		}
	}
	$hashstr += "}"
	return $hashstr
}

$objList = New-Object -TypeName System.Collections.Generic.List[System.String];

<#
	The world’s most populated islands
	https://vividmaps.com/most-populated-islands
#>
$objList.Add("Java, Indonesia")
$objList.Add("Honshu, Japan")
$objList.Add("Great Britain, United Kingdom")
$objList.Add("Luzon, Philippines")
$objList.Add("Sumatra, Indonesia")


Write-Host "";

if ( $null -ne $objList )
{
	
	Write-Host "Get Type of `$objList ";
	Write-Host "************************************";
	Write-Host "";

	$objectType = $objList.GetType();
	
	$log = "Type of `$objList is {0}" -f $objectType;
	
	Write-Host $log;
	
	Write-Host "";
	Write-Host "";

}

Write-Host "Output list contents - using `$objList ";
Write-Host "************************************";
Write-Host "";

<#
	Write Contents using $objList
#>
$objList

Write-Host "";
Write-Host "";



Write-Host "Output list contents - using function Convert-HashToString ";
Write-Host "******************************************************";

Write-Host "";

$strBuffer = $null;

try
{
	
	$strBuffer = Convert-HashToString $objList;

}
catch
{
	  
	$e = $_.Exception
  
	$line = $_.InvocationInfo.ScriptLineNumber
  
	$msg = $e.Message 
	
	Write-Warning -Message $msg;
	
	<#
	
		$exceptionMessageFull = "caught exception: $e at Line Number $line";
	
	#>
	
	$exceptionMessageFull = "caught exception at Line Number {0}" -f $line;
	
	Write-Host -ForegroundColor Red $exceptionMessageFull;	
  
}

if ($null -ne $strBuffer )
{
	
	$strBuffer;

}


Write-Host "";
Write-Host "";

Write-Host "Output list contents - using `$objList -join <separator> ";
Write-Host "******************************************************";

Write-Host "";


<#
	Write Contents using $objList -join <separator>
#>
$objList -join $CHAR_SEPARATOR;

Write-Host "";

Write-Host "";
Write-Host "";


Output

Output – Image

Output – Textual



>powershell -file System.Collections.Generic.List.items.list.ps1

Get Type of $objList
************************************

Type of $objList is System.Collections.Generic.List[string]


Output list contents - using $objList
************************************

Java, Indonesia
Honshu, Japan
Great Britain, United Kingdom
Luzon, Philippines
Sumatra, Indonesia


Output list contents - using function Convert-HashToString
******************************************************

WARNING: Cannot process argument transformation on parameter 'Hash'. Cannot convert the "System.Collections.Generic.List`1[System.String]"
value of type "System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]" to type "System.Collections.Hashtable".
caught exception at Line Number 95



Output list contents - using $objList -join <separator>
******************************************************

Java, Indonesia| Honshu, Japan| Great Britain, United Kingdom| Luzon, Philippines| Sumatra, Indonesia




>

 

Explanation

  1. My problem started when I tried exposing more properties from a returned object
  2. Returned Property
    • HashTable?
      • I assumed the returned property was a hashtable
      • I googled for how to convert an hashtable to a delimited string
      • The function that I found is the Convert-HashToString
      • I passed the returned property to the Convert-HashToString function
      • And, got the error message
        • WARNING: Cannot process argument transformation on parameter ‘Hash‘. Cannot convert the “System.Collections.Generic.List`1[System.String]”
          value of type “System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral,
          PublicKeyToken=b77a5c561934e089]]” to type “System.Collections.Hashtable”.
    • System.Collections.Generic.List[string]?
      • Thankfully, PowerShell was graceful enough to indicate the returned property’s actual type
      • The type is System.Collections.Generic.List[string]
      • Join Verb
        • We can use the join verb to get a delimited string from our collection
        • Syntax
          • <object> -join <delimiter>
        • Sample Code
          • $objList -join $CHAR_SEPARATOR

 

Summary

Fam, steal code.

But, do not do so sheepishly.

PowerShell’s GetType is your help.

Use it to get an object type and do not assume that you already know each object’s type.

Also, please check if the object or property is null.

Basic Defensive Programming.

That is one of the blind sides of habitually stealing code.

You inherit other people’s vices.

 

Leave a comment