这应该是对 Amazon EC2 中的虚拟机的查询,它通过它的 instanceID 和 LaunchTime(当它出现时)列出实例 - 我想计算另一列是 LaunchTime - 当前日期/时间来获取称为 HoursSinceBoot 的“正常运行时间”统计信息。
#get instances
$instances = foreach($i in (get-ec2instance)) {$i.RunningInstance | Select-Object InstanceId,LaunchTime}
$instances | Add-Member -type NoteProperty -name HoursSinceBoot -value (Foreach-object -inputobject $instances {New-TimeSpan -End (get-date) -Start $_.LaunchTime} | select -expandproperty hours)
$instances
这是我得到以下错误的错误,这似乎表明 Launchtime 不是日期时间类型,当我运行 get-member 时,它确实是废话。现在我知道如果我运行 $ [0].LaunchTime ,这段代码就可以工作,但由于某种原因,只使用 $变量,这一切都变得糟糕透顶。我尝试了一些日期时间转换技术,例如 [datetime],但它们有类似的错误“无法转换为日期时间类型”的后果。
New-TimeSpan : Cannot convert 'System.Object[]' to the type 'System.DateTime' required by parameter 'Start'. Specified method is not supported.
At C:\xxx\powershell\aws-watchdog.ps1:3 char:149
+ ... t-date) -Start $_.LaunchTime} | select -expandproperty hours)
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.NewTimeSpanCommand