Guest User

Untitled

a guest
Mar 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. # Get-AppxPackage
  2. # explorer.exe shell:appsFolder\put-your-PackageFamilyName-here!put-your-app-ID-here
  3.  
  4. import subprocess
  5. import os
  6. import xml.etree.ElementTree as etree
  7. import re
  8. import ctypes as ct
  9.  
  10.  
  11. class AppXPackage(object):
  12. def __init__(self, property_dict):
  13. for key, value in property_dict.items():
  14. setattr(self, key, value)
  15. self.applications = self._get_applications()
  16.  
  17. def _get_applications(self):
  18. manifest_path = os.path.join(self.InstallLocation, 'AppxManifest.xml')
  19. if not os.path.isfile(manifest_path):
  20. return []
  21. manifest = etree.parse(manifest_path)
  22. ns = {'default': re.sub(r'\{(.*?)\}.+', r'\1', manifest.getroot().tag)}
  23.  
  24. package_applications = manifest.findall('./default:Applications/default:Application', ns)
  25. if not package_applications:
  26. return []
  27.  
  28. apps = []
  29.  
  30. package_identity = None
  31. package_identity_node = manifest.find('./default:Identity', ns)
  32. if package_identity_node is not None:
  33. package_identity = package_identity_node.get('Name')
  34.  
  35. description = None
  36. description_node = manifest.find('./default:Properties/default:Description', ns)
  37. if description_node is not None:
  38. description = description_node.text
  39.  
  40. display_name = None
  41. display_name_node = manifest.find('./default:Properties/default:DisplayName', ns)
  42. if display_name_node is not None:
  43. display_name = display_name_node.text
  44.  
  45. icon = None
  46. logo_node = manifest.find('./default:Properties/default:DisplayName', ns)
  47. if logo_node is not None:
  48. logo = logo_node.text
  49. icon_path = os.path.join(self.InstallLocation, logo)
  50.  
  51. for application in package_applications:
  52. if display_name and display_name.startswith('ms-resource:'):
  53. resource = self._get_resource('@{{{}\\resources.pri? ms-resource://{}/resources/{}}}'.format(self.InstallLocation,
  54. package_identity,
  55. display_name[len('ms-resource:'):]))
  56. if resource is not None:
  57. display_name = resource
  58. else:
  59. continue
  60.  
  61. if description and description.startswith('ms-resource:'):
  62. resource = self._get_resource('@{{{}\\resources.pri? ms-resource://{}/resources/{}}}'.format(self.InstallLocation,
  63. package_identity,
  64. description[len('ms-resource:'):]))
  65. if resource is not None:
  66. description = resource
  67. else:
  68. continue
  69.  
  70.  
  71. apps.append(AppX(['explorer.exe', 'shell:AppsFolder\{}!{}'.format(self.PackageFamilyName, application.get('Id'))],
  72. display_name,
  73. description,
  74. icon_path))
  75. return apps
  76.  
  77. @staticmethod
  78. def _get_resource(resource_descriptor):
  79. input = ct.create_unicode_buffer(resource_descriptor)
  80. output = ct.create_unicode_buffer(1024)
  81. size = ct.sizeof(output)
  82. input_ptr = ct.pointer(input)
  83. output_ptr = ct.pointer(output)
  84. result = ct.windll.shlwapi.SHLoadIndirectString(input_ptr, output_ptr, size, None)
  85. if result == 0:
  86. return output.value
  87. else:
  88. return None
  89.  
  90.  
  91. class AppX(object):
  92. def __init__(self, execution=[], display_name=None, description=None, icon_path=None):
  93. self.execution = execution
  94. self.display_name = display_name
  95. self.description = description
  96. self.icon_path = icon_path
  97.  
  98.  
  99. if __name__ == '__main__':
  100. startupinfo = subprocess.STARTUPINFO()
  101. startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
  102. output, err = subprocess.Popen(['C:\\windows\\system32\\windowspowershell\\v1.0\\powershell.exe',
  103. 'mode con cols=512; Get-AppxPackage'],
  104. stdout=subprocess.PIPE,
  105. universal_newlines=True,
  106. shell=False,
  107. startupinfo=startupinfo).communicate()
  108.  
  109. packages = []
  110. temp = {}
  111. for package in output.strip().split('\n\n'):
  112. for line in package.splitlines():
  113. line_parts = line.split(":")
  114. key = line_parts[0].strip()
  115. value = ":".join(line_parts[1:]).strip()
  116. temp[key] = value
  117. package = AppXPackage(temp)
  118. if package.applications:
  119. packages.append(package)
  120.  
  121. for app in packages:
  122. print('App: "{}" ({}) -> {}'.format(app.applications[0].display_name,
  123. app.applications[0].description,
  124. ' '.join(app.applications[0].execution)))
Add Comment
Please, Sign In to add comment