-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-NetworkLocation.ps1
58 lines (37 loc) · 1.37 KB
/
New-NetworkLocation.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
Function New-NetworkLocation {
Param(
[Parameter(
Position = 0,
Mandatory = $true
)]
[String]$Name,
[Parameter(
Position = 1,
Mandatory = $true,
ValueFromPipeline = $true
)]
[ValidateScript({Test-Path $_ -PathType Container})]
[String]$TargetPath,
[Switch]$Force
)
If ($Force -and (Test-Path (Join-Path "$env:APPDATA\Microsoft\Windows\Network Shortcuts" $Name))) {
Remove-Item -Path (Join-Path "$env:APPDATA\Microsoft\Windows\Network Shortcuts" $Name) -Recurse -Force
}
$DesktopINIData = @"
[.ShellClassInfo]
CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}
Flags=2
"@
$Folder = New-Item -ItemType Directory -Path (Join-Path "$env:APPDATA\Microsoft\Windows\Network Shortcuts" $Name)
$INIFile = New-Item -ItemType File -Path (Join-Path $Folder.FullNAme "desktop.ini")
$INIFile.Attributes = "" # Clear Archive Flag
$INIFile.Attributes = "Hidden, System"
$INIFile | Set-Content -Value $DesktopINIData
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut((Join-Path $Folder.FullName "target.lnk"))
$Shortcut.TargetPath = $TargetPath
$Shortcut.Description = $TargetPath
$Shortcut.Save()
$Folder.Attributes = "ReadOnly"
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($WshShell) | Out-Null
}