This is supposed to be a query for a virtual machine in Amazon EC2, it lists the instances by it's instanceID and LaunchTime (when it was turned up) - and I want to calculate another column that is it's LaunchTime - Current Date/Time to get a "uptime" statistic called 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
This is the error I get the error below, which seems to indicate Launchtime is not a datetime type, which sure enough when I run a get-member it sure as crap is. Now I know if I run $[0].LaunchTime this code will work, but for some reason using simply the $ variable it all goes to hell. I've tried a few date time conversion techniques like [datetime] but they have similar error "can't convert to type datetime" consequences.
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