【问题标题】:Poweshell script to create shortcut with properties用于创建具有属性的快捷方式的 Powershell 脚本
【发布时间】:2021-07-10 23:04:22
【问题描述】:

我正在关注这个msdn 文档。我想创建一个具有两个属性AppUserModelIdToastActivatorCLSID 的快捷方式。如何使用 powershell 实现这一点。

基本上我想通过powershell脚本转换以下代码并创建快捷方式。

_Use_decl_annotations_
HRESULT DesktopToastsApp::InstallShortcut(PCWSTR shortcutPath, PCWSTR exePath)
{
    ComPtr<IShellLink> shellLink;
    HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink));
    if (SUCCEEDED(hr))
    {
        hr = shellLink->SetPath(exePath);
        if (SUCCEEDED(hr))
        {
            ComPtr<IPropertyStore> propertyStore;

            hr = shellLink.As(&propertyStore);
            if (SUCCEEDED(hr))
            {
                PROPVARIANT propVar;
                propVar.vt = VT_LPWSTR;
                propVar.pwszVal = const_cast<PWSTR>(AppId); // for _In_ scenarios, we don't need a copy
                hr = propertyStore->SetValue(PKEY_AppUserModel_ID, propVar);
                if (SUCCEEDED(hr))
                {
                    propVar.vt = VT_CLSID;
                    propVar.puuid = const_cast<CLSID*>(&__uuidof(NotificationActivator));
                    hr = propertyStore->SetValue(PKEY_AppUserModel_ToastActivatorCLSID, propVar);
                    if (SUCCEEDED(hr))
                    {
                        hr = propertyStore->Commit();
                        if (SUCCEEDED(hr))
                        {
                            ComPtr<IPersistFile> persistFile;
                            hr = shellLink.As(&persistFile);
                            if (SUCCEEDED(hr))
                            {
                                hr = persistFile->Save(shortcutPath, TRUE);
                            }
                        }
                    }
                }
            }
        }
    }
    return hr;
}

我写了一个简单的脚本来创建一个快捷方式,但不知道如何标记上述两个道具

function set-shortcut {

param ( [string]$SourceLnk, [string]$DestinationPath )

    $WshShell = New-Object -comObject WScript.Shell

    $Shortcut = $WshShell.CreateShortcut($SourceLnk)

    $Shortcut.TargetPath = $DestinationPath

    $Shortcut.Save()

    }

【问题讨论】:

    标签: windows powershell powershell-4.0


    【解决方案1】:

    为什么不直接使用 MS powershellgallery.com 中的预构建模块/脚本来确定它们是否能解决您的用例,而不是从头开始重写。除非这是一种学习努力。即使这样,查看下面的底层代码可能会有所帮助。

    Find-Module -Name '*shortcut*'
    
    # Results
    <#
    Version   Name                        Repository Description
    -------   ----                        ---------- -----------
    2.2.0     DSCR_Shortcut               PSGallery  PowerShell DSC Resource to create shortcut file.
    ...
    1.0.6     PSShortcut                  PSGallery  This module eases working with Windows shortcuts (LNK and URL) files.
    ...
    #>
    
    
    Find-Script -Name '*Shortcut*'
    # Results
    <#
    Version Name                       Repository Description   
    ------- ----                       ---------- -----------   
    ...                        
    1.0.0.0 New-DeskTopShortCut        PSGallery  Create a desktop shortcut
    #>
    

    【讨论】:

    • 我经历了这些。但是,这是创建快捷方式的非常简单的脚本。我对给上述两个道具加盖印特别感兴趣。
    • 对于普通的窗口 url 快捷方式,这些属性不存在/可见。您可以使用Get-Item -Path 'D:\Documents\PowerShell\Non-Admin user for WMI - YouTube.URL| Get-Member -Force | Format-Table -AutoSize 或普通桌面应用快捷方式或 AppX 快捷方式看到这一点,都返回相同的集合。
    【解决方案2】:

    我也不知道PSGallery有这个任务的模块,所以我前段时间写了一个脚本,看看有没有帮助:

    Set-Shortcut.ps1

    编辑:

    您只需在脚本顶部添加$InformationPreference = "Continue" 即可显示信息性消息。

    【讨论】:

    • 我对设置 AppUserModelId 和 ActivatorCLSID 特别感兴趣。类似$Shortcut.AppUserModelId= "MyApp"
    • 您可以右键单击任何桌面快捷方式并设置此值吗?或者您可以使用WScript.Shell 设置它吗?这是 C# commandlet 编程的任务,PowerShell 作为脚本语言不是完成此任务的正确工具。
    【解决方案3】:

    想象一个项目有几个属性。您要做的是为某个属性赋值。

    Get-Item -LiteralPath $NomeDoArquivo | Set-ItemProperty -Name $Propriedade -Value $Valor -Force
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 2011-04-21
      • 1970-01-01
      • 2014-11-23
      • 2012-03-30
      相关资源
      最近更新 更多