Guest User

Untitled

a guest
Aug 24th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.26 KB | None | 0 0
  1. using SophiApp.Dto;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Management.Automation;
  7. using System.Threading;
  8. using Windows.ApplicationModel;
  9. using Windows.Foundation;
  10. using Windows.Management.Deployment;
  11.  
  12. namespace SophiApp.Helpers
  13. {
  14.     internal class UwpHelper
  15.     {
  16.         internal static Package GetPackage(string packageName)
  17.         {
  18.             var sid = OsHelper.GetCurrentUserSid().Value;
  19.             var packageManager = new PackageManager();
  20.             return packageManager.FindPackagesForUser(sid)
  21.                                  .First(package => package.Id.Name.Equals(packageName));
  22.         }
  23.  
  24.         internal static IEnumerable<UwpElementDto> GetPackagesDto(bool forAllUsers = false)
  25.         {
  26.             var currentUserScript = @"# The following UWP apps will be excluded from the display
  27. $ExcludedAppxPackages = @(
  28. # Microsoft Desktop App Installer
  29. 'Microsoft.DesktopAppInstaller',
  30.  
  31. # Store Experience Host
  32. 'Microsoft.StorePurchaseApp',
  33.  
  34. # Notepad
  35. 'Microsoft.WindowsNotepad',
  36.  
  37. # Microsoft Store
  38. 'Microsoft.WindowsStore',
  39.  
  40. # Windows Terminal
  41. 'Microsoft.WindowsTerminal',
  42. 'Microsoft.WindowsTerminalPreview',
  43.  
  44. # Web Media Extensions
  45. 'Microsoft.WebMediaExtensions'
  46. )
  47.  
  48. $AppxPackages = Get-AppxPackage -PackageTypeFilter Bundle | Where-Object -FilterScript {$_.Name -notin $ExcludedAppxPackages}
  49.  
  50. # The Bundle packages contains no Microsoft Teams
  51. if (Get-AppxPackage -Name MicrosoftTeams -AllUsers:$false)
  52. {
  53.     # Temporarily hack: due to the fact that there are actually two Microsoft Teams packages, we need to choose the first one to display
  54.     $AppxPackages += Get-AppxPackage -Name MicrosoftTeams -AllUsers:$false | Select-Object -Index 0
  55. }
  56.  
  57. # The Bundle packages contains no Spotify
  58. if (Get-AppxPackage -Name SpotifyAB.SpotifyMusic -AllUsers:$false)
  59. {
  60.     # Temporarily hack: due to the fact that there are actually two Microsoft Teams packages, we need to choose the first one to display
  61.     $AppxPackages += Get-AppxPackage -Name SpotifyAB.SpotifyMusic -AllUsers:$false | Select-Object -Index 0
  62. }
  63.  
  64. $PackagesIds = [Windows.Management.Deployment.PackageManager, Windows.Web, ContentType = WindowsRuntime]::new().FindPackages() | Select-Object -Property DisplayName, Logo -ExpandProperty Id | Select-Object -Property Name, DisplayName, Logo
  65.  
  66. foreach ($AppxPackage in $AppxPackages)
  67. {
  68.     $PackageId = $PackagesIds | Where-Object -FilterScript {$_.Name -eq $AppxPackage.Name}
  69.  
  70.     if (-not $PackageId)
  71.     {
  72.         continue
  73.     }
  74.  
  75.      [PSCustomObject]@{
  76.         Name            = $AppxPackage.Name
  77.         PackageFullName = $AppxPackage.PackageFullName
  78.         Logo            = $PackageId.Logo
  79.         DisplayName     = $PackageId.DisplayName
  80.     }
  81. }";
  82.             var allUsersScript = @"# The following UWP apps will be excluded from the display
  83. $ExcludedAppxPackages = @(
  84. # Microsoft Desktop App Installer
  85. 'Microsoft.DesktopAppInstaller',
  86.  
  87. # Store Experience Host
  88. 'Microsoft.StorePurchaseApp',
  89.  
  90. # Notepad
  91. 'Microsoft.WindowsNotepad',
  92.  
  93. # Microsoft Store
  94. 'Microsoft.WindowsStore',
  95.  
  96. # Windows Terminal
  97. 'Microsoft.WindowsTerminal',
  98. 'Microsoft.WindowsTerminalPreview',
  99.  
  100. # Web Media Extensions
  101. 'Microsoft.WebMediaExtensions'
  102. )
  103.  
  104. $AppxPackages = Get-AppxPackage -PackageTypeFilter Bundle -AllUsers | Where-Object -FilterScript {$_.Name -notin $ExcludedAppxPackages}
  105.  
  106. # The Bundle packages contains no Microsoft Teams
  107. if (Get-AppxPackage -Name MicrosoftTeams -AllUsers:$true)
  108. {
  109.     # Temporarily hack: due to the fact that there are actually two Microsoft Teams packages, we need to choose the first one to display
  110.     $AppxPackages += Get-AppxPackage -Name MicrosoftTeams -AllUsers:$true | Select-Object -Index 0
  111. }
  112.  
  113. # The Bundle packages contains no Spotify
  114. if (Get-AppxPackage -Name SpotifyAB.SpotifyMusic -AllUsers:$true)
  115. {
  116.     # Temporarily hack: due to the fact that there are actually two Microsoft Teams packages, we need to choose the first one to display
  117.     $AppxPackages += Get-AppxPackage -Name SpotifyAB.SpotifyMusic -AllUsers:$true | Select-Object -Index 0
  118. }
  119.  
  120. $PackagesIds = [Windows.Management.Deployment.PackageManager, Windows.Web, ContentType = WindowsRuntime]::new().FindPackages() | Select-Object -Property DisplayName, Logo -ExpandProperty Id | Select-Object -Property Name, DisplayName, Logo
  121.  
  122. foreach ($AppxPackage in $AppxPackages)
  123. {
  124.     $PackageId = $PackagesIds | Where-Object -FilterScript {$_.Name -eq $AppxPackage.Name}
  125.  
  126.     if (-not $PackageId)
  127.     {
  128.         continue
  129.     }
  130.  
  131.      [PSCustomObject]@{
  132.         Name            = $AppxPackage.Name
  133.         PackageFullName = $AppxPackage.PackageFullName
  134.         Logo            = $PackageId.Logo
  135.         DisplayName     = $PackageId.DisplayName
  136.     }
  137. }";
  138.  
  139.             return PowerShell.Create()
  140.                              .AddScript(forAllUsers ? allUsersScript : currentUserScript)
  141.                              .Invoke()
  142.                              .Where(uwp => uwp.Properties["Logo"].Value != null)
  143.                              .Select(uwp => new UwpElementDto()
  144.                              {
  145.                                  Name = uwp.Properties["Name"].Value as string,
  146.                                  PackageFullName = uwp.Properties["PackageFullName"].Value as string,
  147.                                  Logo = uwp.Properties["Logo"].Value.GetFirstValue<Uri>(),
  148.                                  DisplayName = uwp.Properties["DisplayName"].Value.GetFirstValue<string>()
  149.                              });
  150.         }
  151.  
  152.         internal static void InstallPackage(string package)
  153.         {
  154.             var packageUri = new Uri(package);
  155.             var packageManager = new PackageManager();
  156.             var deploymentOperation = packageManager.AddPackageAsync(packageUri, null, DeploymentOptions.None);
  157.             var opCompletedEvent = new ManualResetEvent(false);
  158.             deploymentOperation.Completed = (depProgress, status) => { opCompletedEvent.Set(); };
  159.             opCompletedEvent.WaitOne();
  160.         }
  161.  
  162.         internal static bool PackageExist(string packageName)
  163.         {
  164.             var sid = OsHelper.GetCurrentUserSid().Value;
  165.             var packageManager = new PackageManager();
  166.             return packageManager.FindPackagesForUser(sid)
  167.                                  .Where(package => package.Id.Name == packageName)
  168.                                  .Count() > 0;
  169.         }
  170.  
  171.         internal static void RemovePackage(string packageFullName, bool allUsers)
  172.         {
  173.             var stopwatch = Stopwatch.StartNew();
  174.             var packageManager = new PackageManager();
  175.             var deploymentOperation = packageManager.RemovePackageAsync(packageFullName, allUsers ? RemovalOptions.RemoveForAllUsers : RemovalOptions.None);
  176.             var opCompletedEvent = new ManualResetEvent(false);
  177.             deploymentOperation.Completed = (depProgress, status) => { opCompletedEvent.Set(); };
  178.             opCompletedEvent.WaitOne();
  179.             stopwatch.Stop();
  180.  
  181.             if (deploymentOperation.Status == AsyncStatus.Error)
  182.             {
  183.                 var deploymentResult = deploymentOperation.GetResults();
  184.                 DebugHelper.UwpRemovedHasException(packageFullName, deploymentResult.ErrorText);
  185.                 return;
  186.             }
  187.  
  188.             DebugHelper.UwpRemoved(packageFullName, stopwatch.Elapsed.TotalSeconds, deploymentOperation.Status);
  189.         }
  190.     }
  191. }
Add Comment
Please, Sign In to add comment