3
3
// ------------------------------------------------------------------------------
4
4
namespace Microsoft . Graph . PowerShell . Cmdlets . Custom
5
5
{
6
+ using System ;
7
+ using System . Management . Automation ;
6
8
7
9
public partial class ListCmdlet : global ::System . Management . Automation . PSCmdlet
8
10
{
9
- /// <summary>Backing field for <see cref="All " /> property.</summary>
10
- private global :: System . Management . Automation . SwitchParameter _all ;
11
-
12
- /// <summary>List All pages </summary>
13
- [ global ::System . Management . Automation . Parameter ( Mandatory = false , HelpMessage = "List all pages " ) ]
11
+ /// <summary>Backing field for <see cref="PageSize " /> property.</summary>
12
+ private int _pageSize ;
13
+
14
+ /// <summary>Sets the page size of results. </summary>
15
+ [ global ::System . Management . Automation . Parameter ( Mandatory = false , HelpMessage = "Sets the page size of results. " ) ]
14
16
[ Microsoft . Graph . PowerShell . Runtime . Info (
15
17
Required = false ,
16
18
ReadOnly = false ,
17
- Description = @"List all pages" ,
18
- SerializedName = @"$all" ,
19
+ Description = @"The page size of results." ,
19
20
PossibleTypes = new [ ] { typeof ( global ::System . Management . Automation . SwitchParameter ) } ) ]
20
21
[ global ::Microsoft . Graph . PowerShell . Category ( global ::Microsoft . Graph . PowerShell . ParameterCategory . Runtime ) ]
21
- public global :: System . Management . Automation . SwitchParameter All { get => this . _all ; set => this . _all = value ; }
22
+ public int PageSize { get => this . _pageSize ; set => this . _pageSize = value ; }
22
23
23
24
/// <summary>
24
25
/// Default number of items per page.
@@ -30,11 +31,6 @@ public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
30
31
/// </summary>
31
32
internal const int MaxPageSize = 999 ;
32
33
33
- /// <summary>
34
- /// Original page size/top/limit passed to Cmdlet via parameter.
35
- /// </summary>
36
- internal int originalPageSize = 0 ;
37
-
38
34
/// <summary>
39
35
/// Total number of pages required to iterate page collections excluding overflow items.
40
36
/// </summary>
@@ -56,26 +52,47 @@ public partial class ListCmdlet : global::System.Management.Automation.PSCmdlet
56
52
/// </summary>
57
53
internal int totalFetchedItems = 0 ;
58
54
55
+ /// <summary>
56
+ /// Total number of items to be fetched.
57
+ /// </summary>
58
+ internal int limit = default ;
59
+
59
60
/// <summary>
60
61
/// Initializes paging values.
61
62
/// </summary>
62
63
/// <param name="invocationInfo">A reference to <see cref="System.Management.Automation.InvocationInfo"/> object.</param>
63
- /// <param name="PageSize ">A reference to page size parameter.</param>
64
- public void InitializePaging ( ref global ::System . Management . Automation . InvocationInfo invocationInfo , ref int PageSize )
64
+ /// <param name="top ">A reference to top parameter.</param>
65
+ public void InitializePaging ( ref global ::System . Management . Automation . InvocationInfo invocationInfo , ref int top )
65
66
{
66
- if ( PageSize > MaxPageSize )
67
+ if ( invocationInfo . BoundParameters . ContainsKey ( "PageSize" ) && ( PageSize > MaxPageSize || PageSize == default ) )
68
+ {
69
+ ThrowTerminatingError (
70
+ new ErrorRecord (
71
+ new ArgumentException ( $ "Invalid page size specified `{ PageSize } `. { nameof ( PageSize ) } must be between 1 and { MaxPageSize } inclusive.") ,
72
+ Guid . NewGuid ( ) . ToString ( ) ,
73
+ ErrorCategory . InvalidArgument ,
74
+ null ) ) ;
75
+ }
76
+
77
+ // Move `-Top` parameter to `limit` parameter.
78
+ if ( invocationInfo . BoundParameters . ContainsKey ( "Top" ) )
67
79
{
68
- invocationInfo . BoundParameters [ "All" ] = true ;
69
- originalPageSize = PageSize ;
70
- requiredPages = PageSize / DefaultPageSize ;
71
- overflowItemsCount = PageSize % DefaultPageSize ;
72
- invocationInfo . BoundParameters [ "PageSize" ] = DefaultPageSize ;
73
- PageSize = DefaultPageSize ;
80
+ limit = top ;
81
+ }
82
+
83
+ // Explicitly set `-PageSize` as our $top parameter.
84
+ invocationInfo . BoundParameters [ "Top" ] = invocationInfo . BoundParameters . ContainsKey ( "PageSize" ) ? PageSize : DefaultPageSize ;
85
+ top = ( int ) invocationInfo . BoundParameters [ "Top" ] ;
86
+
87
+ if ( limit != default )
88
+ {
89
+ requiredPages = limit / top ;
90
+ overflowItemsCount = limit % top ;
74
91
}
75
92
}
76
93
77
94
/// <summary>
78
- /// Determines whether the cmdlet should follow the OData next link url .
95
+ /// Determines whether the cmdlet should follow the OData next link URI .
79
96
/// </summary>
80
97
/// <param name="boundParameters">The bound parameters of the cmdlet.</param>
81
98
/// <param name="itemsCount">Current page items count.</param>
@@ -84,15 +101,8 @@ public bool ShouldIteratePages(global::System.Collections.Generic.Dictionary<str
84
101
{
85
102
iteratedPages ++ ;
86
103
totalFetchedItems += itemsCount ;
87
- if ( boundParameters . ContainsKey ( "All" ) ||
88
- ( boundParameters . ContainsKey ( "PageSize" ) && totalFetchedItems < originalPageSize ) )
89
- {
90
- return true ;
91
- }
92
- else
93
- {
94
- return false ;
95
- }
104
+
105
+ return limit == default || totalFetchedItems < limit ;
96
106
}
97
107
98
108
/// <summary>
0 commit comments