Private/Invoke-MgWindowsUpdatesRequest.ps1
|
function Invoke-MgWindowsUpdatesRequest { [CmdletBinding()] param( [Parameter(Mandatory)] [ValidateSet('GET', 'POST', 'PATCH', 'DELETE', 'PUT')] [string] $Method, [Parameter(Mandatory)] [string] $Path, [AllowNull()] [object] $Body, [Collections.IDictionary] $Query, [Collections.IDictionary] $Headers, [switch] $All ) if ($All -and $Method -ne 'GET') { throw '-All is supported only for GET requests.' } $requestHeaders = @{} if ($Headers) { foreach ($entry in $Headers.GetEnumerator()) { $requestHeaders[$entry.Key] = $entry.Value } } if ($Query -and (($Query.Keys -contains '$count') -or ($Query.Keys -contains '$search')) -and -not $requestHeaders.ContainsKey('ConsistencyLevel')) { $requestHeaders.ConsistencyLevel = 'eventual' } $context = Assert-MgWindowsUpdatesGraphContext if (-not $context.Environment) { throw 'The active Microsoft Graph context does not identify its environment.' } $environment = Get-MgEnvironment -Name $context.Environment -ErrorAction Stop if (-not $environment.GraphEndpoint) { throw ( "Microsoft Graph environment '$($context.Environment)' does not define a Graph endpoint." ) } $graphEndpoint = [uri] $environment.GraphEndpoint $uri = ConvertTo-MgWindowsUpdatesRequestUri ` -Path $Path ` -Query $Query ` -GraphEndpoint $graphEndpoint $routePath = ([uri] $uri).AbsolutePath $expectedContext = @{ TenantId = [string] ( Get-MgWindowsUpdatesProperty -InputObject $context -Name 'TenantId' ) Environment = [string] ( Get-MgWindowsUpdatesProperty -InputObject $context -Name 'Environment' ) AuthType = [string] ( Get-MgWindowsUpdatesProperty -InputObject $context -Name 'AuthType' ) Scopes = @( Get-MgWindowsUpdatesProperty -InputObject $context -Name 'Scopes' | Sort-Object ) } $assertRequestContext = { $currentContext = Assert-MgWindowsUpdatesGraphContext $currentTenantId = [string] ( Get-MgWindowsUpdatesProperty -InputObject $currentContext -Name 'TenantId' ) $currentEnvironment = [string] ( Get-MgWindowsUpdatesProperty -InputObject $currentContext -Name 'Environment' ) $currentAuthType = [string] ( Get-MgWindowsUpdatesProperty -InputObject $currentContext -Name 'AuthType' ) $currentScopes = @( Get-MgWindowsUpdatesProperty -InputObject $currentContext -Name 'Scopes' | Sort-Object ) if ($currentTenantId -ne $expectedContext.TenantId -or $currentEnvironment -ne $expectedContext.Environment -or $currentAuthType -ne $expectedContext.AuthType -or (Compare-Object $expectedContext.Scopes $currentScopes)) { throw ( 'The active Microsoft Graph context changed while the request was in progress. ' + 'No further requests were sent.' ) } if ($routePath -match '^/v1\.0/deviceManagement/reports/exportJobs(?:/|$)') { $null = Assert-MgWindowsUpdatesGraphContext ` -Context $currentContext ` -RequiredScope 'DeviceManagementConfiguration.ReadWrite.All' } elseif ($routePath -match '^/beta/admin/windows/updates(?:/|$)') { if ($Method -eq 'GET') { $null = Assert-MgWindowsUpdatesGraphContext ` -Context $currentContext ` -AnyScope @( 'WindowsUpdates.Read.All' 'WindowsUpdates.ReadWrite.All' ) } else { $null = Assert-MgWindowsUpdatesGraphContext ` -Context $currentContext ` -RequiredScope 'WindowsUpdates.ReadWrite.All' } } else { $null = Assert-MgWindowsUpdatesGraphContext -Context $currentContext } } do { & $assertRequestContext $arguments = @{ Method = $Method Uri = $uri ErrorAction = 'Stop' } if ($null -ne $Body) { $arguments.Body = $Body $arguments.ContentType = 'application/json' } if ($requestHeaders.Count -gt 0) { $arguments.Headers = $requestHeaders } $response = Invoke-MgGraphRequest @arguments if ($null -eq $response) { return } $hasValueProperty = if ($response -is [Collections.IDictionary]) { $response.Contains('value') } else { $null -ne $response.PSObject.Properties['value'] } $value = Get-MgWindowsUpdatesProperty -InputObject $response -Name 'value' if ($hasValueProperty) { foreach ($item in @($value)) { Write-Output $item } } elseif ($null -ne $response) { Write-Output $response } $nextLink = if ($All) { Get-MgWindowsUpdatesProperty -InputObject $response -Name '@odata.nextLink' } else { $null } $uri = if ($nextLink) { ConvertTo-MgWindowsUpdatesRequestUri ` -Path $nextLink ` -GraphEndpoint $graphEndpoint } else { $null } $Body = $null } while ($uri) } |