There is a PowerShell cmdlet Invoke-MgSubscribeGroup
that I want to call this way:
Invoke-MgSubscribeGroup -GroupId da2d17a7-64a5-43e5-9d95-7b70333dd78c
@{ UserId = "ed3d927d-7999-459f-955d-2afc272bd4d4" }
(Split into multiple lines for better readability)
When calling it, I get en error message:
A positional parameter cannot be found that accepts argument "System.Collections.Hashtable".
Since I'm not that deep into PowerShell I must have some false understanding of how to pass a hashtable, or I have misunderstood the documentation that says:
...To create the parameters described below, construct a hash table containing the appropriate properties...
My question
What is the correct syntax to call the Invoke-MgSubscribeGroup
cmdlet and pass the user ID?
CodePudding user response:
To elaborate on your own answer:
PowerShell's syntax diagrams - available locally via Invoke-MgSubscribeGroup -?
or Get-Command -Syntax Invoke-MgSubscribeGroup
- contain all the relevant information.
That said, they're not easy to read, especially locally, and there's room for future improvement.
Quoting from Invoke-MgSubscribeGroup
's documentation:
Invoke-MgSubscribeGroup
-GroupId <String>
# ...
Invoke-MgSubscribeGroup
-InputObject <IGroupsIdentity>
# ...
Each (partial) quote represents a distinct parameter set, i.e. a unique combination of parameters representing a distinct feature group.
That
-GroupId
and-InputObject
are in different parameter sets and are exclusive to each, tells you that you cannot use them both in a given invocation (they way you mistakenly tried), i.e., that they are mutually exclusive.Additionally, given that the parameter names
-GroupId
and-InputObject
are not enclosed in[...]
means that you can only pass named, not positional arguments to them - that is, you must precede an argument to bind to these parameters with the parameter name; e.g,-GroupId foo
rather than justfoo
.By convention, a parameter named
-InputObject
is typically used to represent values that can be supplied via the pipeline, as evidenced by the parameter's description statingAccept pipeline input: True
- locally, you can see this with eitherGet-Help -Full Invoke-MgSubscribeGroup
or - parameter-specifically - withGet-Help Invoke-MgSubscribeGroup -Parameter InputObject
Often, multiple input objects can only (meaningfully) be supplied via the pipeline; that is,
-InputObject
is often a mere implementation detail whose purpose is to facilitate pipeline input - see GitHub issue #4242.GitHub issue #4135 proposes making syntax diagrams directly reflect which parameters accept pipeline input.
What complicates matters with respect to Invoke-MgSubscribeGroup
's documentation, specifically (which seems to be very sparse in general):
The help topic contains no examples (which you could normally request locally with
Get-Help -Examples Invoke-MgSubscribeGroup
)The data type of the
-InputObject
parameter,<IGroupsIdentity>
(Microsoft.Graph.PowerShell.Models.IGroupsIdentity
) doesn't seem to have its own documentation; it is only described in the "Notes" section of the help topic, as accepting a hashtable (@{ ... }
), along with a list of the supported entries (keys and value types).
All that said: you could have passed your hashtable via the pipeline, as follows:
@{
UserId = 'ed3d927d-7999-459f-955d-2afc272bd4d4'
GroupId = 'da2d17a7-64a5-43e5-9d95-7b70333dd78c'
} | Invoke-MgSubscribeGroup
The advantage of this approach over passing an argument to -InputObject
is that it would allow you to pass multiple hashtables to act on.
CodePudding user response:
After further investigating and reading the documentation again and again, I've found the correct syntax:
Invoke-MgSubscribeGroup -InputObject @{
UserId = "ed3d927d-7999-459f-955d-2afc272bd4d4";
GroupId = "da2d17a7-64a5-43e5-9d95-7b70333dd78c" }
(Split into multiple lines for better readability)
I now face permission issues, but this is out-of-scope for this question, since I've asked for the correct syntax only.