forked from aaronparker/icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSave-MicrosoftStoreIconImage.ps1
69 lines (61 loc) · 2.73 KB
/
Save-MicrosoftStoreIconImage.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<#
Download an icon in PNG format for an application from the Microsoft Store
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[ValidateScript({ if ((Test-Path -Path $_ -PathType "Leaf")) { $true } else { throw "Path not found: '$_'" } })]
[System.String] $InputFile = "$PSScriptRoot\StoreApps.txt",
[Parameter(Mandatory = $false)]
[ValidateScript({ if ((Test-Path -Path $_ -PathType "Container")) { $true } else { throw "Path not found: '$_'" } })]
[System.String] $OutputPath = "$PSScriptRoot\icons",
[Parameter(Mandatory = $false)]
[System.String] $StoreSearchUrl = "https://apps.microsoft.com/store/api/Products/GetFilteredSearch?hl=en-gb&gl=AU&FilteredCategories=AllProducts&Query="
)
# Don't show a progress bar for Invoke-WebRequest and Invoke-RestMethod
$ProgressPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
# Read the list of Store apps in the input file
# The file must include a list of Store apps as they appear in the store, one line per application
$AppsList = Get-Content -Path $InputFile -ErrorAction "Stop"
# Loop through the list of Store apps and download the icon if it's not already in the repo
foreach ($App in $AppsList) {
$IconFile = [System.IO.Path]::Combine($OutputPath, "MicrosoftStore-$($App -replace '\s', '').png")
if (Test-Path -Path $IconFile -PathType "Leaf") {
# If the icon is already in the repo, skip download
Write-Verbose -Message "Icon exists: '$IconFile'"
continue
}
else {
Write-Verbose -Message "Icon does not exist: '$IconFile'"
Write-Verbose -Message "Search for icon for app: '$App'"
# Search for the application
$SearchUrl = "$StoreSearchUrl$($App -replace '\s', '%20')"
$params = @{
Uri = $SearchUrl
UseBasicParsing = $true
ErrorAction = "Stop"
}
$Search = Invoke-RestMethod @params
# Find the icon URL for the application
if ([System.String]::IsNullOrEmpty($Search.highlightedList.iconUrl)) {
$Product = $Search.productsList | Where-Object { $_.title -eq $App }
$IconUrl = $Product.iconUrl
}
else {
$IconUrl = $Search.highlightedList.iconUrl
}
if ([System.String]::IsNullOrEmpty($IconUrl)) {
Write-Warning -Message "Icon URL is empty for app: '$App'"
}
else {
# If we get a result, download the icon for the app
$params = @{
Uri = $IconUrl
OutFile = $IconFile
UseBasicParsing = $true
ErrorAction = "Stop"
}
Invoke-WebRequest @params
}
}
}