Get-SPOSubWebs.ps1

##############################
#.SYNOPSIS
#Returns only the immediate subsites for a given web or context.
#
#.DESCRIPTION
#Returns only the immediate subsites for a given web or context.
#
#.PARAMETER Context
#he context to start at. The root site for lack of a better term.
#
#.PARAMETER web
#The web to start at.
#
#.PARAMETER MinusAppDomains
#Do not include sites in the return that are part of an application.
#
#.EXAMPLE
#$subWebs = Get-SPOSubWebs -web $web
#
#.NOTES
#Needs to be cleaned up.
##############################
Function Get-SPOSubWebs{
    [CmdletBinding()]
    Param(
    [Parameter(ParameterSetName="Context",Mandatory=$true,ValueFromPipeline)]
    [Microsoft.SharePoint.Client.ClientRuntimeContext[]]
    $Context = $Global:SPOCSOMContext,

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

    [Parameter(ParameterSetName="web")]
    [Parameter(ParameterSetName="Context")]
    [Switch]
    $MinusAppDomains

    )

    Begin{
        [Microsoft.SharePoint.Client.Web[]]$result=@()
        $regex = "https:\/\/\w+\.sharepoint\.com"
    }
    Process{

        #Use the context and return sub sites for the root web object.
        If(-not $web){
            $result = Initialize-SPOCSOMCollections -CSOMCollection $Context.Web.webs -ReturnObject
        }

        #Use the supplied web object and return the sub sites.
        Else{
            $result = Initialize-SPOCSOMCollections -CSOMCollection $web.Webs -ReturnObject
        }

    }
    End{

        #Return the subsites. Remove AppDomains if requested.
        if($result){
            $result | Initialize-SPOCSOMObjectProperty -PropertyName "HasUniqueRoleAssignMents"
        }
        if($MinusAppDomains){
            return ,($result | Where-Object {$_.Url -match $regex})
        }
        else{
            return ,$result
        }
    
    }
}