-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerate-WingetAppScripts.ps1
More file actions
183 lines (155 loc) · 7 KB
/
Copy pathGenerate-WingetAppScripts.ps1
File metadata and controls
183 lines (155 loc) · 7 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Check for Winget
if (-not (Get-Command "winget.exe" -ErrorAction SilentlyContinue)) {
Write-Host "Winget is not installed. Please install it first." -ForegroundColor Red
exit 1
}
# Check for IntuneWinAppUtil.exe
$intuneUtil = Get-Command "IntuneWinAppUtil.exe" -ErrorAction SilentlyContinue
if (-not $intuneUtil) {
$localPath = Join-Path -Path $PSScriptRoot -ChildPath "IntuneWinAppUtil.exe"
if (Test-Path $localPath) {
$intuneUtil = $localPath
}
}
# Prompt user for search query
$query = Read-Host "Enter search term for winget packages (e.g., chrome, vscode)"
if ([string]::IsNullOrWhiteSpace($query)) {
Write-Host "Search term is required. Exiting..." -ForegroundColor Red
exit 1
}
# Perform winget search
$rawResults = winget search "$query" | Select-String -Pattern '^\S+\s+\S+\s+.+$'
$parsedApps = $rawResults | ForEach-Object {
$line = $_.Line.Trim()
$parts = $line -split '\s{2,}', 3
if ($parts.Count -eq 3) {
[PSCustomObject]@{
ID = $parts[0]
Name = $parts[1]
Source = $parts[2]
}
}
} | Sort-Object Name | Out-GridView -Title "Select one or more apps to generate scripts for" -PassThru
if (-not $parsedApps) {
Write-Host "No selections made. Exiting..." -ForegroundColor Yellow
exit 0
}
# Ask user whether to package into .intunewin
$makeWin = Read-Host "Would you like to create .intunewin packages for each app? (y/n)"
$createIntunewin = $makeWin -match '^[Yy]'
# Output path
$RootOutputDir = "$PSScriptRoot\WingetScripts"
New-Item -ItemType Directory -Path $RootOutputDir -Force | Out-Null
foreach ($app in $parsedApps) {
$safeName = ($app.Name -replace '[^\w\.-]', '_')
$packageId = $app.ID
$AppDir = Join-Path $RootOutputDir $safeName
New-Item -Path $AppDir -ItemType Directory -Force | Out-Null
$installPath = Join-Path $AppDir "Install-$safeName.ps1"
$uninstallPath = Join-Path $AppDir "Uninstall-$safeName.ps1"
$detectPath = Join-Path $AppDir "Detect-$safeName.ps1"
$installScript = @"
# Install script for $($app.Name)
`$PackageName = "$packageId"
`$LogDir = "`$env:ProgramData\Microsoft\IntuneManagementExtension\Logs"
`$LogFile = Join-Path -Path `$LogDir -ChildPath "`$PackageName-Install.log"
if (-not (Test-Path -Path `$LogDir)) { New-Item -Path `$LogDir -ItemType Directory -Force | Out-Null }
Start-Transcript -Path `$LogFile -Append -Force
function Write-Log { param ([string]`$Message, [string]`$Color = "White")
`$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[`$timestamp] `$Message" -ForegroundColor `$Color }
if (-not (Get-Command "winget.exe" -ErrorAction SilentlyContinue)) {
Write-Log "Winget not found. Aborting..." -Color Red
Stop-Transcript; exit 1 }
try {
Write-Log "Installing `$PackageName..." -Color Cyan
`$args = "install `"`$PackageName`" --silent --accept-source-agreements --accept-package-agreements --force"
`$p = Start-Process -FilePath "winget.exe" -ArgumentList `$args -NoNewWindow -Wait -PassThru
if (`$p.ExitCode -eq 0) {
Write-Log "`$PackageName installed successfully." -Color Green
} else {
Write-Log "Install failed with exit code `$(`$p.ExitCode)" -Color Red
Stop-Transcript; exit `$p.ExitCode }
} catch {
Write-Log "Error: `$(`$_.Exception.Message)" -Color Red
Stop-Transcript; exit 1 }
Write-Log "Done." -Color Cyan; Stop-Transcript; exit 0
"@
$uninstallScript = @"
# Uninstall script for $($app.Name)
`$PackageName = "$packageId"
`$LogDir = "`$env:ProgramData\Microsoft\IntuneManagementExtension\Logs"
`$LogFile = Join-Path -Path `$LogDir -ChildPath "`$PackageName-Uninstall.log"
if (-not (Test-Path -Path `$LogDir)) { New-Item -Path `$LogDir -ItemType Directory -Force | Out-Null }
Start-Transcript -Path `$LogFile -Append -Force
function Write-Log { param ([string]`$Message, [string]`$Color = "White")
`$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[`$timestamp] `$Message" -ForegroundColor `$Color }
if (-not (Get-Command "winget.exe" -ErrorAction SilentlyContinue)) {
Write-Log "Winget not found. Aborting..." -Color Red
Stop-Transcript; exit 1 }
try {
`$installed = & winget list --id `$PackageName --exact --source winget
if (`$LASTEXITCODE -ne 0 -or `$installed -notmatch `$PackageName) {
Write-Log "`$PackageName is not installed." -Color Yellow
Stop-Transcript; exit 0 }
} catch {
Write-Log "Check failed: `$(`$_.Exception.Message)" -Color Red
Stop-Transcript; exit 1 }
try {
Write-Log "Uninstalling `$PackageName..." -Color Cyan
`$args = "uninstall `"`$PackageName`" --silent --force"
`$p = Start-Process -FilePath "winget.exe" -ArgumentList `$args -NoNewWindow -Wait -PassThru
if (`$p.ExitCode -eq 0) {
Write-Log "Uninstalled successfully." -Color Green
} else {
Write-Log "Uninstall failed with exit code `$(`$p.ExitCode)" -Color Red
Stop-Transcript; exit `$p.ExitCode }
} catch {
Write-Log "Error: `$(`$_.Exception.Message)" -Color Red
Stop-Transcript; exit 1 }
Write-Log "Done." -Color Cyan; Stop-Transcript; exit 0
"@
$detectScript = @"
# Detection script for $($app.Name)
`$PackageName = "$packageId"
function Write-Log { param ([string]`$Message, [string]`$Color = "White")
`$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Write-Host "[`$timestamp] `$Message" -ForegroundColor `$Color }
if (-not (Get-Command "winget.exe" -ErrorAction SilentlyContinue)) {
Write-Log "Winget not found." -Color Red; exit 1 }
try {
`$installed = & winget list --id `$PackageName --exact --source winget
if (`$LASTEXITCODE -eq 0 -and `$installed -match `$PackageName) {
Write-Log "`$PackageName is installed." -Color Green; exit 0
} else {
Write-Log "`$PackageName NOT installed." -Color Yellow; exit 1 }
} catch {
Write-Log "Error: `$(`$_.Exception.Message)" -Color Red; exit 1 }
"@
$installScript | Out-File -FilePath $installPath -Encoding UTF8 -Force
$uninstallScript | Out-File -FilePath $uninstallPath -Encoding UTF8 -Force
$detectScript | Out-File -FilePath $detectPath -Encoding UTF8 -Force
Write-Host "`nGenerated scripts for $($app.Name):" -ForegroundColor Cyan
Write-Host " - Install: $installPath"
Write-Host " - Uninstall: $uninstallPath"
Write-Host " - Detect: $detectPath"
# Package into .intunewin
if ($createIntunewin -and $intuneUtil) {
$outFile = Join-Path $AppDir "$safeName.intunewin"
& $intuneUtil -q `
-c $AppDir `
-s "Install-$safeName.ps1" `
-o $AppDir `
-q
if (Test-Path $outFile) {
Write-Host " - IntuneWin: $outFile" -ForegroundColor Green
} else {
Write-Host " - IntuneWin packaging failed." -ForegroundColor Red
}
}
elseif ($createIntunewin -and -not $intuneUtil) {
Write-Host " - Skipped packaging: IntuneWinAppUtil.exe not found." -ForegroundColor DarkYellow
}
}
Write-Host "`nAll scripts saved under: $RootOutputDir" -ForegroundColor Green