Private/New-MdtDrive.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
function New-MdtDrive { <# .SYNOPSIS Creates a new persistent PS drive mapped to an MDT share. .NOTES Author: Aaron Parker Twitter: @stealthpuppy .PARAMETER Path A path to a Microsoft Deployment Toolkit share. .PARAMETER Drive A PS drive letter to map to the MDT share. #> [CmdletBinding(SupportsShouldProcess = $true)] [OutputType([System.String])] param ( [Parameter(Mandatory = $false, Position = 0)] [ValidateNotNullOrEmpty()] [System.String] $Drive = "DS099", [Parameter(Mandatory = $true, Position = 1)] [ValidateNotNullOrEmpty()] [System.String] $Path ) # Set a description to be applied to the new MDT drive $Description = "MDT drive created by $($MyInvocation.MyCommand)" if ($mdtDrives = Get-MdtPersistentDrive | Where-Object { ($_.Path -eq $Path) -and ($_.Description -eq $Description) }) { Write-Verbose -Message "Found MDT drive: $($mdtDrives[0].Name)" $output = $mdtDrives[0].Name } else { if ($PSCmdlet.ShouldProcess("$($Drive): to $($Path)", "Mapping")) { $params = @{ Name = $Drive PSProvider = "MDTProvider" Root = $Path #NetworkPath = $Path Description = $Description ErrorAction = "Stop" } New-PSDrive @params | Add-MDTPersistentDrive # Return the MDT drive name $psDrive = Get-MdtPersistentDrive | Where-Object { $_.Path -eq $Path -and $_.Name -eq $Drive } Write-Verbose -Message "Found: $($psDrive.Name)" $output = $psDrive.Name } } Write-Output -InputObject $output } |