Solved

powershell expression

Posted on 2014-10-08
16
188 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 40

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
 
LVL 40

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 40

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 40

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 40

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

837 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