Skip to content

Add better support for -count parameter #152

Closed

Description

@peombwa

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

Activity

  1. added this to the 1.0 GA milestone on Mar 25, 2020
  2. KirkMunro commented on Jun 20, 2020

    @KirkMunro

    When fixing this issue, I have two suggested approaches:

    1. 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.

    2. 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:

      Get-MgUser [-ExpandProperty <string[]>] [-Property <string[]>] [-Filter <string>] [-PageSize <int>] [-Search <string>] [-Skip <int>] [-Sort <string[]>] [-Break] [-HttpPipelineAppend <SendAsyncStep[]>] [-HttpPipelinePrepend <SendAsyncStep[]>] [-Proxy <uri>] [-ProxyCredential <pscredential>] [-ProxyUseDefaultCredentials] [-All] [<CommonParameters>]
      
      Get-MgUser -Count [-Filter <string>] [-Search <string>] [-HttpPipelineAppend <SendAsyncStep[]>] [-HttpPipelinePrepend <SendAsyncStep[]>] [-Proxy <uri>] [-ProxyCredential <pscredential>] [-ProxyUseDefaultCredentials] [-All] [<CommonParameters>]
      
      Get-MgUser -UserId <string> [-ExpandProperty <string[]>] [-Property <string[]>] [-Break] [-HttpPipelineAppend <SendAsyncStep[]>] [-HttpPipelinePrepend <SendAsyncStep[]>] [-Proxy <uri>] [-ProxyCredential <pscredential>] [-ProxyUseDefaultCredentials] [<CommonParameters>]
      
      Get-MgUser -InputObject <IUsersUserIdentity> [-ExpandProperty <string[]>] [-Property <string[]>] [-Break] [-HttpPipelineAppend <SendAsyncStep[]>] [-HttpPipelinePrepend <SendAsyncStep[]>] [-Proxy <uri>] [-ProxyCredential <pscredential>] [-ProxyUseDefaultCredentials] [<CommonParameters>]
      

    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.

  3. ryanspletzer commented on Aug 1, 2020

    @ryanspletzer

    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 consistencylevel to eventual.

    image

    Then you can do the following:

    https://graph.microsoft.com/beta/users/$count

    This response returns a single integer in the body:

    image

    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=true

    image

    https://graph.microsoft.com/beta/users?$filter=startsWith(displayName, 'Ryan')&$count=true

    image

    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.)

  4. peombwa commented on Aug 27, 2020

    @peombwa
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions