Solved

powershell expression

Posted on 2014-10-08
16
161 Views
Last Modified: 2014-10-10
Hi guys,

I am attempting this code however it fails:

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-14)} | Select VM,Name,@{N="SizeGB";E={@([math]::Round($_.SizeGB))}},Created, @{N="VMHost",E={Get-vm $_ | get-vmhost | select name}}

its fails at @{N="VMHost",E={Get-vm $_ | get-vmhost | select name}}  

how do I fix this?

thank you in advance
0
Comment
Question by:Kay
  • 11
  • 5
16 Comments
 

Author Comment

by:Kay
ID: 40368332
I get this error message:

At line:1 char:157
+ ... , @{N="VMHost",E={$(Get-vm $_ | get-vmhost | select name)}}
+                    ~
Missing expression after ','.
At line:1 char:157
+ ... , @{N="VMHost",E={$(Get-vm $_ | get-vmhost | select name)}}
+                    ~~
Unexpected token 'E=' in expression or statement.
At line:1 char:157
+ ... , @{N="VMHost",E={$(Get-vm $_ | get-vmhost | select name)}}
+                    ~
The hash literal was incomplete.
At line:1 char:200
+ ... | select name)}}
+                    ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterToken
0
 

Author Comment

by:Kay
ID: 40368338
I got it its the semicolon that was missing
0
 

Author Comment

by:Kay
ID: 40368339
it's still not giving the vmhost name, there is something else I am doing wrong please help!
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40368768
Give this a shot.
Get-VM | ForEach `
{
    $obj = $_ | Get-Snapshot |
     Where {$_.Created -lt (Get-Date).AddDays(-14)} |
     Select VM,Name,@{N="SizeGB";E={@([math]::Round($_.SizeGB))}},Created
    $obj | Add-Member -Name "VMHost" -Value ($_ | get-vmhost | select -expand name) -PassThru
}

Open in new window

0
 

Author Comment

by:Kay
ID: 40370267
thank you very much what does the -passthru mean?
0
 

Author Comment

by:Kay
ID: 40370275
it don't work, it prompts for membertype and then when I press enter it says:

Get-VM *cfg* | ForEach {
    $obj = $_ | Get-Snapshot |
     Where {$_.Created -lt (Get-Date).AddDays(-14)} |
     Select VM,Name,@{N="SizeGB";E={@([math]::Round($_.SizeGB))}},Created
    $obj | Add-Member -Name "VMHost" -Value ($_ | get-vmhost | select -expand name) -PassThru
}
0
 

Author Comment

by:Kay
ID: 40370278
sorry the error message is :

Add-Member : Cannot bind parameter 'MemberType'. Cannot convert value "" to type
"System.Management.Automation.PSMemberTypes". Error: "Cannot convert the "" value of type "System.String" to type
"System.Management.Automation.PSMemberTypes"."
At line:5 char:12
+     $obj | Add-Member -Name "VMHost" -Value ($_ | get-vmhost | select -expand na ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Add-Member], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.AddMemberCommand
0
 

Author Comment

by:Kay
ID: 40370280
I can't understand why this doesn't work:

Get-VM | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-14)} | Select VM,Name,@{N="SizeGB";E={@([math]::Round($_.SizeGB))}},Created, @{N="VMHost";E={Get-vm $_ | get-vmhost | select name}}
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 39

Expert Comment

by:footech
ID: 40370403
Sorry, just need to change this line as follows:
$obj | Add-Member -MemberType NoteProperty -Name "VMHost" -Value ($_ | get-vmhost | select -expand name) -PassThru

Open in new window

By default, the Add-Member cmdlet doesn't output anything.  The -PassThru parameter changes that.

This...
@{N="VMHost";E={Get-vm $_ | get-vmhost | select name}}
doesn't work because $_ doesn't have the needed object at that point.
0
 

Author Comment

by:Kay
ID: 40370410
is there any way I can assign the object at this point? I am also trying to learn more powershell.
0
 
LVL 39

Expert Comment

by:footech
ID: 40370447
You have to use a variable to capture the object further up the pipeline so that you can reuse it.  The below would probably work.
Get-VM | ForEach `
{
    $vm = $_
    $vm | Get-Snapshot | Where {$_.Created -lt (Get-Date).AddDays(-14)} | Select VM,Name,@{N="SizeGB";E={@([math]::Round($_.SizeGB))}},Created, @{N="VMHost",E={$vm | get-vmhost | select name}} 
}

Open in new window

0
 

Author Comment

by:Kay
ID: 40370538
fantastic!
0
 

Author Comment

by:Kay
ID: 40370548
I get this error message:

At line:3 char:157
+ ... , @{N="VMHost",E={$vm | get-vmhost | select name}}
+                    ~
Missing expression after ','.
At line:3 char:157
+ ... , @{N="VMHost",E={$vm | get-vmhost | select name}}
+                    ~~
Unexpected token 'E=' in expression or statement.
At line:3 char:157
+ ... , @{N="VMHost",E={$vm | get-vmhost | select name}}
+                    ~
The hash literal was incomplete.
At line:4 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterToken
0
 
LVL 39

Expert Comment

by:footech
ID: 40371000
I had just copied a portion of your earlier code.  I forgot to change the comma to a semicolon.
0
 

Author Comment

by:Kay
ID: 40373377
fantastic this works, problem is I get this @{name=....

Created : 09/10/2014 10:47:33
VMHost  : @{Name=vmw-esx-31.yu.asul.ac.uk}

and I only want the name?
0
 
LVL 39

Expert Comment

by:footech
ID: 40373404
Change it to
@{N="VMHost";E={$vm | get-vmhost | select -expand name}}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now