Skip to content

Commit

Permalink
Override arguments for installers (#96)
Browse files Browse the repository at this point in the history
* Enable overriding arguments
* Make burn arguments better
  • Loading branch information
svrooij authored Jul 30, 2024
1 parent c6b33c6 commit 6c877cd
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 50 deletions.
14 changes: 13 additions & 1 deletion src/Svrooij.WinTuner.CmdLets/Commands/NewWtWingetPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class NewWtWingetPackage : DependencyCmdlet<Startup>
/// The folder to store the package in
/// </summary>
[Parameter(
Mandatory = false,
Mandatory = true,
Position = 1,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
Expand Down Expand Up @@ -107,6 +107,17 @@ public class NewWtWingetPackage : DependencyCmdlet<Startup>
HelpMessage = "The desired locale, if available (eg. 'en-US')")]
public string? Locale { get; set; }

/// <summary>
/// Override the installer arguments
/// </summary>
[Parameter(
Mandatory = false,
Position = 8,
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = false,
HelpMessage = "Override the installer arguments")]
public string? InstallerArguments { get; set; }

[ServiceDependency]
private ILogger<NewWtWingetPackage> logger;

Expand Down Expand Up @@ -152,6 +163,7 @@ public override async Task ProcessRecordAsync(CancellationToken cancellationToke
InstallerContext = InstallerContext,
PackageScript = PackageScript ?? false,
Locale = Locale,
OverrideArguments = InstallerArguments
},
cancellationToken: cancellationToken);

Expand Down
6 changes: 0 additions & 6 deletions src/Svrooij.WinTuner.CmdLets/Commands/TestWtIntuneWin.cs
Original file line number Diff line number Diff line change
@@ -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;
/// <summary>
Expand Down
47 changes: 16 additions & 31 deletions src/Svrooij.WinTuner.CmdLets/Commands/TestWtSetupFile.cs
Original file line number Diff line number Diff line change
@@ -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;
/// <summary>
Expand All @@ -29,16 +21,11 @@ namespace Svrooij.WinTuner.CmdLets.Commands;
[OutputType(typeof(string))]
public class TestWtSetupFile : DependencyCmdlet<Startup>
{
/// <summary>
/// <para type="description">The absolute path to your setup file</para>
/// </summary>
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Absolute path to your setup file")]
public string? SetupFile { get; set; }
[ServiceDependency]
private ILogger<TestWtSetupFile>? logger;

[ServiceDependency]
private WindowsSandbox? sandbox;

/// <summary>
/// <para type="description">Override the installer arguments</para>
Expand All @@ -51,30 +38,28 @@ public class TestWtSetupFile : DependencyCmdlet<Startup>
HelpMessage = "Override the installer arguments")]
public string? InstallerArguments { get; set; }

/// <summary>
/// <para type="description">The absolute path to your setup file</para>
/// </summary>
[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipeline = false,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Absolute path to your setup file")]
public string? SetupFile { get; set; }
/// <summary>
/// <para type="description">Sleep for x seconds before closing</para>
/// </summary>
[Parameter(
Mandatory = false,
HelpMessage = "Sleep for x seconds before auto shutdown")]
public int? Sleep { get; set; }

[ServiceDependency]
private ILogger<TestWtSetupFile>? logger;

[ServiceDependency]
private WindowsSandbox? sandbox;

[ServiceDependency]
private MetadataManager? metadataManager;

/// <inheritdoc/>
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)
{
Expand Down
28 changes: 26 additions & 2 deletions src/Svrooij.WinTuner.CmdLets/Svrooij.WinTuner.CmdLets.dll-Help.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@
</dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName, ByValue)" position="1" aliases="none">
<command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName, ByValue)" position="1" aliases="none">
<maml:name>PackageFolder</maml:name>
<maml:description>
<maml:para>The folder to store the package in</maml:para>
Expand Down Expand Up @@ -1940,10 +1940,22 @@
</dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
<maml:name>InstallerArguments</maml:name>
<maml:description>
<maml:para>Override the installer arguments</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
<dev:type>
<maml:name>String</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
</command:syntaxItem>
</command:syntax>
<command:parameters>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName, ByValue)" position="1" aliases="none">
<command:parameter required="true" variableLength="true" globbing="false" pipelineInput="True (ByPropertyName, ByValue)" position="1" aliases="none">
<maml:name>PackageFolder</maml:name>
<maml:description>
<maml:para>The folder to store the package in</maml:para>
Expand Down Expand Up @@ -2051,6 +2063,18 @@
</dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="named" aliases="none">
<maml:name>InstallerArguments</maml:name>
<maml:description>
<maml:para>Override the installer arguments</maml:para>
</maml:description>
<command:parameterValue required="true" variableLength="false">String</command:parameterValue>
<dev:type>
<maml:name>String</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
</command:parameters>
<command:inputTypes>
<command:inputType>
Expand Down
22 changes: 19 additions & 3 deletions src/Svrooij.WinTuner.CmdLets/docs/New-WtWingetPackage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ Create intunewin file from Winget installer
## SYNTAX

```
New-WtWingetPackage [-PackageId] <String> [[-PackageFolder] <String>] [[-Version] <String>]
New-WtWingetPackage [-PackageId] <String> [-PackageFolder] <String> [[-Version] <String>]
[[-TempFolder] <String>] [-Architecture <Architecture>] [-InstallerContext <InstallerContext>]
[-PackageScript <Boolean>] [-Locale <String>] [-ProgressAction <ActionPreference>] [<CommonParameters>]
[-PackageScript <Boolean>] [-Locale <String>] [-InstallerArguments <String>]
[-ProgressAction <ActionPreference>] [<CommonParameters>]
```

## DESCRIPTION
Expand All @@ -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)
Expand Down Expand Up @@ -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).
Expand Down
19 changes: 12 additions & 7 deletions src/WingetIntune/Intune/IntuneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/// <summary>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -549,7 +549,7 @@ private void ComputeInstallerDetails(ref PackageInfo package, PackageOptions pac
private static readonly Dictionary<InstallerType, string> DefaultInstallerSwitches = new()
{
{ InstallerType.Inno, "/VERYSILENT /SUPPRESSMSGBOXES /NORESTART /SP-" },
{ InstallerType.Burn, "/quiet /norestart" },
{ InstallerType.Burn, "/quiet /norestart /install" },
{ InstallerType.Nullsoft, "/S" },
};

Expand All @@ -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:
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/WingetIntune/Models/PackageOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
10 changes: 10 additions & 0 deletions src/WingetIntune/Models/WingetPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ internal WingetPackage(PackageInfo packageInfo, string packageFolder, string pac
/// The filename of the intunewin file
/// </summary>
public string PackageFile { get; set; }

/// <summary>
/// Installer filename
/// </summary>
public string? InstallerFile { get; set; }

/// <summary>
/// Installer arguments
/// </summary>
public string? InstallerArguments { get; set; }
}

0 comments on commit 6c877cd

Please sign in to comment.