forked from proxb/PowerShell_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-SymLink.ps1
121 lines (107 loc) · 4.3 KB
/
New-SymLink.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
Function New-SymLink {
<#
.SYNOPSIS
Creates a Symbolic link to a file or directory
.DESCRIPTION
Creates a Symbolic link to a file or directory as an alternative to mklink.exe
.PARAMETER Path
Name of the path that you will reference with a symbolic link.
.PARAMETER SymName
Name of the symbolic link to create. Can be a full path/unc or just the name.
If only a name is given, the symbolic link will be created on the current directory that the
function is being run on.
.PARAMETER File
Create a file symbolic link
.PARAMETER Directory
Create a directory symbolic link
.NOTES
Name: New-SymLink
Author: Boe Prox
Created: 15 Jul 2013
.EXAMPLE
New-SymLink -Path "C:\users\admin\downloads" -SymName "C:\users\admin\desktop\downloads" -Directory
SymLink Target Type
------- ------ ----
C:\Users\admin\Desktop\Downloads C:\Users\admin\Downloads Directory
Description
-----------
Creates a symbolic link to downloads folder that resides on C:\users\admin\desktop.
.EXAMPLE
New-SymLink -Path "C:\users\admin\downloads\document.txt" -SymName "SomeDocument" -File
SymLink Target Type
------- ------ ----
C:\users\admin\desktop\SomeDocument C:\users\admin\downloads\document.txt File
Description
-----------
Creates a symbolic link to document.txt file under the current directory called SomeDocument.
#>
[cmdletbinding(
DefaultParameterSetName = 'Directory',
SupportsShouldProcess=$True
)]
Param (
[parameter(Position=0,ParameterSetName='Directory',ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,Mandatory=$True)]
[parameter(Position=0,ParameterSetName='File',ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,Mandatory=$True)]
[ValidateScript({
If (Test-Path $_) {$True} Else {
Throw "`'$_`' doesn't exist!"
}
})]
[string]$Path,
[parameter(Position=1,ParameterSetName='Directory')]
[parameter(Position=1,ParameterSetName='File')]
[string]$SymName,
[parameter(Position=2,ParameterSetName='File')]
[switch]$File,
[parameter(Position=2,ParameterSetName='Directory')]
[switch]$Directory
)
Begin {
Try {
$null = [mklink.symlink]
} Catch {
Add-Type @"
using System;
using System.Runtime.InteropServices;
namespace mklink
{
public class symlink
{
[DllImport("kernel32.dll")]
public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags);
}
}
"@
}
}
Process {
#Assume target Symlink is on current directory if not giving full path or UNC
If ($SymName -notmatch "^(?:[a-z]:\\)|(?:\\\\\w+\\[a-z]\$)") {
$SymName = "{0}\{1}" -f $pwd,$SymName
}
$Flag = @{
File = 0
Directory = 1
}
If ($PScmdlet.ShouldProcess($Path,'Create Symbolic Link')) {
Try {
$return = [mklink.symlink]::CreateSymbolicLink($SymName,$Path,$Flag[$PScmdlet.ParameterSetName])
If ($return) {
$object = New-Object PSObject -Property @{
SymLink = $SymName
Target = $Path
Type = $PScmdlet.ParameterSetName
}
$object.pstypenames.insert(0,'System.File.SymbolicLink')
$object
} Else {
Throw "Unable to create symbolic link!"
}
} Catch {
Write-warning ("{0}: {1}" -f $path,$_.Exception.Message)
}
}
}
}