PowerShell可以创建没有名称的快捷方式

时间:2018-10-22 09:57:31

标签: powershell

是否有可能创建没有名称的快捷方式? (无法通过GUI)

enter image description here

虽然可以通过GUI进行操作,但无法在URL快捷方式上设置热键。那是错误还是故意的?

代码与New-Shortcut

中的代码基本相同

控制台输出为:

FullName         : C:\Stuff\.lnk    
Arguments        :
Description      :
Hotkey           :
IconLocation     : ,0
RelativePath     :
TargetPath       : C:\WINDOWS\explorer.exe
WindowStyle      : 0
WorkingDirectory :

这是我的功能:

function New-Shortcut {
param 
(
    # Full path of the shortcut
    [Parameter(Mandatory=$true)]
    [System.IO.FileInfo] $FullPath,
    # Path to the target
    [Parameter(Mandatory=$true)]
    [string] $TargetPath,
    # Arguments for the shortcut
    [Parameter(Mandatory=$false)]
    [string] $Arguments = $null,
    # Description if needed
    [Parameter(Mandatory=$false)]
    [string] $Description = $null,
    # Hotkey if needed
    [Parameter(Mandatory=$false)]
    [string] $HotKey = $null,
    # Working directory if it differs from the shortcut directory
    [Parameter(Mandatory=$false)]
    [string] $WorkingDirectory = $null,
    # Windows style (minimized, maximized, ...)
    [Parameter(Mandatory=$false)]
    [int] $WindowStyle = $null,
    # Path to an icon if needed
    [Parameter(Mandatory=$false)]
    [string] $IconPath = $null,
    # Run as admin configuration
    [Parameter(Mandatory=$false)]
    [switch] $RunAsAdmin  = $null
)

Write-Debug "Check if '$FullPath' is valid ([character]:[\[character/digit]+]* + [.lnk | .url]."
if (!($FullPath.Extension -match "(\.lnk|\.url)"))
{
    Write-Debug "'$FullPath' is no valid 'lnk' or 'url' shortcut."
    Write-Error -Message "'$FullPath' not valid (no valid shortcut type)." -Category InvalidArgument `
                -CategoryTargetName $FullPath -CategoryTargetType $FullPath.GetType() `
                -ErrorId "NoValidShortcutPath" -ErrorAction Stop
}
Write-Debug "'$FullPath' is a valid 'lnk' or 'url' shortcut."

# Define Shortcut Properties
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($FullPath)

$Shortcut.TargetPath = $TargetPath

# Create directory for shortcut if it not exist
if(!(Test-Path $FullPath.Directory))
{
    Write-Verbose ("Directory '{0}' does not exist, creating it..." -f $FullPath.Directory)
    New-Item -Path $FullPath.Directory -ItemType Directory | Out-Null
}

if ($FullPath.Extension -eq ".url")
{
    Write-Verbose "Creating new shortcut '$FullPath' pointing at '$TargetPath'."
    $Shortcut.Save()
    Write-Debug "Shortcut '$FullPath' pointing at '$TargetPath' created!"
    return $Shortcut
}

$Shortcut.Arguments = $Arguments
$Shortcut.Description = $Description
$Shortcut.WorkingDirectory = $WorkingDirectory
$Shortcut.WindowStyle = $WindowStyle
$Shortcut.HotKey = $HotKey

If ($IconPath)
{
    Write-Debug "Set icon to path '$IconPath'..."
    $Shortcut.IconLocation = $IconPath 
    Write-Debug "Icon path set."
}

Write-Verbose "Creating new shortcut '$FullPath' pointing at '$TargetPath'."
$Shortcut.Save()
Write-Debug "Shortcut '$FullPath' pointing at '$TargetPath' created!"

if ($RunAsAdmin)
{
    $bytes = [System.IO.File]::ReadAllBytes($FullPath)
    $bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
    [System.IO.File]::WriteAllBytes($FullPath, $bytes)
}

return $Shortcut
}

至少我想很好地发布我的第一个问题。

这是我的单元测试,它适合于我可以创建没有名称的快捷方式的情况:

New-Shortcut -FullPath "C:\tmp\.lnk" -TargetPath "explorer.exe" -Verbose | 
            Should be $true #Should throw "'$invalidPath' not valid (no valid shortcut type)."
            #Test-Path $invalidPathIsNotInvalid | Should Be $false

由于我的考验,我陷入了挣扎的困境,在这里我试图解释一下。但是正如LotPings所说,快捷方式的名称似乎是“ .lnk”,这很罕见,但有效。

好像它已经回答了我的问题:)

0 个答案:

没有答案