Split-SPOItemURL.ps1

##############################
#.SYNOPSIS
#Splits a SharePoint URL for an item into its component parts.
#
#.DESCRIPTION
#Long description
#
#.PARAMETER URL
#$URL - the URL to Split
#
#.PARAMETER Cred
#$Cred - The credential to use for testing.
#
#.EXAMPLE
#$items = Get-SPOListItems -List $Context.Web.Lists[5]
#$items = Get-SPOListItems -List "Audit Logs" -Context $context
#
#.NOTES
#Used to break a part an Item URL to locate the full site URL, the name of the document library and the name of the item
##############################

Function Split-SPOItemURL{
    Param(
        [CmdletBinding()]
        [Parameter(Mandatory=$true,ValueFromPipeline)]
        [String]$url,

        [Parameter(Mandatory=$true)]
        [PsCredential]$Cred
    )
    $URLSplit = $url -split "/"
    $FileName = $URLSplit[($URLSplit.Count - 1)]
    $CollectionURL = ($URLSplit[0..4] -join "`/")
    $URLSplit = $URLSplit[5..($URLSplit.Count -2)]
    $SiteURL = $CollectionUrl

    If ($URLSplit.Count -ne 1){

        #$UrlNotComplete = $true
        #While($UrlNotComplete){
        For($i=0; $i -lt $URLSplit.Count; $i++){
            $ctx = Connect-SPOCSOM -credential $Cred -SiteURL ($SiteUrl + "/" + $URLSplit[$i])
            Try{
                Get-SPOWeb -Context $ctx | Out-Null
                $SiteURL += "/" + $URLSplit[$i]
                Disconnect-SPOCSOM -contexts $ctx
               }
            Catch{
                break;
            }
        }
        $DocLibName = $URLSplit[$i]
    }
    Else{
        $DocLibName = $URLSplit
    }
    
    Return @($SiteURL,$DocLibName,$FileName)
}