Public/New-Junction.ps1
|
<#
.SYNOPSIS Creates a directory junction. .DESCRIPTION Wraps New-Item -ItemType Junction to create a junction at -Path that points to -Value. Extra arguments are passed through to New-Item. .PARAMETER Value Target directory path that the junction points to. .PARAMETER Path Junction path to create. .PARAMETER Arguments Additional arguments forwarded to New-Item. .EXAMPLE New-Junction -Value "C:\Projects\Shared" -Path "D:\LinkToShared" Creates D:\LinkToShared as a junction to C:\Projects\Shared. #> function New-Junction { [CmdletBinding()] [OutputType([System.IO.FileSystemInfo])] param ( [Parameter(ParameterSetName = 'nameSet', Position = 0, ValueFromPipelineByPropertyName = $true)] [Parameter(ParameterSetName = 'pathSet', Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)] [string[]] ${Path}, [Parameter(ParameterSetName = 'nameSet', Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [AllowNull()] [AllowEmptyString()] [string] ${Name}, [Parameter(Position = 1, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [Alias('Target')] [System.Object] ${Value}, [switch] ${Force}, [Parameter(ValueFromPipelineByPropertyName = $true)] [pscredential] [System.Management.Automation.CredentialAttribute()] ${Credential}) dynamicparam { try { $targetCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Management\New-Item', [System.Management.Automation.CommandTypes]::Cmdlet, $PSBoundParameters) $dynamicParams = @($targetCmd.Parameters.GetEnumerator() | Microsoft.PowerShell.Core\Where-Object { $_.Value.IsDynamic }) if ($dynamicParams.Length -gt 0) { $paramDictionary = [Management.Automation.RuntimeDefinedParameterDictionary]::new() foreach ($param in $dynamicParams) { $param = $param.Value if (-not $MyInvocation.MyCommand.Parameters.ContainsKey($param.Name)) { $dynParam = [Management.Automation.RuntimeDefinedParameter]::new($param.Name, $param.ParameterType, $param.Attributes) $paramDictionary.Add($param.Name, $dynParam) } } return $paramDictionary } } catch { throw } } process{ Microsoft.PowerShell.Management\New-Item -ItemType Junction @PsBoundParameters } } |