3

I have one problem with two questions. I have following powercli cmd:

$snapLst = Get-VM vmindev |Get-Snapshot |Select VM, Name, Description, `
 @{Name='Created'; Expression={{$_.Created.ToString("yyMMdd")}}, `
 @{Name='SizeMB'; Expression={[int] $_.SizeMB}}
$resultLst=$snapLst| where SizeMB -gt 1000 |Sort-Object SizeMB |`
 Select @{Name='Type'; Expression={'BIG'}},*

When run in my DEV env (powercli session on my desktop connecting to the vSphere sever), everything is fine. When run in PRODUCTION (ie. powercli session on the vSphere server), I get the following error:

Where-Object : Cannot bind parameter 'FilterScript'. Cannot convert the "SizeMB" value of type "System.String" to type System.Management.Automation.ScriptBlock".
    At C:\Users\kness\Scripts\sn2.ps1:32 char:27
    + $resultLst=$snapLst| where <<<<  SizeMB -gt 1000 |Sort-Object SizeMB |Select
    @{Name='Type'; Expression={'BIG'}},
        + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBindingException
        + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.WhereObjectCommand

Q1: What - in the environment setup - would make the script behaves differently? I checked the versions of the powercli ... they're exactly the same.

Q2: Because the above error, I look into the property of the list by running this cmd:

$snapLst = Get-VM vmindev |Get-Snapshot |Get-Member |Findstr Size
SizeGB            Property   System.Nullable`1[[System.Decimal, mscorlib, Ve...
SizeMB            Property   System.Decimal SizeMB {get;}

SizeMB is a "decimal" type; why the error complains that it is a "string"?

Thx in advance!

ky bof
  • 89
  • 1
  • 5
  • 1
    For Q1: What could be different is the PowerShell versions (Management Framework versions) between your Dev and Prod environments. Also, your code in Q2 is probably off. What your'e actually doing is getting reflective information about the "Size" property of your object, not the Size value. – thepip3r Jun 08 '17 at 17:11
  • 1
    you name the property SizeInMB but then try to filter on SizeMB – Noah Sparks Jun 08 '17 at 17:37
  • 3
    `where SizeMB -gt 1000` is a shortcut syntax which needs PSv3+ (I think, maybe v4). Change it to `where { $_.SizeMB -gt 1000 }` for your PSv2 production server. Type `$PsVersionTable` to see what you have, but I'm guessing your desktop is Windows 8.1 (PSv4) or 10 (PSv5.1) and your vSphere server is Server 2008 r2 (PSv2) – TessellatingHeckler Jun 08 '17 at 17:40
  • 1
    "*SizeMB is a "decimal" type; why the error complains that it is a "string*"?" - because at the point of the error, PowerShell is reading "SizeMB" as text to pass to `where`, it hasn't got as far as looking it up as a property yet, this is complaining about earlier language processing. It's looking like a string parameter. – TessellatingHeckler Jun 08 '17 at 17:43
  • @BenH, that's nice that he's now included the appropriate code. That second line wasn't there on the original post--which is when I made the comment. – thepip3r Jun 08 '17 at 17:49
  • @thepip3r [This meta post](https://meta.stackoverflow.com/questions/335532/should-you-delete-comments-about-an-error-that-was-fixed) is relevant then. – BenH Jun 08 '17 at 17:56
  • BenH,Noah, you are right! That was a cut&paste from a version where I want to see whether change name would make a difference. I cut&paste from the original one now. – ky bof Jun 08 '17 at 20:20

1 Answers1

5

I got it resolved with helps fr TessellatingHeckler.

A1. Although my PowerCLI package is the same, it operates on two different Powershell installation. On my desktop, PSVersion is 4.0 and on the production server, it is 2.0

A2. This is related to A1. In PS 4.0, the cmdlet where is translated properly to Where-Object {...}. Whereas in PS 2.0, it is a syntax error. The complain about SizeMB being System.String is just a red herring following the "where" syntax error.

By changing where SizeMB -gt 1000 to Where-Object {$_.SizeMB -gt 1000} the script is now working. I guess, the alternative would be to upgrade the PS on the vSphere server ... but I dont want to go there. Thanks for your helps everyone.

ky bof
  • 89
  • 1
  • 5