0

I am using the Powershell ISE for the below snippets.

Below ist the output format of the following command:

aws ec2 --profile $profile --region $region describe-instances --query "Reservations[*].Instances[*].[InstanceId,LaunchTime,State]"
[
    [
        [
            "i-abc0123456789",
            "2023-07-17T04:00:18+00:00",
            {
                "Code": 8,
                "Name": "running"
            }
        ]
    ],
    [
        [
            "i-xyz9876543210",
            "2023-07-17T04:00:16+00:00",
            {
                "Code": 16,
                "Name": "running"
            }
        ]
    ]
]

I have taken the help from this community to iterate the above input json stream in the below way as my lack of knowledge on this part, but I am now not able to see any value as an output of it i.e.

Write-Output ("instanceid :"+$a)

  $objects = aws ec2 --profile $awsProfile --region $awsRegion describe-instances --query "Reservations[*].Instances[*].[InstanceId,LaunchTime,State]" | ConvertFrom-Json 
  $objects | ForEach-Object {  
    # Construct and implicitly output a [pscustomobject]
    # instance from the elements of each nested array.
    $a = [pscustomobject] @{
      instanceid = $_[0][0]
      launchdateb = [datetimeoffset] $_[0][1]
      code = $_[0][2].Code
      name = $_[0][2].Name
      
   }
   Write-Output ("instanceid :"+$a)
}
  1. Is this the way or any other comprehend way if I can iterate through the aws-cli objects of describe-instances
  2. And while doing the looping/iterating - how do I compare the Launchdate with any other date separately ?
mklement0
  • 382,024
  • 64
  • 607
  • 775
Money Times
  • 135
  • 9
  • Do you mean `Write-Output ("instanceid :"+$a.instanceid)`? Right now you are trying to concatenate the `[string]`, `"instanceid :"` with the `[pscustomobject]`, `$a`. If you want to view all of the pscustomobjects in one table/list you don't have to set the object to `$a` and you shouldn't use `Write-Output` – Nico Nekoru Jul 25 '23 at 15:10
  • `$objects | ForEach-Object { [pscustomobject] @{ instanceid = $_[0][0]; launchdateb = [datetimeoffset] $_[0][1]; code = $_[0][2].Code; name = $_[0][2].Name} }` – Nico Nekoru Jul 25 '23 at 15:13
  • Re 1.: I suggest providing a [mcve]. Re 2: I suggest asking a follow-up question once 1. is answered. – mklement0 Jul 26 '23 at 02:23
  • As an aside: The PowerShell ISE is [no longer actively developed](https://docs.microsoft.com/en-us/powershell/scripting/components/ise/introducing-the-windows-powershell-ise#support) and [there are reasons not to use it](https://stackoverflow.com/a/57134096/45375) (bottom section), notably not being able to run PowerShell (Core) 7+. The actively developed, cross-platform editor that offers the best PowerShell development experience is [Visual Studio Code](https://code.visualstudio.com/) with its [PowerShell extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell). – mklement0 Jul 26 '23 at 02:24
  • @mklement0 As i have checked the same snippet is not running from VisualStudioCode which you have referred, wiuld please check once with this input ` [ [ "i-xyz9876543210", "2023-07-17T04:00:16+00:00", { "Code": 16, "Name": "running" } ] ] ]` if that is working – Money Times Jul 26 '23 at 10:49
  • @NicoNekoru Could you guide Me a liitle bit that how should I utilise this snippet to check the individual object ? – Money Times Jul 26 '23 at 11:13
  • @MoneyTimes, the JSON snippet in your comment is invalid, it's missing an opening `[`; if I add that, it works as expected for me (`'[ [ [ "i-xyz9876543210", "2023-07-17T04:00:16+00:00", { "Code": 16, "Name": "running" } ] ] ]' | ConvertFrom-Json | ForEach-Object { ... }`) – mklement0 Jul 26 '23 at 14:22
  • @mklement0 If I run it `aws ec2 --profile $profile --region $region describe-instances --query "Reservations[*].Instances[*].[InstanceId,LaunchTime,State]" | ForEach-Object { # Construct and implicitly output a [pscustomobject] # instance from the elements of each nested array. $a = [pscustomobject] @{ instanceid = $_[0][0] launchdateb = [datetimeoffset] $_[0][1] code = $_[0][2].Code name = $_[0][2].Name } Write-Output ("instanceid :"+$a) } ` – Money Times Jul 26 '23 at 15:37
  • continueing frm the above ...I am getting the below null value `instanceid :@{instanceid= ; launchdateb=01/01/0001 00:00:00 +00:00; code=; name=} instanceid :@{instanceid=]; launchdateb=01/01/0001 00:00:00 +00:00; code=; name=}` – Money Times Jul 26 '23 at 15:38
  • @MoneyTimes, now you're missing the `ConvertFrom-Json` call (as well as enclosure of `aws ... | ConvertFrom-Json` in `(...)`, if you pipe directly to `ForEach-Object` in Windows PowerShell). – mklement0 Jul 26 '23 at 15:43
  • @mklement0 added `ConvertFrom-Json` , then getting this error message `Cannot convert null to type "System.DateTimeOffset". At line:7 char:5 + $a = [pscustomobject] @{ + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : nullToObjectInvalidCast instanceid :@{instanceid=]; launchdateb=01/01/0001 00:00:00 +00:00; code=; name=}` – Money Times Jul 26 '23 at 15:52
  • 1
    @MoneyTimes, we're back to square one: If you've followed all the instructions and it works with a literal JSON string as the input (as shown in the bottom snippet of my [previous answer](https://stackoverflow.com/a/76733849/45375), the implication is that the JSON returned by your `aws` call is structured differently, and you need to find it how. Without a [mcve], I don't think we'll be able to help you. – mklement0 Jul 26 '23 at 16:26
  • @mklement0 believe me Sir I am putting in th same thing that you referred `aws ec2 --profile $profile --region $region describe-instances --query "Reservations[*].Instances[*].[InstanceId,LaunchTime,State]" | ConvertFrom-Json | ForEach-Object { # Construct and implicitly output a [pscustomobject] # instance from the elements of each nested array. $a = [pscustomobject] @{...} Write-Output ("instanceid :"+$a) }` and getting the below error again – Money Times Jul 27 '23 at 10:19
  • @mklement0 `Cannot convert null to type "System.DateTimeOffset". At line:7 char:5 + $a = [pscustomobject] @{ + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : nullToObjectInvalidCast instanceid :@{instanceid=]; launchdateb=01/01/0001 00:00:00 +00:00; code=; name=}` – Money Times Jul 27 '23 at 10:19
  • @mklement0 Sir - do you have any other idea if I can iterate over this type of datastructure – Money Times Jul 27 '23 at 10:24
  • @MoneyTimes, this means that the no timestamp string is present where the code expects it, which means that either `null` is present instead in the JSON input or, as noted, that structure of the JSON data isn't what you expect. If you want help, paste the JSON result (multi-line string) of your `aws` call _verbatim_ into your question (replace any sensitive values with dummy ones), and include the error message you get when you run the code against it. – mklement0 Jul 27 '23 at 12:32

0 Answers0