Get-SPOListItems.ps1

##############################
#.SYNOPSIS
#Gets all items from a list in SharePont Online.
#
#.DESCRIPTION
#Long description
#
#.PARAMETER List
#$List - The list to return items from. This can be a string or the actual list object(Mandatory)
#
#.PARAMETER context
#The context to load the querys to and execute. (Optional)
#
#.PARAMETER MaxiumItems
#Unused right now.
#
#.EXAMPLE
#$items = Get-SPOListItems -List $Context.Web.Lists[5]
#$items = Get-SPOListItems -List "Audit Logs" -Context $context
#
#.NOTES
#Uses a basic caml query to retrieve the items.
#This should be expanded to support specific Web objects as opposed to Context objects.
#Expand to allow for List ID
##############################
function Get-SPOListItems {
    [CmdletBinding()]
    Param(

        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ParameterSetName="ListObject")]
        [Microsoft.SharePoint.Client.List[]]
        $List,

        [Parameter(Mandatory=$true,ValueFromPipeline=$False,ParameterSetName="Web")]
        [Parameter(Mandatory=$true,ValueFromPipeline=$False,ParameterSetName="Context")]
        [String[]]
        $ListName,

        [Parameter(Mandatory=$true,ParameterSetName="Context")]
        #[Parameter(Mandatory=$false,ParameterSetName="ListObject")]
        [Microsoft.SharePoint.Client.ClientRuntimeContext]
        $Context = $Global:SPOCSOMContext,

        [Parameter(Mandatory=$true,ParameterSetName="Web")]
        #[Parameter(Mandatory=$false,ParameterSetName="ListObject")]
        [Microsoft.SharePoint.Client.Web]
        $Web

        #[Parameter(Mandatory=$false,ParameterSetName="ListName")]
        #[Parameter(Mandatory=$false,ParameterSetName="ListObject")]
        #[int]$MaxiumItems = 100
    )

    Begin{


        [Microsoft.SharePoint.Client.ListItem[]]$results = @()

        #Create recursive CAML Query to return all items.
        Write-Verbose -Message "Creating Recursive CAML Query to return all items."
        $CAMLQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
        $CAMLQuery.ViewXml = "
            <View Scope = 'RecursiveAll'>
                <Query>
                    <OrderBy>
                        <FieldRef Name='FileRef' />
                    </OrderBy>
                </Query>
                <RowLimit>5000</RowLimit>
            </View>
        "

        [Microsoft.SharePoint.Client.ListItemCollectionPosition]$CAMLQueryPosition = $null
    }

    Process{
        
        if($Context){
            $web = $Context.Web
        }
        
        #If supplied listname was a string, ty to get it by the name. If not, it must be a list object.
        Try{
            if($ListName){
                Write-Verbose -Message "List name supplied. Attempting to get list by name."
                [Microsoft.SharePoint.Client.List[]]$List = @()
                foreach($ln in $ListName){
                    $List += $Web.Lists.GetByTitle($ln)
                }
            }
            else{
                Write-Verbose -Message "List was an object."
            }
        }
        Catch{
            Throw "Could not get List $ln"
            break
        }

        Foreach($l in $List){
            #Load the CAML Query to the list GetItems function.
            do{
                Write-Verbose -Message "Loading CAML Query into the GetItems function"
                $CAMLQuery.ListItemCollectionPosition = $CAMLQueryPosition
                $Items = $l.GetItems($CAMLQuery)
                #initialize the list object after CAML query is loaded.
                Write-Verbose -Message "Getting Items from Context."
                $results += Initialize-SPOCSOMCollections -CSOMCollection $Items -ReturnObject
                $CAMLQueryPosition = $Items.ListItemCollectionPosition
                #Write-host $CAMLQueryPosition.PagingInfo
                
            }
            While($null -ne $CAMLQueryPosition)
        }
        if($results){
            Initialize-SPOCSOMObjectProperty -Objects $results -PropertyName @("DisplayName","FileSystemObjectType","HasUniqueRoleAssignments")
            Initialize-SPOCSOMCollections -CSOMObjects $results -CollectionName "ParentList"
            #Items are missing a significant amount of data. This adds it back to it.
            Foreach($r in $results){
                $r | Add-Member -MemberType NoteProperty -Name "Parent Web URL" -Value $r.ParentList.ParentWebUrl
                $r | Add-Member -MemberType NoteProperty -Name "ParentList Title" -Value $r.ParentList.Title
            }
        }

            return ,$results
    }

    End{
    }
}