Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to read hash value in Powershell 1

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
0
0
US
I am writing a simple script to get more familiar with powershell.

This script reads input parameters into a hash

Code:
$states = @($args)
$states
write-host Color is $states.color

On the command-line, I set the following values$shape = 'circle'; $color = 'pink'; $size = 'large'I then invoke the program with the following command

Code:
.\shapes_n_colors.ps1  $shape $size $color

And, I get the following output:

Code:
circle
large
pink
Color is

I am unable to figure out why $states.color is blank. I was expecting the output "Color is pink"

I am following this artical,
Where am I going wrong???
 
I pretty new to all of this so hopefully someone smarter than me can chime in...
But here is what I see
Enclose the hash table in braces ({})
You're using parenthesis.

Secondly, you don't have any attributes assigned to your object, yet you're trying to reference them. The way I'm looking at it, the script would think "circle", "large", and "pink" are states. I don't know the syntax but generally speaking I think it would go like this:

In the script you would have to acquire the values then assign them to the attributes
$states[1].shape = $shape
$states[1].size = $size
$states[1].color = $color

write-host Color is $states[1].color

.\shapes_n_colors.ps1 $shape $size $color

Again, I don't know the syntax, so I don't know if you need to declare the number of attibutes each object will have or what. Hopefully this will help you enough so you can figure it out.



Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Okay I played with this a bit and here is what I came up with:
Code:
param($shape,$size,$color)
$states = @{Shape=$shape; Size=$size; Color=$color}
$states
write-host "Color is "$states.Color

Call it like you did above: .\shapes_n_colors.ps1 $shape $size $color


Light travels faster than sound. That's why some people appear bright until you hear them speak.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top