Solved

powershell expression

Posted on 2014-10-08
16
204 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
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.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

679 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