Link to home
Create AccountLog in
Avatar of Alan Varga
Alan VargaFlag 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

$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"

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

Avatar of Qlemo
Flag of Germany image

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

Avatar of Alan Varga



I can certainly simplify the call, but you didn't answer the question of how to use @parms in the called/receiving script.
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.

Here's exactly what I needed to see:
    $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!
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!
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.