Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prototype creating a WinGet Configuration DSC file in PowerShell #462

Merged
merged 6 commits into from
Dec 4, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/prototype/WingetCreateMakeDSC.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# This script is a prototype for quickly creating DSC files.

#Powershell 7 Required
$hostdata=host
if ($hostdata.version.major -lt 7) {
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
Write-host "This script requires powershell 7. You can update powershell by typing winget install Microsoft.Powershell." -ForegroundColor red
[Environment]::Exit(1)
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
}

#Set output encoding to UTF-8
$OutputEncoding = [ System.Text.Encoding]::UTF8

if ($null -eq (Get-InstalledModule -Name Microsoft.Winget.Client))
{
try { Install-Module Microsoft.Winget.Client
} catch {
#Pass the exception
throw [System.Net.WebException]::new("Error retrieving powershell module: Microsoft.Winget.Client. Check that you have installed the Windows Package Manager modules correctly.", $_.Exception)
#bugbug is this good enough?
}
}

if ($null -eq (Get-InstalledModule -Name powershell-yaml))
{
try {
Install-Module powershell-yaml
} catch {
#Pass the exception
throw [System.Net.WebException]::new("Error retrieving powershell module: powershell-yaml. Check that you have installed the Windows Package Manager modules correctly.", $_.Exception)
#bugbug is this good enough?
}
}
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved

[System.Collections.ArrayList]$finalPackages = @()
$configurationVersion = "0.2.0"

$continue = $true
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
do
{
$appId = Read-Host "What is the Winget ID, or name of the package you want to add to the configuration file?"
$findResult = Find-WinGetPackage $appId

if ($findResult.count -ne 0)
{
$Index=1
foreach ($package in $findResult)
{
$packageDetails = "[$($Index)] $($package.Name) | $($package.Id) | $($package.Version)"
Write-Host $packageDetails
$index++
}

$selection = -1
$packageSelected = $false
while (-not($packageSelected))
{
write-host
# TODO: We should capture against bad value. "string"
# TODO: We should allow user to skip. Maybe hit S or X.
$selection = [int](Read-Host "Input the number of the package you want to select")
if (($selection -gt $findResult.count) -or ($selection -lt 1))
{
Write-Host "Selection is out of range, try again."
}
else
{
$packageSelected = $true
}
}

$selectedPackage = $findResult[$selection - 1]

#Specify Source
#Winget currently has 2 sources. If the ID contains a period, we will assume winget.
#otherwise it is the MSSTORE. We are not accounting for private REPOs at this time.
If ($selectedPackage.Id -like "*.*") {
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
$source="winget"
} else {
$source="msstore"
}

$unit = @{"resource" = "Microsoft.WinGet.DSC/WinGetPackage"; "directives" = @{"description" = $selectedPackage.Name; "allowPrerelease" = $true; }; "settings" = @{"id" = $selectedPackage.Id; "source"=$source }}
$tempvar = $finalPackages.Add($unit)
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
write-host Added $selectedPackage.Name -ForegroundColor blue


}
else
{
Write-Host "No package found matching input criteria." -ForegroundColor DarkYellow
}
} while ($(Read-Host "Would you like to add another package? [y/n]") -eq 'y')

Write-host
$fileName = Read-Host "Name of the configuration file (without extension)"
$filePath = Join-Path -Path (Get-Location) -ChildPath "$($fileName).winget"
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved

ConvertTo-Yaml @{"properties"= @{"resources"=$finalPackages; "configurationVersion"= $configurationVersion}} -OutFile $filePath -Force
KevinLaMS marked this conversation as resolved.
Show resolved Hide resolved
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved

Write-Host
Write-Host Testing resulting file. -ForegroundColor yellow
(&winget configure --help) > $null

if ($LASTEXITCODE -eq 0) {
winget configure validate --file $filePath
}
else {
Write-Host "'winget configure' is not available, skipping validation." -ForegroundColor Yellow
}

Write-Host "Configuration file created at: $($filePath)" -ForegroundColor Green
KevinLaMS marked this conversation as resolved.
Show resolved Hide resolved