Public/Set-LogDirectory.ps1

function Set-LogDirectory
{
    <#
        .DESCRIPTION
            Set the directory where the log file will be created
         
        .PARAMETER ScriptRoot
            Specify the root directory the parent script is called from
 
        .EXAMPLE
            Set-LogDirectory -ScriptRoot $PSScriptRoot
 
        .NOTES
            Created by: Jon Anderson
            Modified: 2023-07-10
    #>

    [CmdletBinding()]
    param(   
        [parameter(Mandatory=$true)][ValidateNotNullOrEmpty()]
        [String]$ScriptRoot
    )

    $ConfigMgrPath = "$ENV:WINDIR\CCM\Logs"
    $IntunePath = "$ENV:ALLUSERSPROFILE\Microsoft\IntuneManagementExtension\Logs"
    $DefaultPath = "$ENV:ALLUSERSPROFILE\Win32AppManagement"
    if(($ScriptRoot -match "ccmcache") -and (Test-Path $ConfigMgrPath))
    {
        return $ConfigMgrPath
    }
    elseif(($ScriptRoot -match "IMECache") -and (Test-Path $IntunePath))
    {
        return $IntunePath
    }
    else
    {
        if(!(Test-Path $DefaultPath))
        {
            try
            {
                New-Item -Path $DefaultPath -ItemType "Directory" -Force -ErrorAction Stop | Out-Null
            }
            catch
            {
                throw "Failed to create the log directory: $($PSItem.Exception.Message)"
            }   
        }
        return $DefaultPath
    }
}