From 6c877cd1e0c885ac2d42f4a8243e0e35b8fb4662 Mon Sep 17 00:00:00 2001 From: Stephan van Rooij <1292510+svrooij@users.noreply.github.com> Date: Tue, 30 Jul 2024 23:55:58 +0200 Subject: [PATCH] Override arguments for installers (#96) * Enable overriding arguments * Make burn arguments better --- .../Commands/NewWtWingetPackage.cs | 14 +++++- .../Commands/TestWtIntuneWin.cs | 6 --- .../Commands/TestWtSetupFile.cs | 47 +++++++------------ .../Svrooij.WinTuner.CmdLets.dll-Help.xml | 28 ++++++++++- .../docs/New-WtWingetPackage.md | 22 +++++++-- src/WingetIntune/Intune/IntuneManager.cs | 19 +++++--- src/WingetIntune/Models/PackageOptions.cs | 1 + src/WingetIntune/Models/WingetPackage.cs | 10 ++++ 8 files changed, 97 insertions(+), 50 deletions(-) diff --git a/src/Svrooij.WinTuner.CmdLets/Commands/NewWtWingetPackage.cs b/src/Svrooij.WinTuner.CmdLets/Commands/NewWtWingetPackage.cs index 3dfe162..36a0b31 100644 --- a/src/Svrooij.WinTuner.CmdLets/Commands/NewWtWingetPackage.cs +++ b/src/Svrooij.WinTuner.CmdLets/Commands/NewWtWingetPackage.cs @@ -34,7 +34,7 @@ public class NewWtWingetPackage : DependencyCmdlet /// The folder to store the package in /// [Parameter( - Mandatory = false, + Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, @@ -107,6 +107,17 @@ public class NewWtWingetPackage : DependencyCmdlet HelpMessage = "The desired locale, if available (eg. 'en-US')")] public string? Locale { get; set; } + /// + /// Override the installer arguments + /// + [Parameter( + Mandatory = false, + Position = 8, + ValueFromPipeline = false, + ValueFromPipelineByPropertyName = false, + HelpMessage = "Override the installer arguments")] + public string? InstallerArguments { get; set; } + [ServiceDependency] private ILogger logger; @@ -152,6 +163,7 @@ public override async Task ProcessRecordAsync(CancellationToken cancellationToke InstallerContext = InstallerContext, PackageScript = PackageScript ?? false, Locale = Locale, + OverrideArguments = InstallerArguments }, cancellationToken: cancellationToken); diff --git a/src/Svrooij.WinTuner.CmdLets/Commands/TestWtIntuneWin.cs b/src/Svrooij.WinTuner.CmdLets/Commands/TestWtIntuneWin.cs index 0f43329..b17b59a 100644 --- a/src/Svrooij.WinTuner.CmdLets/Commands/TestWtIntuneWin.cs +++ b/src/Svrooij.WinTuner.CmdLets/Commands/TestWtIntuneWin.cs @@ -1,19 +1,13 @@ using Microsoft.Extensions.Logging; -using Microsoft.Graph.Beta; using Svrooij.PowerShell.DependencyInjection; using System; using System.IO; using System.Linq; using System.Management.Automation; -using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using WingetIntune; -using WingetIntune.Graph; using WingetIntune.Intune; -using WingetIntune.Models; using WingetIntune.Testing; -using GraphModels = Microsoft.Graph.Beta.Models; namespace Svrooij.WinTuner.CmdLets.Commands; /// diff --git a/src/Svrooij.WinTuner.CmdLets/Commands/TestWtSetupFile.cs b/src/Svrooij.WinTuner.CmdLets/Commands/TestWtSetupFile.cs index ed71d85..bfe60c6 100644 --- a/src/Svrooij.WinTuner.CmdLets/Commands/TestWtSetupFile.cs +++ b/src/Svrooij.WinTuner.CmdLets/Commands/TestWtSetupFile.cs @@ -1,19 +1,11 @@ using Microsoft.Extensions.Logging; -using Microsoft.Graph.Beta; using Svrooij.PowerShell.DependencyInjection; -using System; using System.IO; using System.Linq; using System.Management.Automation; -using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using WingetIntune; -using WingetIntune.Graph; -using WingetIntune.Intune; -using WingetIntune.Models; using WingetIntune.Testing; -using GraphModels = Microsoft.Graph.Beta.Models; namespace Svrooij.WinTuner.CmdLets.Commands; /// @@ -29,16 +21,11 @@ namespace Svrooij.WinTuner.CmdLets.Commands; [OutputType(typeof(string))] public class TestWtSetupFile : DependencyCmdlet { - /// - /// The absolute path to your setup file - /// - [Parameter( - Mandatory = true, - Position = 0, - ValueFromPipeline = false, - ValueFromPipelineByPropertyName = true, - HelpMessage = "Absolute path to your setup file")] - public string? SetupFile { get; set; } + [ServiceDependency] + private ILogger? logger; + + [ServiceDependency] + private WindowsSandbox? sandbox; /// /// Override the installer arguments @@ -51,6 +38,16 @@ public class TestWtSetupFile : DependencyCmdlet HelpMessage = "Override the installer arguments")] public string? InstallerArguments { get; set; } + /// + /// The absolute path to your setup file + /// + [Parameter( + Mandatory = true, + Position = 0, + ValueFromPipeline = false, + ValueFromPipelineByPropertyName = true, + HelpMessage = "Absolute path to your setup file")] + public string? SetupFile { get; set; } /// /// Sleep for x seconds before closing /// @@ -58,23 +55,11 @@ public class TestWtSetupFile : DependencyCmdlet Mandatory = false, HelpMessage = "Sleep for x seconds before auto shutdown")] public int? Sleep { get; set; } - - [ServiceDependency] - private ILogger? logger; - - [ServiceDependency] - private WindowsSandbox? sandbox; - - [ServiceDependency] - private MetadataManager? metadataManager; - /// public override async Task ProcessRecordAsync(CancellationToken cancellationToken) { - - var sandboxFile = await sandbox!.PrepareSandboxForInstaller(SetupFile!, InstallerArguments, Sleep, cancellationToken); - logger?.LogDebug("Sandbox file created at {sandboxFile}", sandboxFile); + logger?.LogDebug("Sandbox file created at {SandboxFile}", sandboxFile); var result = await sandbox.RunSandbox(sandboxFile, true, cancellationToken); if (result is null) { diff --git a/src/Svrooij.WinTuner.CmdLets/Svrooij.WinTuner.CmdLets.dll-Help.xml b/src/Svrooij.WinTuner.CmdLets/Svrooij.WinTuner.CmdLets.dll-Help.xml index 04d3033..5279449 100644 --- a/src/Svrooij.WinTuner.CmdLets/Svrooij.WinTuner.CmdLets.dll-Help.xml +++ b/src/Svrooij.WinTuner.CmdLets/Svrooij.WinTuner.CmdLets.dll-Help.xml @@ -1844,7 +1844,7 @@ None - + PackageFolder The folder to store the package in @@ -1940,10 +1940,22 @@ None + + InstallerArguments + + Override the installer arguments + + String + + String + + + None + - + PackageFolder The folder to store the package in @@ -2051,6 +2063,18 @@ None + + InstallerArguments + + Override the installer arguments + + String + + String + + + None + diff --git a/src/Svrooij.WinTuner.CmdLets/docs/New-WtWingetPackage.md b/src/Svrooij.WinTuner.CmdLets/docs/New-WtWingetPackage.md index 2824d26..85dab68 100644 --- a/src/Svrooij.WinTuner.CmdLets/docs/New-WtWingetPackage.md +++ b/src/Svrooij.WinTuner.CmdLets/docs/New-WtWingetPackage.md @@ -13,9 +13,10 @@ Create intunewin file from Winget installer ## SYNTAX ``` -New-WtWingetPackage [-PackageId] [[-PackageFolder] ] [[-Version] ] +New-WtWingetPackage [-PackageId] [-PackageFolder] [[-Version] ] [[-TempFolder] ] [-Architecture ] [-InstallerContext ] - [-PackageScript ] [-Locale ] [-ProgressAction ] [] + [-PackageScript ] [-Locale ] [-InstallerArguments ] + [-ProgressAction ] [] ``` ## DESCRIPTION @@ -40,7 +41,7 @@ Type: String Parameter Sets: (All) Aliases: -Required: False +Required: True Position: 1 Default value: None Accept pipeline input: True (ByPropertyName, ByValue) @@ -167,6 +168,21 @@ Accept pipeline input: True (ByPropertyName, ByValue) Accept wildcard characters: False ``` +### -InstallerArguments +Override the installer arguments + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: False +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + ### CommonParameters This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). diff --git a/src/WingetIntune/Intune/IntuneManager.cs b/src/WingetIntune/Intune/IntuneManager.cs index 4315522..6944df2 100644 --- a/src/WingetIntune/Intune/IntuneManager.cs +++ b/src/WingetIntune/Intune/IntuneManager.cs @@ -72,7 +72,7 @@ public IntuneManager(ILoggerFactory? loggerFactory, IFileManager fileManager, IP await WriteReadmeAsync(packageFolder, packageInfo, cancellationToken); await WritePackageInfo(packageFolder, packageInfo, cancellationToken); - return new Models.WingetPackage(packageInfo, packageFolder, intunePackage!); + return new Models.WingetPackage(packageInfo, packageFolder, intunePackage!) { InstallerArguments = packageInfo.InstallCommandLine?.Substring(packageInfo.InstallerFilename?.Length + 3 ?? 0), InstallerFile = packageInfo.InstallerFilename }; } /// @@ -183,7 +183,7 @@ await fileManager.WriteAllTextAsync( await WritePackageInfo(packageFolder, packageInfo, cancellationToken); await WriteReadmeAsync(packageFolder, packageInfo, cancellationToken); - return new WingetPackage(packageInfo, packageFolder, intuneFile); + return new WingetPackage(packageInfo, packageFolder, intuneFile) { InstallerFile = packageInfo.InstallerFilename, InstallerArguments = packageInfo.InstallCommandLine?.Substring(packageInfo.InstallerFilename?.Length + 3 ?? 0) }; } private static string GetPsCommandContent(string command, string successSearch, string message, string? packageId = null, string? action = null) @@ -521,7 +521,7 @@ private void ComputeInstallerDetails(ref PackageInfo package, PackageOptions pac package.InstallerContext = installer.ParseInstallerContext() == InstallerContext.Unknown ? (package.InstallerContext ?? packageOptions.InstallerContext) : installer.ParseInstallerContext(); package.InstallerType = installer.ParseInstallerType(); package.Installer = installer; - if (!package.InstallerType.IsMsi() || packageOptions.PackageScript == true) + if (!package.InstallerType.IsMsi() || packageOptions.PackageScript) { ComputeInstallerCommands(ref package, packageOptions); } @@ -549,7 +549,7 @@ private void ComputeInstallerDetails(ref PackageInfo package, PackageOptions pac private static readonly Dictionary DefaultInstallerSwitches = new() { { InstallerType.Inno, "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" }, - { InstallerType.Burn, "/quiet /norestart" }, + { InstallerType.Burn, "/quiet /norestart /install" }, { InstallerType.Nullsoft, "/S" }, }; @@ -565,7 +565,7 @@ private void ComputeInstallerCommands(ref PackageInfo package, PackageOptions pa // And it also helps with installers that otherwise would just not install silently or install at all if (packageOptions.PackageScript != true) { - string? installerSwitches = package.Installer?.InstallerSwitches?.GetPreferred(); + string? installerSwitches = packageOptions.OverrideArguments ?? package.Installer?.InstallerSwitches?.GetPreferred(); switch (package.InstallerType) { case InstallerType.Inno: @@ -574,14 +574,19 @@ private void ComputeInstallerCommands(ref PackageInfo package, PackageOptions pa installerSwitches += " " + DefaultInstallerSwitches[InstallerType.Inno]; installerSwitches = installerSwitches.Trim(); } - package.InstallCommandLine = $"\"{package.InstallerFilename}\" {installerSwitches ?? DefaultInstallerSwitches[InstallerType.Inno]}"; + package.InstallCommandLine = $"\"{package.InstallerFilename}\" {installerSwitches}"; // Don't know the uninstall command // Configure the uninstall command for Inno Setup //package.UninstallCommandLine = $"\"{package.InstallerFilename}\" /VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP- /D={{0}}"; break; case InstallerType.Burn: - package.InstallCommandLine = $"\"{package.InstallerFilename}\" {installerSwitches ?? DefaultInstallerSwitches[InstallerType.Burn]}"; + if (installerSwitches?.Contains("/quiet") != true) + { + installerSwitches += " " + DefaultInstallerSwitches[InstallerType.Burn]; + installerSwitches = string.Join(" ", installerSwitches.Split(' ').Distinct()).Trim(); + } + package.InstallCommandLine = $"\"{package.InstallerFilename}\" {installerSwitches}"; // Have to check the uninstall command package.UninstallCommandLine = $"\"{package.InstallerFilename}\" /quiet /norestart /uninstall /passive"; // /burn.ignoredependencies=\"{package.PackageIdentifier}\" break; diff --git a/src/WingetIntune/Models/PackageOptions.cs b/src/WingetIntune/Models/PackageOptions.cs index 58e835b..72f9b03 100644 --- a/src/WingetIntune/Models/PackageOptions.cs +++ b/src/WingetIntune/Models/PackageOptions.cs @@ -6,5 +6,6 @@ public class PackageOptions public Architecture Architecture { get; init; } public bool PackageScript { get; init; } public string? Locale { get; init; } + public string? OverrideArguments { get; init; } public static PackageOptions Create() => new PackageOptions { Architecture = Architecture.X64, InstallerContext = InstallerContext.System, PackageScript = false }; } diff --git a/src/WingetIntune/Models/WingetPackage.cs b/src/WingetIntune/Models/WingetPackage.cs index 50b5092..d04f873 100644 --- a/src/WingetIntune/Models/WingetPackage.cs +++ b/src/WingetIntune/Models/WingetPackage.cs @@ -34,4 +34,14 @@ internal WingetPackage(PackageInfo packageInfo, string packageFolder, string pac /// The filename of the intunewin file /// public string PackageFile { get; set; } + + /// + /// Installer filename + /// + public string? InstallerFile { get; set; } + + /// + /// Installer arguments + /// + public string? InstallerArguments { get; set; } }