INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Unable to read hash value in Powershell

Unable to read hash value in Powershell

(OP)
I am writing a simple script to get more familiar with powershell.

This script reads input parameters into a hash

CODE --> Powershell

$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, http://technet.microsoft.com/en-us/library/hh84778...

Where am I going wrong???

RE: Unable to read hash value in Powershell

I pretty new to all of this so hopefully someone smarter than me can chime in...
But here is what I see

Quote:

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.

RE: Unable to read hash value in Powershell

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.

RE: Unable to read hash value in Powershell

(OP)
Thank you Blister, this now works!!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close