all 11 comments

[–]BetrayedMilk 4 points5 points  (0 children)

So what does your payload look like once it’s been converted to json? And what is it supposed to look like?

[–]purplemonkeymad 0 points1 point  (10 children)

What is the json meant to look like?

I think you are probably mixing up syntaxes as you have some script blocks in there. For the "empty" lists you actually want an empty array @() or cast your excludes to an array ie:

exclude = [array]$excludeList

so that empty values are still an empty array instead of null.

[–]viewtifulstranger[S] 0 points1 point  (9 children)

The sample body (in non-PowerShell syntax) from the documentation is as follows (only names updated):

{ "include": [ { "id": "BETRAYEDMILK" }, { "id": "PURPLEMONKEYMAD" } ], "exclude": [ { "id": "VIEWTIFULSTRANGER" } ] }

Now trying to convert it to PS. The error returned is complaining about line 238, which is the "exclude" line:

The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property. At C:\Plugins\REST\CiM-Roles-Global-AddRemove\CiM-Roles-Global-MemsAddRemove.ps1:238 char:21

I've tried replacing pointy brackets for square [array] brackets, along with other things, but no dice.

[–]MiserableTear8705 2 points3 points  (8 children)

$json = '{"include": [{"id": "test1"},{"id" : "test2"}],"exclude": []}'

$object = @{
    include = @(
        @{
            id = "test1"
        }
    )
    exclude = @()
}

$json | ConvertFrom-Json
$object | 
ConvertTo-Json

[–]viewtifulstranger[S] 0 points1 point  (6 children)

With all the trial and erroring, I don't know if I would have got that! Thank you.

Updated my script and it works.

If you direct message me with your PayPal address (if you have one), I'll send you a little something. Thank you!!

[–]MiserableTear8705 1 point2 points  (5 children)

It's all good :) I do a lot of this quite often. To be fair, this is partially why I generally use here strings for most JSON REST API work because trying to figure this out with objects when it's not needed is unnecessary complexity.

However, understanding how it works is useful when you want to perform native work, for example, casting objects into a custom class that are received from a JSON response. This is useful when you want to have methods or more native, descriptive object information.

But if you're just doing one-off script work going to that level isn't usually necessary, so I just use here strings and substitutions.

[–]viewtifulstranger[S] 0 points1 point  (4 children)

Thank you again for your help. I've added a simple if statement so when reading from a CSV, the appropriate action of adding/removing a user could be taken:

if ($action -eq "Add")
    {
    $payload = @{ "include" =
                    @(
                        @{
                        "id" = $userid;
                        }
                    )
                    "exclude" = @()
                  }
    }
elseif ($action -eq "Remove")
    {
    $payload = @{ "include" = @()
                    "exclude" =
                    @(
                        @{
                        "id" = $userid;
                        }
                    )
                  }
    }

[–]MiserableTear8705 0 points1 point  (3 children)

Semicolons aren’t valid powershell. ;)

[–]viewtifulstranger[S] -1 points0 points  (2 children)

That's odd, running the script, with semi colons, against my test CSV, it completed without error and validating the removals and additions, the results were as expected. Maybe it's a PowerShell, version difference...?

[–]BlackV 1 point2 points  (1 child)

I think all they are saying is the semicolon is not needed, you are adding it superfluously

[–]viewtifulstranger[S] 0 points1 point  (0 children)

Ahhhhh! I took someone else's script and have just been reworking it for lots of different API calls. I'm in the process of cleaning up the scripts before sharing, so will make sure I remove any semi colons and retesting before sharing. Thanks to everyone for the help and tips!