Avatar of Alan Varga
Alan Varga
Flag for United States of America asked on

Property cannot be found on a hashtable passed in from another script

I can't believe how difficult it is to find how to parse and use parameters passed in from another script!  I've been at this for almost 5 hours.

There are plenty of articles on how to create a hashtable, and some on how to pass a hashtable to another script, but nothing on what to do with the hashtable once the target script receives it.

This is an excerpt from "Test 1A-sending script.ps1"
$parms = @{
    ComputerName = 'mike';
    ComputerId = 'SYS 001'
}

#Display hashtable
$parms.Keys
$parms.Values

$filePath = ".\Test 1B-receiving script.ps1"
$commandLine = "-NoExit & `'$filePath`' $parms"
Write-Host `n$commandLine -f Cyan

Start-Process Powershell.exe -ArgumentList $commandLine

Open in new window


and this is an excerpt from "Test 1B-receiving script.ps1"
Param(
    $parms2
)

Write-Host "Made it to test 1B."
Write-Host $parms2 -ForegroundColor Yellow
Write-Host $parms2.ComputerName -ForegroundColor Gray

Open in new window


Here are the results of running the sending script, which appear in a separate window for the receiving script:
Made it to test 1B.
System.Collections.Hashtable
The property 'ComputerName' cannot be found on this object.  Verify that the property exists.
At: C:\Workshop\PowerShell-work\Passing arguments\Test 1B-receiving script.ps1:61 char:1
+Write-Host $parms2.ComputerName -ForegroundColor Gray

Open in new window

Powershell* hashtable

Avatar of undefined
Last Comment
Qlemo

8/22/2022 - Mon
Qlemo

Why are you making this more complicated as they need to be? You call another PowerShell script this way:
& ".\Test 1B-receiving script.ps1" @parms

Open in new window

Alan Varga

ASKER
Qlemo,

I can certainly simplify the call, but you didn't answer the question of how to use @parms in the called/receiving script.
ASKER CERTIFIED SOLUTION
Ben Personick (Previously QCubed)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Qlemo

Because there is no need to. The way I showed, called splatting, you supply a hash table containing parameters to other funtcions and cmdlets, with the keys as parameter name and the value as parameter value.

Your issue here is not how to read out the parameters in the called script, but how to provide them. A "$hashtable" almost always results in the type string (!) you see.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Alan Varga

ASKER
Ben,

Here's exactly what I needed to see:
param(
    $parmset2 = @()
)

Open in new window

I didn't know what to set the parameter variable to.  @()  did the trick.  I modified my receiving script and the hashtable values were extracted.  Thank you very much!
Alan Varga

ASKER
The correct solution was simple and easy to understand, but I couldn't find this after hours of searching and reading.  There is always an export at EE who can defuse my frustration.  Thanks Ben!
Ben Personick (Previously QCubed)

Glad to help :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Qlemo

VieleFragen, your statement in #a42111113 is wrong. The key was to provide the hash table as parameter when calling, instead of a string. Your way to call the second script was wrong.

Be aware that that code is correct for having a hash table as single parameter value, but not for passing true parameters thru calls. E.g., you could not do that for calling Get-WMIObject in the second script.