windows - 用于创建具有属性的快捷方式的 Powershell 脚本

标签 windows powershell powershell-4.0

我正在关注这个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;
}

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

function set-shortcut {

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

    $WshShell = New-Object -comObject WScript.Shell

    $Shortcut = $WshShell.CreateShortcut($SourceLnk)

    $Shortcut.TargetPath = $DestinationPath

    $Shortcut.Save()

    }

最佳答案

为什么不直接使用 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
#>

关于windows - 用于创建具有属性的快捷方式的 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67116442/

相关文章:

javascript - 在 GoDaddy VPS Windows Plesk 上安装 Node.js

java - 生成的 jar 文件问题切换 Windows/Linux 操作系统

Android - 如何在 Windows 上构建 native 库并在 Android NDK 中使用它

powershell - 为什么 “where”不能按我期望的在这里选择某些结果?

.net - 在 .NET 事件中使用 ref 参数时 PowerShell 崩溃

c++ - 如何从 Windows 平台中的 C++ 应用程序在远程服务器上运行远程服务器的 .exe 二进制文件

powershell - 在powershell中对列表的每个元素运行一个函数

xml - PowerShell 从具有多个属性的 XML 中获取属性值

通过 Powershell 的 Azure 克隆应用程序 - InternalServerError

powershell - 如何在Powershell脚本中使用属性值