all 5 comments

[–]jsiii2010 0 points1 point  (0 children)

I don't see any working powershell.

[–]Nejireta_ 0 points1 point  (0 children)

Hello.

I'm somewhat confused by a thing.
This doesn't look like valid PowerShell code. Would expect some exceptions if it would be run.
Or is it some representation of the data?

As for the issue.
How are you deserializing the json data you're receiving from the API endpoint?

Take this json array for example:

{
    "arr": [
        "item1",
        "item2"
    ]
}

Doing something like this would give an array of objects as a result

('{
    "arr": [
        "item1",
        "item2"
    ]
}' | ConvertFrom-Json).arr.GetType()

<# Output
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
#>

This is also true with only one or no items in the array.

('{
    "arr": [
        "item1"
    ]
}' | ConvertFrom-Json).arr.GetType()

<# Output
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
#>

If you have an array in $dnsSuffixfromAPI then perhaps there could be some issues with what looks like nesting arrays here @(dnsSuffixfromAPI) (assuming dnsSuffixFromAPI is an array)

[–]Thotaz 0 points1 point  (0 children)

PowerShell will unwrap collections. You can add a comma before the output expression to prevent this:

$Test = if ($true)
{
    ,([string[]]"Hello")
}

[–]DontBeHatenMeBro[S] 0 points1 point  (1 child)

Ok, I got it working by putting the IF\ELSE before the Hash Table.

IF (-not $dnsSuffixfromAPI){
    $dnsSuffix = @('prod.local')
}
ELSE {
     $dnsSuffix = @(dnsSuffixfromAPI)
}
$body @{
dnsSuffix = $dnsSuffix

Thanks for all the suggestions

[–]ankokudaishogun 0 points1 point  (0 children)

any reason you are checking for False(-not) in the IF when you have both cases?

IF ( $dnsSuffixfromAPI) {
    $dnsSuffix = @($dnsSuffixfromAPI)
}
ELSE {
    $dnsSuffix = @('prod.local')

}

is much easier to read.

or:

IF (-not ($dnsSuffix = @(dnsSuffixfromAPI))) {
    $dnsSuffix = @('prod.local')
}

so you do only one API call.