Private/Initialize-Folder.ps1

function Initialize-Folder {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [VSphereConnectorConfiguration]$Config,

        [Parameter(Mandatory = $true)]
        [string]$Path
    )

    $ScriptRootPath = $Config.ScriptRootPath
    $vSphereEnvironment = $Config.EnvironmentConfig.Name

    $targetPath = Join-Path -Path $ScriptRootPath -ChildPath (Join-Path -Path $Path -ChildPath $vSphereEnvironment)

    if (-not (Test-Path -Path $targetPath)) {
        New-Item -Path $targetPath -ItemType Directory -Force -ErrorAction Stop | Out-Null
        Write-CustomLog -Message "Created directory: $targetPath" -Severity 'INFO'
    }

    # Verify write access
    try {
        $tempFile = Join-Path -Path $targetPath -ChildPath "write-access-check_$(New-Guid).tmp"
        New-Item -Path $tempFile -ItemType File -ErrorAction Stop | Out-Null
        Remove-Item -Path $tempFile -ErrorAction Stop
    }
    catch {
        throw "No write access to directory: $targetPath."
    }

    return $targetPath
}