You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, using -count parameter results in an invalid count option.
Repro
Get-MgUserMessage -UserId {user-id} -Count
Get-MgUserMessage_List: 'True' is not a valid count option.
Cause
The service expects the $count value to be lowercase true, yet the generated client sends a pascal case True. AB#5873
When fixing this issue, I have two suggested approaches:
Change from the currently broken -Count switch parameter to -CountVariable, and make it work like PowerShell's common -*Variable parameters.
In PowerShell, I can capture the output of a command using the -OutVariable command. For example, this command will result in my Windows Update service being stored in a variable called wu:
Get-Service wuauserv -OutVariable wu
If the wu variable does not exist already, it gets created, and if the variable exists, its value gets overwritten. In both cases, I end up with the Windows Update service object stored in a wu variable.
With Graph, the count usually comes back as one piece of metadata along with whatever you wanted to retrieve, but that doesn't work very well with PowerShell where we're simply calling a command to retrieve data. Having a -CountVariable common parameter for Graph cmdlets that support the retrieval of a count could make this easier, internally passing through the properly cased count=true query string value if it is used, and returning the count in the variable that was passed in. Here is an example showing how that might be used:
Get-MgUser-CountVariable userCount -PageSize 1$userCount# Contains the number of users in the tenant
You may want to consider setting a variable alias of cv for that variable as well so that it is easy to use and follows a similar convention established by ov for OutVariable and pv for PipelineVariable.
Since you handle paging automatically within a cmdlet, I personally don't see much value in returning any data when I want to know the number of records that would be returned. Graph doesn't seem to have a very eloquent way to retrieve just the count though, but you can always combine count=true with top=1, and then throw away the first item. If you were to take that approach, you could have a -Count switch variable on a separate parameter set so that you could simply retrieve the count of something.
For example, I could get a count of users in whatever tenant I have connect to by simply invoking Get-MgUser -Count. That cmdlet would retrieve an integer. Similarly, I could invoke Get-MgGroup -Filter 'resourceProvisioningOptions/Any(x:x eq ''Team'')' -Count to get a count of the number of Teams I have in my organization. It makes sense to have -Count paired with -Filter (and -Search) so that you can count filtered results, but -Count has questionable value when paired with -Property, -PageSize, -Skip, -Sort, and -ExpandProperty.
If you were to go this approach, your Get-MgUser cmdlet parameter sets could be redefined. In the output below, -Count has been removed from the first parameter set and a new parameter set has been added as the second entry, which is only for retrieval of the count of a data set produced by a query:
Of these two possibilities, I prefer the second option because it allows me to simply request a count without having to manipulate the number of results that are actually returned, and the cmdlet takes care of how to get that count value for me.
One has to set an additional request header of consistencylevel to eventual.
Then you can do the following:
https://graph.microsoft.com/beta/users/$count
This response returns a single integer in the body:
You can also do the following to get a count that is in line with the results of a list query, so this, in theory, would show you the total number of items in the total paginated set, based your current query, couple of examples:
I think that the straight count of an entire collection, e.g. /users/$count, would be better served by a separate parameter set that only requires a single -Count switch parameter, and that -CountVariable would work best with the existing List parameter set, to capture the total count of the set of results of the query you performed. (Keeping in mind that any sort of count today still relies on the beta Graph endpoint.)
We will go with the -CountVariable for now. The variable will be set as a global variable since everything is exported as functions.
We currently can't support raw counts (/users/$count) since the schema only describes $count as a parameter. One can use Invoke-GraphRequest to get a raw count in the interim.
Activity
KirkMunro commentedon Jun 20, 2020
When fixing this issue, I have two suggested approaches:
Change from the currently broken
-Countswitch parameter to-CountVariable, and make it work like PowerShell's common-*Variableparameters.In PowerShell, I can capture the output of a command using the
-OutVariablecommand. For example, this command will result in my Windows Update service being stored in a variable calledwu:Get-Service wuauserv -OutVariable wuIf the
wuvariable does not exist already, it gets created, and if the variable exists, its value gets overwritten. In both cases, I end up with the Windows Update service object stored in awuvariable.With Graph, the count usually comes back as one piece of metadata along with whatever you wanted to retrieve, but that doesn't work very well with PowerShell where we're simply calling a command to retrieve data. Having a
-CountVariablecommon parameter for Graph cmdlets that support the retrieval of a count could make this easier, internally passing through the properly casedcount=truequery string value if it is used, and returning the count in the variable that was passed in. Here is an example showing how that might be used:You may want to consider setting a variable alias of
cvfor that variable as well so that it is easy to use and follows a similar convention established byovforOutVariableandpvforPipelineVariable.Since you handle paging automatically within a cmdlet, I personally don't see much value in returning any data when I want to know the number of records that would be returned. Graph doesn't seem to have a very eloquent way to retrieve just the count though, but you can always combine
count=truewithtop=1, and then throw away the first item. If you were to take that approach, you could have a-Countswitch variable on a separate parameter set so that you could simply retrieve the count of something.For example, I could get a count of users in whatever tenant I have connect to by simply invoking
Get-MgUser -Count. That cmdlet would retrieve an integer. Similarly, I could invokeGet-MgGroup -Filter 'resourceProvisioningOptions/Any(x:x eq ''Team'')' -Countto get a count of the number of Teams I have in my organization. It makes sense to have-Countpaired with-Filter(and-Search) so that you can count filtered results, but-Counthas questionable value when paired with-Property,-PageSize,-Skip,-Sort, and-ExpandProperty.If you were to go this approach, your
Get-MgUsercmdlet parameter sets could be redefined. In the output below,-Counthas been removed from the first parameter set and a new parameter set has been added as the second entry, which is only for retrieval of the count of a data set produced by a query:Of these two possibilities, I prefer the second option because it allows me to simply request a count without having to manipulate the number of results that are actually returned, and the cmdlet takes care of how to get that count value for me.
ryanspletzer commentedon Aug 1, 2020
FWIW there is a way to get just a straight count of objects in a collection from the Graph API, at least from the beta endpoint. See:
https://developer.microsoft.com/en-us/office/blogs/build-advanced-queries-with-count-filter-search-and-orderby/
One has to set an additional request header of
consistencyleveltoeventual.Then you can do the following:
https://graph.microsoft.com/beta/users/$countThis response returns a single integer in the body:
You can also do the following to get a count that is in line with the results of a list query, so this, in theory, would show you the total number of items in the total paginated set, based your current query, couple of examples:
https://graph.microsoft.com/beta/users/?$count=truehttps://graph.microsoft.com/beta/users?$filter=startsWith(displayName, 'Ryan')&$count=trueI think that the straight count of an entire collection, e.g.
/users/$count, would be better served by a separate parameter set that only requires a single-Countswitch parameter, and that-CountVariablewould work best with the existing List parameter set, to capture the total count of the set of results of the query you performed. (Keeping in mind that any sort of count today still relies on the beta Graph endpoint.)peombwa commentedon Aug 27, 2020
We will go with the
-CountVariablefor now. The variable will be set as a global variable since everything is exported as functions.We currently can't support raw counts (
/users/$count) since the schema only describes $count as a parameter. One can useInvoke-GraphRequestto get a raw count in the interim.