powershell - 其中对象 : Cannot bind parameter 'FilterScript'

标签 powershell powercli

我有一个问题有两个问题。我有以下 powercli 命令:

$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'}},*

在我的 DEV 环境中运行时(连接到 vSphere 服务器的桌面上的 powercli session ),一切都很好。在生产环境中运行时(即 vSphere 服务器上的 powercli session ),出现以下错误:

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

问题 1:什么 - 在环境设置中 - 会使脚本的行为有所不同?我检查了 powercli 的版本……它们完全一样。

Q2:因为上面的错误,我通过运行这个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是“十进制”类型;为什么错误提示它是一个“字符串”?

提前致谢!

最佳答案

我在 TessellatingHeckler 的帮助下解决了这个问题。

A1.虽然我的 PowerCLI 包是相同的,但它在两个不同的 Powershell 安装上运行。在我的桌面上,PSVersion 是 4.0,在生产服务器上,它是 2.0

A2.这与A1有关。在 PS 4.0 中,cmdlet where 被正确转换为 Where-Object {...}。而在 PS 2.0 中,这是一个语法错误。关于 SizeMBSystem.String 的提示只是“where”语法错误后的转移注意力。

通过将 where SizeMB -gt 1000 更改为 Where-Object {$_.SizeMB -gt 1000},脚本现在可以运行了。我想,另一种方法是升级 vSphere 服务器上的 PS……但我不想去那里。感谢大家的帮助。

关于powershell - 其中对象 : Cannot bind parameter 'FilterScript' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44441623/

相关文章:

python - powercli 中的 invoke-vmscript 的 pyvmomi 等效项是什么?

powershell - PowerCLI遍历集群和主机

c# - 模拟在 Powershell 中使用 WindowsIdentity 抛出 FileNotFoundException

json - 使用Powershell从json获取动态键值

powershell - Powershell当前时间减去特定格式的n秒

powershell - 输出文件和回车

powershell - 反序列化对象类型问题 - 特别是 Powershell 5 类和 Import-CliXml

powershell - 如何从PS输出中过滤出特定值

powershell - 需要有关网络驱动器中的 Powershell Copy-Item 的帮助