如何在Windows 10中从PowerShell启动通用Windows应用程序(UWP)?

klh5stk1  于 2023-01-30  发布在  Shell
关注(0)|答案(5)|浏览(339)

在PowerShell中,如果我运行Get-AppxPackage,我将获得已安装的 *UWP应用 * 列表,包括我的应用。例如:

Name              : TonyHenrique.tonyuwpteste
Publisher         : CN=tTony
Architecture      : X64
ResourceId        :
Version           : 1.1.12.0
PackageFullName   : TonyHenrique.tonyuwpteste_1.1.12.0_x64__h3h3tmhvy8gfc
InstallLocation   : C:\Program Files\WindowsApps\TonyHenrique.tonyuwpteste_1.1.12.0_x64__h3h3tmhvy8gfc
IsFramework       : False
PackageFamilyName : TonyHenrique.tonyuwpteste_h3h3tmhvy8gfc
PublisherId       : h3h3tmhvy8gfc
IsResourcePackage : False
IsBundle          : False
IsDevelopmentMode : False
Dependencies      : {Microsoft.NET.CoreRuntime.2.1_2.1.25801.2_x64__8wekyb3d8bbwe, Microsoft.VCLibs.140.00.Debug_14.0.25805.1_x64__8wekyb3d8bbwe,
                    TonyHenrique.tonyuwpteste_1.1.12.0_neutral_split.scale-100_h3h3tmhvy8gfc}
IsPartiallyStaged : False
SignatureKind     : Developer
Status            : Ok

现在我想启动此应用程序。
如何在PowerShellcmd中执行此操作?

ocebsuys

ocebsuys1#

在开发过程中,我遇到过应用家族名称偶尔更改的情况。您可以通过简单的查找按名称可靠地启动应用:

  • 命令
powershell.exe explorer.exe shell:AppsFolder\$(get-appxpackage -name YourAppName ^| select -expandproperty PackageFamilyName)!App
  • 动力 shell
explorer.exe shell:AppsFolder\$(get-appxpackage -name YourAppName | select -expandproperty PackageFamilyName)!App
cigdeys3

cigdeys32#

借助Windows 10 Fall Creators更新1709(内部版本号16299),您现在可以为UWP应用定义应用执行别名,以便从cmd或powershell轻松启动它:

<Extensions>
    <uap5:Extension
      Category="windows.appExecutionAlias"
      StartPage="index.html">
      <uap5:AppExecutionAlias>
        <uap5:ExecutionAlias Alias="MyApp.exe" />
      </uap5:AppExecutionAlias>
    </uap5:Extension>
</Extensions>

此外,我们现在支持UWP应用的命令行参数。您可以从OnActivated事件读取它们:

async protected override void OnActivated(IActivatedEventArgs args)
{
    switch (args.Kind)
    {
        case ActivationKind.CommandLineLaunch:
            CommandLineActivatedEventArgs cmdLineArgs = 
                args as CommandLineActivatedEventArgs;
            CommandLineActivationOperation operation = cmdLineArgs.Operation;
            string cmdLineString = operation.Arguments;
            string activationPath = operation.CurrentDirectoryPath;

参见博客文章:https://blogs.windows.com/buildingapps/2017/07/05/command-line-activation-universal-windows-apps/

bejyjqdl

bejyjqdl3#

在PowerShell中尝试以下操作:

start shell:AppsFolder\TonyHenrique.tonyuwpteste_h3h3tmhvy8gfc!App
xqk2d5yq

xqk2d5yq4#

如果您知道显示名称,则可以使用Get-StartApps,其中包含正确的后缀:

start "shell:AppsFolder\$(Get-StartApps "Groove Music" | select -ExpandProperty AppId)"
oxf4rvwz

oxf4rvwz5#

我知道这是一个老职位,但我建立了一个函数来做。
希望对其他人有帮助。

function Start-UniversalWindowsApp {

    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory,
            Position = 0,
            ValueFromPipeline,
            ValueFromPipelineByPropertyName = $false
        )]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
        
        [Parameter()]
        [pscredential]$Credential
    )

    $queriedapp = $Global:UwpList | Where-Object { $PSItem.Name -like "*$Name*" }
    if (!$queriedapp) { Write-Error -Exception [System.Data.ObjectNotFoundException] -Message "No app was found with the name '$Name'." }
    if ($queriedapp.Count -gt 1) {
        $indexapplist = @()
        for ($i = 1; $i -le $queriedapp.Count; $i++) {
            $indexapplist += [pscustomobject]@{ Index = $i; App = $queriedapp[$i - 1] }
        }

        Write-Host @"
More than one app was found for the name $($Name):

"@

        $indexapplist | ForEach-Object { Write-Host "$($PSItem.Index) - $($PSItem.App.Name)" }
        $usrinput = Read-Host @"

Select one or all packages.
[I] Package Index  [A] All  [C] Cancel
"@

        while (($usrinput -ne 'A') -and ($usrinput -ne 'C') -and ($usrinput -notin $indexapplist.Index) ) {
            if ($usrinput) {
                Write-Host "Invalid option '$usrinput'."
            }
        }

        $appstorun = $null
        switch ($usrinput) {
            'A' { $appstorun = $queriedapp }
            'C' { $Global:LASTEXITCODE = 1223; return }
            Default { $appstorun = ($indexapplist | Where-Object { $PSItem.Index -eq $usrinput }).App }
        }

    }
    else {
        $appstorun = $queriedapp
    }

    if ($Credential) {
        foreach ($app in $appstorun) {
            Start-Process -FilePath 'explorer.exe' -ArgumentList "shell:AppsFolder\$($app.PackageFamilyName)!App" -Credential $Credential
        }
    }
    else {
        foreach ($app in $appstorun) {
            Start-Process -FilePath 'explorer.exe' -ArgumentList "shell:AppsFolder\$($app.PackageFamilyName)!App"
        }
    }

}

相关问题