Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

powershell expression

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
Kay
Asked:
Kay
  • 11
  • 5
1 Solution
 
KayAuthor Commented:
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
 
KayAuthor Commented:
I got it its the semicolon that was missing
0
 
KayAuthor Commented:
it's still not giving the vmhost name, there is something else I am doing wrong please help!
0
Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

 
footechCommented:
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
 
KayAuthor Commented:
thank you very much what does the -passthru mean?
0
 
KayAuthor Commented:
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
 
KayAuthor Commented:
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
 
KayAuthor Commented:
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
 
footechCommented:
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
 
KayAuthor Commented:
is there any way I can assign the object at this point? I am also trying to learn more powershell.
0
 
footechCommented:
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
 
KayAuthor Commented:
fantastic!
0
 
KayAuthor Commented:
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
 
footechCommented:
I had just copied a portion of your earlier code.  I forgot to change the comma to a semicolon.
0
 
KayAuthor Commented:
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
 
footechCommented:
Change it to
@{N="VMHost";E={$vm | get-vmhost | select -expand name}}

Open in new window

0

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now