|
| 1 | +# Copyright © 2017 - 2021 Chocolatey Software, Inc. |
| 2 | +# Copyright © 2015 - 2017 RealDimensions Software, LLC |
| 3 | +# Copyright © 2011 - 2015 RealDimensions Software, LLC & original authors/contributors from https://github.com/chocolatey/chocolatey |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +function Install-ChocolateyDesktopLink { |
| 18 | + <# |
| 19 | + .SYNOPSIS |
| 20 | + DEPRECATED - This adds a shortcut on the desktop to the specified file path. |
| 21 | +
|
| 22 | + .DESCRIPTION |
| 23 | + Determines the desktop folder and creates a shortcut to the specified |
| 24 | + file path. Will not throw errors if it fails. |
| 25 | + It is recommended you use `Install-ChocolateyShortcut` instead of this |
| 26 | + method as this has been removed upstream. |
| 27 | +
|
| 28 | + .NOTES |
| 29 | + This function was removed and deprecated in Chocolatey CLI in favor of using |
| 30 | + https://docs.chocolatey.org/en-us/create/functions/install-chocolateyshortcut. |
| 31 | + As the recommendation is to no longer use this function, no updates |
| 32 | + will be accepted to fix any problems. |
| 33 | + Compared to original function that was available in Chocolatey CLI, this |
| 34 | + implementation do not include any error handling, nor any promise that shortcuts |
| 35 | + will be created in the same way as Chocolatey CLI did it. |
| 36 | +
|
| 37 | + .INPUTS |
| 38 | + None |
| 39 | +
|
| 40 | + .OUTPUTS |
| 41 | + None |
| 42 | +
|
| 43 | + .PARAMETER TargetFilePath |
| 44 | + This is the location to the application/executable file that you want to |
| 45 | + add a shortcut to on the desktop. This is mandatory. |
| 46 | +
|
| 47 | + .PARAMETER IgnoredArguments |
| 48 | + Allows splatting with arguments that do not apply. Do not use directly. |
| 49 | +
|
| 50 | + .LINK |
| 51 | + Install-ChocolateyShortcut |
| 52 | +#> |
| 53 | + param( |
| 54 | + [parameter(Mandatory = $true, Position = 0)][string] $targetFilePath, |
| 55 | + [parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments |
| 56 | + ) |
| 57 | + Write-Warning "Install-ChocolateyDesktopLink was removed in Chocolatey CLI v1. If you are the package maintainer, please use Install-ChocolateyShortcut instead." |
| 58 | + Write-Warning "If you are not the maintainer, please contact the maintainer to update the $env:ChocolateyPackageName package." |
| 59 | + Write-Warning "There is no guarantee that this function works as expected compared to original function in Chocolatey CLI pre 1.0.0" |
| 60 | + |
| 61 | + if (!$targetFilePath) { |
| 62 | + throw "Install-ChocolateyDesktopLink - `$targetFilePath can not be null." |
| 63 | + } |
| 64 | + |
| 65 | + if (!(Test-Path($targetFilePath))) { |
| 66 | + Write-Warning "'$targetFilePath' does not exist. If it is not created the shortcut will not be valid." |
| 67 | + } |
| 68 | + |
| 69 | + Write-Debug "Creating Shortcut..." |
| 70 | + |
| 71 | + try { |
| 72 | + $desktop = $([System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::DesktopDirectory)) |
| 73 | + if (!(Test-Path($desktop))) { |
| 74 | + [System.IO.Directory]::CreateDirectory($desktop) | Out-Null |
| 75 | + } |
| 76 | + $link = Join-Path $desktop "$([System.IO.Path]::GetFileName($targetFilePath)).lnk" |
| 77 | + $workingDirectory = $([System.IO.Path]::GetDirectoryName($targetFilePath)) |
| 78 | + |
| 79 | + $wshshell = New-Object -ComObject WScript.Shell |
| 80 | + $lnk = $wshshell.CreateShortcut($link) |
| 81 | + $lnk.TargetPath = $targetFilePath |
| 82 | + $lnk.WorkingDirectory = $workingDirectory |
| 83 | + $lnk.Save() |
| 84 | + |
| 85 | + Write-Host "Desktop Shortcut created pointing at `'$targetFilePath`'." |
| 86 | + } |
| 87 | + catch { |
| 88 | + Write-Warning "Unable to create desktop link. Error captured was $($_.Exception.Message)." |
| 89 | + } |
| 90 | +} |
0 commit comments