Merge pull request #34 from zehadialam/feature-app-download-automation

Handle Win32 Apps of MSStore Type & Update InstallAppsandSysprep.cmd
This commit is contained in:
rbalsleyMSFT
2024-07-02 19:04:14 -07:00
committed by GitHub
3 changed files with 80 additions and 37 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
win32:7-Zip winget:7-Zip
store:Company Portal store:Company Portal
+13 -1
View File
@@ -22,16 +22,28 @@ for /d %%D in ("%basepath%\*") do (
set "dependenciesfolder=!appfolder!\Dependencies" set "dependenciesfolder=!appfolder!\Dependencies"
for %%F in ("!appfolder!\*") do ( for %%F in ("!appfolder!\*") do (
if not "%%~dpF"=="!dependenciesfolder!\" ( if not "%%~dpF"=="!dependenciesfolder!\" (
if /i not "%%~xF"==".xml" (
if /i not "%%~xF"==".yaml" (
set "mainpackage=%%F" set "mainpackage=%%F"
) )
) )
)
)
for %%F in ("!appfolder!\*.xml") do (
set "licensefile=%%F"
)
if defined mainpackage ( if defined mainpackage (
if exist "!dependenciesfolder!" ( if exist "!dependenciesfolder!" (
set "dism_command=DISM /Online /Add-ProvisionedAppxPackage /PackagePath:"!mainpackage!"" set "dism_command=DISM /Online /Add-ProvisionedAppxPackage /PackagePath:"!mainpackage!""
for %%G in ("!dependenciesfolder!\*") do ( for %%G in ("!dependenciesfolder!\*") do (
set "dism_command=!dism_command! /DependencyPackagePath:"%%G"" set "dism_command=!dism_command! /DependencyPackagePath:"%%G""
) )
set "dism_command=!dism_command! /SkipLicense /Region:All" if defined licensefile (
set "dism_command=!dism_command! /LicensePath:"!licensefile!""
) else (
set "dism_command=!dism_command! /SkipLicense"
)
set "dism_command=!dism_command! /Region:All"
echo !dism_command! echo !dism_command!
!dism_command! !dism_command!
) )
+64 -33
View File
@@ -1734,34 +1734,20 @@ function New-WinGetSettings {
} }
} }
function Get-Win32App { function Add-Win32SilentInstallCommand {
param ( param (
[string]$Win32App, [string]$AppFolder,
[int]$LineNumber [string]$AppFolderPath
) )
$wingetSearchResult = & winget.exe search --name "$Win32App" --exact --accept-source-agreements --source winget $appName = $AppFolder
if ($wingetSearchResult -contains "No package found matching input criteria.") { $installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include *.exe, *.msi -File -ErrorAction Stop
WriteLog "$Win32App not found in WinGet repository. Skipping download."
return
}
$appFolderPath = Join-Path -Path "$AppsPath\Win32" -ChildPath $Win32App
New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null
$appFolder = Split-Path -Path $appFolderPath -Leaf
WriteLog "Downloading $Win32App..."
$wingetDownloadResult = & winget.exe download --name "$Win32App" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String
if ($wingetDownloadResult -notmatch "Installer downloaded") {
WriteLog "$Win32App did not successfully download."
Remove-Item -Path $appFolderPath -Recurse -Force
return
}
WriteLog "$Win32App has completed downloading."
$installerPath = Get-ChildItem -Path "$appFolderPath\*" -Include *.exe, *.msi -File
$installer = Split-Path -Path $installerPath -Leaf $installer = Split-Path -Path $installerPath -Leaf
$yamlFile = Get-ChildItem -Path "$appFolderPath\*" -Include *.yaml -File $yamlFile = Get-ChildItem -Path "$appFolderPath\*" -Include *.yaml -File -ErrorAction Stop
$yamlContent = Get-Content -Path $yamlFile -Raw $yamlContent = Get-Content -Path $yamlFile -Raw
$silentInstallSwitch = [regex]::Match($yamlContent, 'Silent:\s*(.+)').Groups[1].Value $silentInstallSwitch = [regex]::Match($yamlContent, 'Silent:\s*(.+)').Groups[1].Value
$silentInstallSwitch = $silentInstallSwitch.Replace("'", "").Trim()
if (-not $silentInstallSwitch) { if (-not $silentInstallSwitch) {
WriteLog "Silent install switch for $Win32App could not be found. Skipping the inclusion of $Win32App." WriteLog "Silent install switch for $appName could not be found. Skipping the inclusion of $appName."
Remove-Item -Path $appFolderPath -Recurse -Force Remove-Item -Path $appFolderPath -Recurse -Force
return return
} }
@@ -1775,10 +1761,37 @@ function Get-Win32App {
$cmdFile = "$AppsPath\InstallAppsandSysprep.cmd" $cmdFile = "$AppsPath\InstallAppsandSysprep.cmd"
$cmdContent = Get-Content -Path $cmdFile $cmdContent = Get-Content -Path $cmdFile
$cmdContent = $cmdContent[0..($lineNumber - 2)] + $silentInstallCommand.Trim() + $cmdContent[($lineNumber - 1)..($cmdContent.Length - 1)] $cmdContent = $cmdContent[0..($lineNumber - 2)] + $silentInstallCommand.Trim() + $cmdContent[($lineNumber - 1)..($cmdContent.Length - 1)]
WriteLog "Writing silent install command for $Win32App to InstallAppsandSysprep.cmd at line number $LineNumber" WriteLog "Writing silent install command for $appName to InstallAppsandSysprep.cmd at line number $LineNumber"
Set-Content -Path $cmdFile -Value $cmdContent Set-Content -Path $cmdFile -Value $cmdContent
} }
function Get-WinGetApp {
param (
[string]$WinGetApp,
[int]$LineNumber
)
$wingetSearchResult = & winget.exe search --name "$WinGetApp" --exact --accept-source-agreements --source winget
if ($wingetSearchResult -contains "No package found matching input criteria.") {
WriteLog "$WinGetApp not found in WinGet repository. Skipping download."
return
}
$appFolderPath = Join-Path -Path "$AppsPath\Win32" -ChildPath $WinGetApp
New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null
$appFolder = Split-Path -Path $appFolderPath -Leaf
WriteLog "Downloading $WinGetApp..."
$wingetDownloadResult = & winget.exe download --name "$WinGetApp" --exact --download-directory "$appFolderPath" --scope machine --source winget --architecture "$WindowsArch" | Out-String
if ($wingetDownloadResult -match "No applicable installer found") {
$wingetDownloadResult = & winget.exe download --name "$WinGetApp" --exact --download-directory "$appFolderPath" --scope machine --source winget | Out-String
}
if ($wingetDownloadResult -notmatch "Installer downloaded") {
WriteLog "$WinGetApp did not successfully download."
Remove-Item -Path $appFolderPath -Recurse -Force
return
}
WriteLog "$WinGetApp has completed downloading."
Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath
}
function Get-StoreApp { function Get-StoreApp {
param ( param (
[string]$StoreApp [string]$StoreApp
@@ -1788,6 +1801,24 @@ function Get-StoreApp {
WriteLog "$StoreApp not found in WinGet repository. Skipping download." WriteLog "$StoreApp not found in WinGet repository. Skipping download."
return return
} }
# Skip the header lines and get the line with the app information
$appResult = $wingetSearchResult | Select-Object -Skip 2 | Select-Object -First 1
# Split the line by whitespace and get the second-to-last item (the Id)
$appID = ($appResult -split '\s+')[-2]
# Checking app ID to determine if store app is a win32 app
if ($appID.StartsWith("XP")) {
WriteLog "$StoreApp is a win32 app. Adding to $AppsPath\win32 folder"
$appFolderPath = Join-Path -Path "$AppsPath\win32" -ChildPath $StoreApp
New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null
$appFolder = Split-Path -Path $appFolderPath -Leaf
WriteLog "Downloading $StoreApp..."
$wingetDownloadResult = & winget.exe download --name --exact "$StoreApp" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --architecture "$WindowsArch" --scope machine | Out-String
if ($wingetDownloadResult -match "No applicable installer found") {
$wingetDownloadResult = & winget.exe download --name --exact "$StoreApp" --download-directory "$appFolderPath" --accept-package-agreements --accept-source-agreements --source msstore --scope machine | Out-String
}
Add-Win32SilentInstallCommand -AppFolder $appFolder -AppFolderPath $appFolderPath
return
}
$appFolderPath = Join-Path -Path "$AppsPath\MSStore" -ChildPath $StoreApp $appFolderPath = Join-Path -Path "$AppsPath\MSStore" -ChildPath $StoreApp
New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null New-Item -Path $appFolderPath -ItemType Directory -Force | Out-Null
# Invoke-Process is not used here because it terminates the script if the exit code of the process is not zero. # Invoke-Process is not used here because it terminates the script if the exit code of the process is not zero.
@@ -1812,7 +1843,7 @@ function Get-StoreApp {
Set-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" -Value $updatedcmdContent Set-Content -Path "$AppsPath\InstallAppsandSysprep.cmd" -Value $updatedcmdContent
} }
WriteLog "$StoreApp has completed downloading. Identifying the latest version of $StoreApp." WriteLog "$StoreApp has completed downloading. Identifying the latest version of $StoreApp."
$packages = Get-ChildItem -Path "$appFolderPath\*" -Exclude "Dependencies\*", "*.xml", "*.yaml" -File $packages = Get-ChildItem -Path "$appFolderPath\*" -Exclude "Dependencies\*", "*.xml", "*.yaml" -File -ErrorAction Stop
# WinGet downloads multiple versions of certain store apps. The latest version of the package will be determined based on the date of the file signature. # WinGet downloads multiple versions of certain store apps. The latest version of the package will be determined based on the date of the file signature.
$latestPackage = "" $latestPackage = ""
$latestDate = [datetime]::MinValue $latestDate = [datetime]::MinValue
@@ -1851,17 +1882,17 @@ function Get-Apps {
WriteLog "No apps were specified in AppsList.txt file." WriteLog "No apps were specified in AppsList.txt file."
return return
} }
$win32Apps = @() $wingetApps = @()
$storeApps = @() $storeApps = @()
$apps | ForEach-Object { $apps | ForEach-Object {
if ($_ -like 'win32:*') { if ($_ -like 'winget:*') {
$win32Apps += $_.Substring(6) $wingetApps += $_.Substring(7).Trim()
} }
elseif ($_ -like 'store:*') { elseif ($_ -like 'store:*') {
$storeApps += $_.Substring(6) $storeApps += $_.Substring(6).Trim()
} }
} }
$wingetInstalled = Get-ChildItem -Path "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" $wingetInstalled = Get-ChildItem -Path "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\winget.exe" -ErrorAction SilentlyContinue
if (-not $wingetInstalled) { if (-not $wingetInstalled) {
WriteLog "WinGet is not installed. Downloading preview version of WinGet and its dependencies..." WriteLog "WinGet is not installed. Downloading preview version of WinGet and its dependencies..."
Install-WinGet -InstallWithDependencies $true Install-WinGet -InstallWithDependencies $true
@@ -1880,17 +1911,17 @@ function Get-Apps {
$lineNumber = 13 $lineNumber = 13
$win32Folder = Join-Path -Path $AppsPath -ChildPath "Win32" $win32Folder = Join-Path -Path $AppsPath -ChildPath "Win32"
$storeAppsFolder = Join-Path -Path $AppsPath -ChildPath "MSStore" $storeAppsFolder = Join-Path -Path $AppsPath -ChildPath "MSStore"
if ($win32Apps) { if ($wingetApps) {
if (-not (Test-Path -Path $win32Folder -PathType Container)) { if (-not (Test-Path -Path $win32Folder -PathType Container)) {
New-Item -Path $win32Folder -ItemType Directory -Force | Out-Null New-Item -Path $win32Folder -ItemType Directory -Force | Out-Null
} }
foreach ($win32App in $win32Apps) { foreach ($wingetApp in $wingetApps) {
try { try {
Get-Win32App -Win32App $win32App -LineNumber $lineNumber Get-WinGetApp -WinGetApp $wingetApp -LineNumber $lineNumber
$lineNumber++ $lineNumber++
} }
catch { catch {
WriteLog "Error occurred while processing $win32App : $_" WriteLog "Error occurred while processing $wingetApp : $_"
throw $_ throw $_
} }
} }