private/MDT/Get-MDTDeploymentShare.ps1

function Get-MDTDeploymentShare {
    <#
    .SYNOPSIS
        Returns all MDT Deployment Shares registered on this machine.
 
    .DESCRIPTION
        Calls Get-MDTPersistentDrive to retrieve every persisted MDT deployment
        share registered on this machine and returns the full collection.
 
          - Zero shares : issues a warning and returns $null.
          - One or more : returns all drive objects as an array.
 
    .EXAMPLE
        Get-MDTDeploymentShare
 
        Returns all registered MDT deployment shares.
 
    .EXAMPLE
        $shares = Get-MDTDeploymentShare
        foreach ($share in $shares) {
            Write-Host "$($share.Name) -> $($share.Path)"
        }
 
        Iterates every registered share and displays its name and path.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.Management.Automation.PSDriveInfo[]
        Returns all registered MDT PSDrive objects, or $null if none are found.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
    #>

    [CmdletBinding()]
    [OutputType([System.Management.Automation.PSDriveInfo[]])]
    param ()

    #region PS7: bridge to Windows PowerShell 5.1 for MDT cmdlet access
    if ($PSEdition -eq 'Core') {
        $bridgeScript = @'
try {
    $installDir = $null
    try {
        $regVal = Get-ItemPropertyValue -Path 'HKLM:\SOFTWARE\Microsoft\Deployment 4' -Name 'Install Dir' -ErrorAction Stop
        $regVal = $regVal.TrimEnd('\')
        if (Test-Path -Path $regVal) { $installDir = $regVal }
    } catch {}
    if ([string]::IsNullOrEmpty($installDir)) {
        $defaultPath = 'C:\Program Files\Microsoft Deployment Toolkit'
        if (Test-Path -Path $defaultPath) { $installDir = $defaultPath }
    }
    if ([string]::IsNullOrEmpty($installDir)) { exit 1 }
    $mdtModule = Join-Path -Path $installDir -ChildPath 'bin\MicrosoftDeploymentToolkit.psd1'
    if (-not (Test-Path -LiteralPath $mdtModule)) { exit 1 }
    Import-Module -Name $mdtModule -ErrorAction Stop
    $drives = @(Get-MDTPersistentDrive -ErrorAction SilentlyContinue)
    if ($drives.Count -gt 0) { $drives | Select-Object -Property Name,Path,Description | ConvertTo-Json -Compress }
} catch { exit 1 }
'@

        Write-Verbose "[$(Get-Date -format s)] PS7: discovering MDT shares via powershell.exe bridge"
        $jsonOutput = & powershell.exe -NoProfile -NonInteractive -Command $bridgeScript 2>$null

        if ([string]::IsNullOrWhiteSpace($jsonOutput)) {
            Write-Warning "[$(Get-Date -format s)] No MDT deployment shares found. MDT may not be installed or no shares are registered."
            Write-Warning "[$(Get-Date -format s)] Use Add-MDTPersistentDrive in Windows PowerShell to register a deployment share."
            return $null
        }

        try {
            $bridgeResult = $jsonOutput | ConvertFrom-Json -ErrorAction Stop
            if ($bridgeResult -isnot [array]) { $bridgeResult = @($bridgeResult) }
        }
        catch {
            Write-Warning "[$(Get-Date -format s)] Failed to parse MDT bridge output: $_"
            return $null
        }

        Write-Verbose "[$(Get-Date -format s)] Found $($bridgeResult.Count) deployment share(s) via bridge"

        #region Persist deployment shares to OSDeployCore\cache\osdeploymdt\config.json
        $configFolder = "$env:ProgramData\OSDeployCore\cache\osdeploymdt"
        if (-not (Test-Path -Path $configFolder)) {
            New-Item -Path $configFolder -ItemType Directory -Force | Out-Null
        }

        $configPath = Join-Path -Path $configFolder -ChildPath 'config.json'
        $configObject = [ordered]@{}

        if (Test-Path -LiteralPath $configPath -PathType Leaf) {
            try {
                $rawConfig = Get-Content -LiteralPath $configPath -Raw -ErrorAction Stop
                if (-not [string]::IsNullOrWhiteSpace($rawConfig)) {
                    $existingConfig = $rawConfig | ConvertFrom-Json -ErrorAction Stop
                    foreach ($property in $existingConfig.PSObject.Properties) {
                        $configObject[$property.Name] = $property.Value
                    }
                }
            }
            catch {
                $configObject = [ordered]@{}
            }
        }

        $configObject.DeploymentShares = @(
            $bridgeResult | ForEach-Object {
                [ordered]@{ Name = $_.Name; Path = $_.Path; Description = $_.Description }
            }
        )

        try {
            $configObject | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $configPath -Encoding UTF8 -Force -ErrorAction Stop
            Write-Verbose "[$(Get-Date -format s)] Deployment shares saved to '$configPath'"
        }
        catch {
            Write-Warning "[$(Get-Date -format s)] Failed to write '$configPath': $_"
        }
        #endregion

        return $bridgeResult
    }
    #endregion

    #region Ensure MDT cmdlets are available
    if (-not (Import-MDTCmdlets)) {
        Write-Warning "[$(Get-Date -format s)] MDT cmdlets could not be loaded. Ensure MDT is installed."
        return $null
    }
    #endregion

    #region Retrieve persisted MDT drives
    $drives = @(Get-MDTPersistentDrive -ErrorAction SilentlyContinue)

    if ($drives.Count -eq 0) {
        Write-Warning "[$(Get-Date -format s)] No persisted MDT deployment shares were found."
        Write-Warning "[$(Get-Date -format s)] Use Add-MDTPersistentDrive to register a deployment share first."
        return $null
    }
    #endregion

    Write-Verbose "[$(Get-Date -format s)] Found $($drives.Count) deployment share(s)"

    #region Persist deployment shares to OSDeployCore\cache\osdeploymdt\config.json
    $configFolder = "$env:ProgramData\OSDeployCore\cache\osdeploymdt"
    if (-not (Test-Path -Path $configFolder)) {
        New-Item -Path $configFolder -ItemType Directory -Force | Out-Null
    }

    $configPath = Join-Path -Path $configFolder -ChildPath 'config.json'

    $configObject = [ordered]@{}

    if (Test-Path -LiteralPath $configPath -PathType Leaf) {
        try {
            $raw = Get-Content -LiteralPath $configPath -Raw -ErrorAction Stop
            if (-not [string]::IsNullOrWhiteSpace($raw)) {
                $existingConfig = $raw | ConvertFrom-Json -ErrorAction Stop
                foreach ($property in $existingConfig.PSObject.Properties) {
                    $configObject[$property.Name] = $property.Value
                }
            }
        }
        catch {
            Write-Warning "[$(Get-Date -format s)] Failed to parse '$configPath'. Recreating config file."
            $configObject = [ordered]@{}
        }
    }

    $configObject.DeploymentShares = @(
        $drives | ForEach-Object {
            [ordered]@{
                Name        = $_.Name
                Path        = $_.Path
                Description = $_.Description
            }
        }
    )

    try {
        $configObject | ConvertTo-Json -Depth 10 | Set-Content -LiteralPath $configPath -Encoding UTF8 -Force -ErrorAction Stop
        Write-Verbose "[$(Get-Date -format s)] Deployment shares saved to '$configPath'"
    }
    catch {
        Write-Warning "[$(Get-Date -format s)] Failed to write '$configPath': $_"
    }
    #endregion

    return $drives
}