如何在Powershell中按解析日期对对象数组进行排序

时间:2015-01-15 20:55:27

标签: powershell amazon-web-services amazon-ec2

我在通过从其中一个字段(描述)解析的日期排序对象数组时遇到了一些问题。

对象列表如下所示:

Architecture        : x86_64
BlockDeviceMappings : {/dev/sda1, xvdca, xvdcb, xvdcc...}
Description         : .NET Beanstalk (new) HostManager v2.0.0.1 on Windows 2012R2 (2014-07-25) based on AMI ami-9ade1df2
Hypervisor          : xen
ImageId             : ami-1e4c8476
ImageLocation       : amazon/.NET Beanstalk Cfn Container v2.0.0.1 on Windows 2012R2
ImageOwnerAlias     : amazon
ImageType           : machine
KernelId            : 
Name                : .NET Beanstalk Cfn Container v2.0.0.1 on Windows 2012R2
OwnerId             : 102837901569
Platform            : Windows
ProductCodes        : {}
Public              : True
RamdiskId           : 
RootDeviceName      : /dev/sda1
RootDeviceType      : ebs
SriovNetSupport     : simple
State               : available
StateReason         : 
Tags                : {}
VirtualizationType  : hvm

Architecture        : x86_64
BlockDeviceMappings : {/dev/sda1, xvdca, xvdcb, xvdcc...}
Description         : .NET Beanstalk (new) HostManager v2.0.2.6 on Windows 2012R2 (2014-11-18) based on AMI ami-ba13abd2
Hypervisor          : xen
ImageId             : ami-94e879fc
ImageLocation       : amazon/.NET Beanstalk Cfn Container v2.0.2.6 on Windows 2012R2
ImageOwnerAlias     : amazon
ImageType           : machine
KernelId            : 
Name                : .NET Beanstalk Cfn Container v2.0.2.6 on Windows 2012R2
OwnerId             : 102837901569
Platform            : Windows
ProductCodes        : {}
Public              : True
RamdiskId           : 
RootDeviceName      : /dev/sda1
RootDeviceType      : ebs
SriovNetSupport     : simple
State               : available
StateReason         : 
Tags                : {}
VirtualizationType  : hvm

Architecture        : x86_64
BlockDeviceMappings : {/dev/sda1, xvdca, xvdcb, xvdcc...}
Description         : .NET Beanstalk (new) HostManager v2.0.2.1 on Windows 2012R2 (2014-10-25) based on AMI ami-ba13abd2
Hypervisor          : xen
ImageId             : ami-d848c9b0
ImageLocation       : amazon/.NET Beanstalk Cfn Container v2.0.2.1 on Windows 2012R2
ImageOwnerAlias     : amazon
ImageType           : machine
KernelId            : 
Name                : .NET Beanstalk Cfn Container v2.0.2.1 on Windows 2012R2
OwnerId             : 102837901569
Platform            : Windows
ProductCodes        : {}
Public              : True
RamdiskId           : 
RootDeviceName      : /dev/sda1
RootDeviceType      : ebs
SriovNetSupport     : simple
State               : available
StateReason         : 
Tags                : {}
VirtualizationType  : hvm

以下是部分注释掉的代码,显示日期解析似乎很好:

$platform_values = New-Object 'collections.generic.list[string]'
$platform_values.add("windows")
$filter_platform = New-Object Amazon.EC2.Model.Filter -Property @{Name = "platform"; Values = $platform_values}

$state_values = New-Object 'collections.generic.list[string]'
$state_values.add("available")
$filter_state = New-Object Amazon.EC2.Model.Filter -Property @{Name = "state"; Values = $state_values}

$name_values1 = New-Object 'collections.generic.list[string]'
$name_values1.add("*Windows 2012R2")
$filter_name1 = New-Object Amazon.EC2.Model.Filter -Property @{Name = "name"; Values = $name_values1}

$name_values2 = New-Object 'collections.generic.list[string]'
$name_values2.add(".NET Beanstalk*")
$filter_name2 = New-Object Amazon.EC2.Model.Filter -Property @{Name = "name"; Values = $name_values2}

$provider = New-Object System.Globalization.CultureInfo "en-US"
$dateRegexString = "\((\d\d\d\d-\d\d-\d\d)\)"
$dateFormatString = "yyyy-MM-dd"

#This part works fine for getting the date into a variable
#$line = ".NET Beanstalk (new) HostManager v2.0.0.1 on Windows 2012R2 (2014-07-25) based on AMI ami-9ade1df2"
#$val = [DateTime]::ParseExact([regex]::Matches($line, $dateRegexString)[0].Groups[1].Value, $dateFormatString, $provider) -as [datetime]
#Write-Host $val
#Exit

#Sorting by date isn't working though
$ami = Get-EC2Image -Owner amazon -Filter $filter_name1,$filter_name2,$filter_platform,$filter_state
$ami | Sort-Object {"[DateTime]::ParseExact([regex]::Matches(Description, $dateRegexString)[0].Groups[1].Value, $dateFormatString, $provider) -as [datetime]"} -Descending

1 个答案:

答案 0 :(得分:1)

您将scriptblock中的代码定义为字符串。删除双引号,它应该按预期工作:

$ami | Sort-Object {[DateTime]::ParseExact([regex]::Matches($_.Description, $dateRegexString)[0].Groups[1].Value, $dateFormatString, $provider) -as [datetime]} -Descending