Public/Get-specDriveMap.ps1

Function Get-specDriveMap {
    <#
    .SYNOPSIS
        Retrieves information about mapped drives using Get-SmbMapping.
 
    .DESCRIPTION
        The Get-specDriveMap function retrieves information about all mapped drives or specific mapped drives using the Get-SmbMapping cmdlet.
 
    .PARAMETER DriveLetter
        Specifies the drive letter(s) for which to retrieve mapping information.
 
        Type the drive letter(s) as a string or provide them via the pipeline.
 
    .EXAMPLE
        Get-specDriveMap
 
        Retrieves information about all mapped drives
 
    .EXAMPLE
        Get-specDriveMap -DriveLetter 'N:'
 
        Retrieves information about the mapped drive with the letter 'N:'.
 
    .EXAMPLE
        Get-specDriveMap -DriveLetter 'N:','Z:'
 
        Retrieves information about the mapped drive with the letter 'N:' and 'Z:' using serial input.
 
    .EXAMPLE
        'N:','O:' | Get-specDriveMap
 
        Retrieves information about the mapped drives with the letters 'N:' and 'O:' using pipeline input.
 
    .EXAMPLE
        $result = Get-specDriveMap
 
        if ([string]::IsNullOrEmpty($result)) {
            write-host "No drive maps found"
        } else {
            write-host "DRIVE MAPS FOUND!"
        }
 
        This example shows how you might test the functions output in a script. If no drive maps are found then the $result variable will be null or empty.
 
    .OUTPUTS
        System.Management.Automation.PSCustomObject
        Outputs a custom object with the following properties:
        - DriveLetter: The local drive letter.
        - RemotePath: The remote path to which the drive is mapped.
        - Status: The status of the drive mapping. eg. 'OK' or 'Disconnected'
        - GlobalMapping: Indicates whether the drive mapping is global or not
 
    .NOTES
        Author: owen.heaume
        Version: 1.0
    #>



    [cmdletbinding()]
    param (
        [Parameter(
            Mandatory = $false,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true,
            Position = 0)]
        [ValidateLength(1, 2)]
        [alias ('Drive')]
        [alias('RowKey')]
        [string[]]$DriveLetter
    )

    Begin {
        $driveMap = Get-SmbMapping
    }

    process {
        if ($DriveLetter) {
            foreach ($letter in $DriveLetter) {
                # if the drive letter does not end in a colon then add it
                if ($letter[-1] -ne ':') {
                    $letter = $letter + ':'
                }

                $map = $driveMap | Where-Object -Property 'LocalPath' -eq $letter

                if ($map) {
                    [pscustomobject]@{
                        DriveLetter   = $map.LocalPath
                        RemotePath    = $map.RemotePath
                        Status        = $map.status
                        GlobalMapping = $map.GlobalMapping
                    }
                } else {
                    Write-Warning -Message "No drive map found for $letter"
                }
            }
        } else {
            # if no drivemaps were found write a warning
            if ($driveMap.count -eq 0) {
                Write-Warning -Message "No drive maps found"
            } else {
                $drivemap | % {
                    [pscustomobject]@{
                        DriveLetter   = $_.LocalPath
                        RemotePath    = $_.RemotePath
                        Status        = $_.status
                        GlobalMapping = $_.GlobalMapping
                    }
                }
            }
        }
    }

    end { }
}