【powershell】how to input a hastabler as paramte

for the record i have write a script below that list the hashtable’s value and it work well

$return = & 'c:\Program Files\Anime\bin\animelist (2).ps1'            

 filter get-hash([string]$hashtable) {
$_.$hashtable
}


$return1 = $return  | Get-Member -MemberType NoteProperty|Select-Object -Property name
$return1| foreach-object {

    $return|get-hash $_.name
}

after this i try to improve my script so it could list any hashtable i input for example

$return | list.ps1

 list.ps $return

but i have fail with below code

param (
    [Parameter()]
    [string]
    $return
)

 filter get-hash([string]$hashtable) {
$_.$hashtable
}


$return1 = $return  | Get-Member -MemberType NoteProperty|Select-Object -Property name
$return1| foreach-object {

    $return|get-hash $_.name
}

I’m not following the code very easily to understand the goal really. Typically I’d suggest providing output as well, and then the expected output you desire.

However if the parameter $return is the hashtable, try casting [Systems.Collections.HashTable] or just [HashTable] instead:

param (
    [Parameter()]
    [Hashtable]
    $return
)

Casting [String] to a hashtable is basically going to invoke the ToString method on it, resulting in a string output.

the code was get the every noteobject in the parameter and filter the value of hashtable and list the value of the hashtable
and i manage to do it with code

$return = & 'c:\Program Files\Anime\bin\animelist (2).ps1'

 filter get-hash([string]$hashtable) {
$_.$hashtable
}


$return1 = $return  | Get-Member -MemberType NoteProperty|Select-Object -Property name
$return1| foreach-object {

    $return|get-hash $_.name
}


I have solve it with code



[CmdletBinding()]
param (
    [Parameter()]
    [PSCustomObject]$return
)

 filter get-hash([string]$hashtable) {
$_.$hashtable
}


$return1 = $return  | Get-Member -MemberType NoteProperty|Select-Object -Property name
$return1| foreach-object {

    $return|get-hash $_.name
}

Just want to point out that ur question was how to pass a hashtable as a Parameter. I answered that but that really wasn’t what you’re looking for.

Take a look at my previous post to help us help you in the future!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.