如何在 Powershell 中按解析日期对对象数组进行排序
How to sort an array of objects by a parsed date in Powershell
我在按从其中一个字段(描述)中解析出的日期对对象数组进行排序时遇到一些问题。
对象列表如下所示:
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
您将脚本块中的代码定义为字符串。删除双引号,它应该按预期工作:
$ami | Sort-Object {[DateTime]::ParseExact([regex]::Matches($_.Description, $dateRegexString)[0].Groups[1].Value, $dateFormatString, $provider) -as [datetime]} -Descending
我在按从其中一个字段(描述)中解析出的日期对对象数组进行排序时遇到一些问题。
对象列表如下所示:
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
您将脚本块中的代码定义为字符串。删除双引号,它应该按预期工作:
$ami | Sort-Object {[DateTime]::ParseExact([regex]::Matches($_.Description, $dateRegexString)[0].Groups[1].Value, $dateFormatString, $provider) -as [datetime]} -Descending