Solved

powershell expression

Posted on 2014-10-08
16
214 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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