2

When I create object in powershell v2 i am unable to access powershell member properties, which will flowlesly works with PowerShell V3. e.g. if i create below object in v3,

$Services = @();
$item = @{};
$item.Name = "ServiceName";
$item.Action = 2;
$item.ActionTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss";
$obj = New-Object PSObject -Property $item;
$Services = $Services + $obj

I can access $services.Action which will be 2, whether on PowerShell v2 it will be blank.

Any help?

thanks

2 Answers 2

3

This is actually because you are wrapping the object in an array.

In v2, to get all of the Action properties of an array of objects, you would do something like this:

$Services | ForEach-Object { $_.Action }
# or
$Services | Select-Object -ExpandProperty Action

In PowerShell v3, this is no longer necessary:

$Services.Action

Will automatically perform the same operation.

New V3 Language Features

Further, if you had just done $obj.Action it would have worked in v2 as well (for just that one object).

Sign up to request clarification or add additional context in comments.

Comments

0

Depending on if you want to list all actions or just for a particular index, you can use:

$Services | Select -ExpandProperty Action

or (in the case of the first service):

$Services[0].Action

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.