Skip to content

Commit

Permalink
Fix formatting (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasNieto authored Oct 8, 2024
1 parent ed09031 commit 65d243d
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 147 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog][keep-a-changelog],
and this project adheres to [Semantic Versioning][semver].

[keep-a-changelog]: https://keepachangelog.com/en/1.0.0/
[semver]: https://semver.org/spec/v2.0.0.html

## [Unreleased]

## [0.3.0] - 2023-07-18

# Added
### Added

- Add Install-Package (#21)
- Add Uninstall-Package (#21)
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Thomas Nieto
Copyright (c) 2024 Thomas Nieto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@

## Install AnyPackage.Msu

```PowerShell
```powershell
Install-PSResource AnyPackage.Msu
```

## Import AnyPackage.Msu

```PowerShell
```powershell
Import-Module AnyPackage.Msu
```

## Sample usages

### Get list of installed packages

```powerShell
```powershell
Get-Package -Name KB12456789
```

Expand Down
2 changes: 1 addition & 1 deletion src/AnyPackage.Msu.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
CompatiblePSEditions = @('Desktop', 'Core')
GUID = '758cc272-d0a4-49e5-9a24-7d9e730f60e1'
Author = 'Thomas Nieto'
Copyright = '(c) 2023 Thomas Nieto. All rights reserved.'
Copyright = '(c) 2024 Thomas Nieto. All rights reserved.'
Description = 'Windows Msu provider for AnyPackage.'
PowerShellVersion = '5.1'
RequiredModules = @(
Expand Down
24 changes: 12 additions & 12 deletions src/code/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

using System;
using System.Management.Automation;

using static AnyPackage.Provider.PackageProviderManager;

namespace AnyPackage.Provider.Msu
namespace AnyPackage.Provider.Msu;

public sealed class Init : IModuleAssemblyInitializer, IModuleAssemblyCleanup
{
public sealed class Init : IModuleAssemblyInitializer, IModuleAssemblyCleanup
private readonly Guid _id = new Guid("314633fe-c7e9-4eeb-824b-382a8a4e92b8");

public void OnImport()
{
private readonly Guid _id = new Guid("314633fe-c7e9-4eeb-824b-382a8a4e92b8");

public void OnImport()
{
RegisterProvider(_id, typeof(MsuProvider), "AnyPackage.Msu");
}
RegisterProvider(_id, typeof(MsuProvider), "AnyPackage.Msu");
}

public void OnRemove(PSModuleInfo psModuleInfo)
{
UnregisterProvider(_id);
}
public void OnRemove(PSModuleInfo psModuleInfo)
{
UnregisterProvider(_id);
}
}
252 changes: 126 additions & 126 deletions src/code/MsuProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,163 +3,163 @@
// terms of the MIT license.

using Microsoft.Deployment.Compression.Cab;

using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Management.Automation;

namespace AnyPackage.Provider.Msu
namespace AnyPackage.Provider.Msu;

[PackageProvider("Msu", PackageByName = false, FileExtensions = new string[] { ".msu" })]
public sealed class MsuProvider : PackageProvider, IFindPackage, IGetPackage, IInstallPackage, IUninstallPackage
{
[PackageProvider("Msu", PackageByName = false, FileExtensions = new string[] { ".msu" })]
public sealed class MsuProvider : PackageProvider, IFindPackage, IGetPackage, IInstallPackage, IUninstallPackage
public void FindPackage(PackageRequest request)
{
public void FindPackage(PackageRequest request)
{
var file = new CabInfo(request.Path).GetFiles()
.Where(x => Path.GetExtension(x.Name) == ".txt")
.FirstOrDefault();

if (file is null)
{
return;
}
var file = new CabInfo(request.Path).GetFiles()
.Where(x => Path.GetExtension(x.Name) == ".txt")
.FirstOrDefault();

string line;
Dictionary<string, object> metadata = new Dictionary<string, object>();
using var reader = file.OpenText();
if (file is null)
{
return;
}

while ((line = reader.ReadLine()) is not null)
{
var values = line.Split(new char[] { '=' }, 2);
var key = values[0].Replace(" ", "");
string line;
Dictionary<string, object> metadata = new Dictionary<string, object>();
using var reader = file.OpenText();

// Remove quotes around value
var value = values[1].Substring(1, values[1].Length - 2);
while ((line = reader.ReadLine()) is not null)
{
var values = line.Split(new char[] { '=' }, 2);
var key = values[0].Replace(" ", "");

metadata.Add(key, value);
}
// Remove quotes around value
var value = values[1].Substring(1, values[1].Length - 2);

if (metadata.ContainsKey("KBArticleNumber"))
{
var kb = string.Format("KB{0}", metadata["KBArticleNumber"]);
var source = new PackageSourceInfo(request.Path, request.Path, ProviderInfo);
var package = new PackageInfo(kb, null, source, (string)metadata["PackageType"], null, metadata, ProviderInfo);
request.WritePackage(package);
}
metadata.Add(key, value);
}

public void GetPackage(PackageRequest request)
if (metadata.ContainsKey("KBArticleNumber"))
{
var quickFix = new ManagementObjectSearcher(@"root\cimv2", "select * from Win32_QuickFixEngineering");

foreach (var hotFix in quickFix.Get())
{
if (request.IsMatch((string)hotFix["HotFixID"]) && (request.Version is null || request.Version.ToString() == "*"))
{
var package = new PackageInfo((string)hotFix["HotFixID"], null, (string)hotFix["Description"], ProviderInfo);
request.WritePackage(package);
}
}
var kb = string.Format("KB{0}", metadata["KBArticleNumber"]);
var source = new PackageSourceInfo(request.Path, request.Path, ProviderInfo);
var package = new PackageInfo(kb, null, source, (string)metadata["PackageType"], null, metadata, ProviderInfo);
request.WritePackage(package);
}
}

public void InstallPackage(PackageRequest request)
{
using var powershell = PowerShell.Create(RunspaceMode.CurrentRunspace);
powershell.AddCommand("Find-Package").AddParameter("Path", request.Path);
var package = powershell.Invoke<PackageInfo>().FirstOrDefault();
public void GetPackage(PackageRequest request)
{
var quickFix = new ManagementObjectSearcher(@"root\cimv2", "select * from Win32_QuickFixEngineering");

if (package is null)
foreach (var hotFix in quickFix.Get())
{
if (request.IsMatch((string)hotFix["HotFixID"]) && (request.Version is null || request.Version.ToString() == "*"))
{
return;
var package = new PackageInfo((string)hotFix["HotFixID"], null, (string)hotFix["Description"], ProviderInfo);
request.WritePackage(package);
}
}
}

using var process = new Process();
process.StartInfo.FileName = "wusa.exe";
process.StartInfo.Arguments = $"{request.Path} /quiet /norestart";
process.StartInfo.UseShellExecute = true;
process.Start();
process.WaitForExit();

if (!ValidateExitCode(process.ExitCode, request))
{
return;
}
public void InstallPackage(PackageRequest request)
{
using var powershell = PowerShell.Create(RunspaceMode.CurrentRunspace);
powershell.AddCommand("Find-Package").AddParameter("Path", request.Path);
var package = powershell.Invoke<PackageInfo>().FirstOrDefault();

request.WritePackage(package);
if (package is null)
{
return;
}

public void UninstallPackage(PackageRequest request)
using var process = new Process();
process.StartInfo.FileName = "wusa.exe";
process.StartInfo.Arguments = $"{request.Path} /quiet /norestart";
process.StartInfo.UseShellExecute = true;
process.Start();
process.WaitForExit();

if (!ValidateExitCode(process.ExitCode, request))
{
using var process = new Process();
process.StartInfo.FileName = "wusa.exe";
var kb = request.Name.Replace("KB", "");
process.StartInfo.Arguments = $"/uninstall /kb:{kb} /norestart";
process.StartInfo.UseShellExecute = true;
process.Start();
process.WaitForExit();

if (!ValidateExitCode(process.ExitCode, request))
{
return;
}
return;
}

var package = request.Package is not null ? request.Package : new PackageInfo(request.Name, ProviderInfo);
request.WritePackage(package);
request.WritePackage(package);
}

public void UninstallPackage(PackageRequest request)
{
using var process = new Process();
process.StartInfo.FileName = "wusa.exe";
var kb = request.Name.Replace("KB", "");
process.StartInfo.Arguments = $"/uninstall /kb:{kb} /norestart";
process.StartInfo.UseShellExecute = true;
process.Start();
process.WaitForExit();

if (!ValidateExitCode(process.ExitCode, request))
{
return;
}

private bool ValidateExitCode(int exitCode, PackageRequest request)
var package = request.Package is not null ? request.Package : new PackageInfo(request.Name, ProviderInfo);
request.WritePackage(package);
}

private bool ValidateExitCode(int exitCode, PackageRequest request)
{
// https://learn.microsoft.com/en-us/windows/win32/wua_sdk/wua-success-and-error-codes-
// http://inetexplorer.mvps.org/archive/windows_update_codes.htm
switch (exitCode)
{
// https://learn.microsoft.com/en-us/windows/win32/wua_sdk/wua-success-and-error-codes-
// http://inetexplorer.mvps.org/archive/windows_update_codes.htm
switch (exitCode)
{
case 0:
request.WriteVerbose("The operation completed successfully.");
return true;

case 1223:
request.WriteVerbose("The operation was canceled by the user.");
return false;

case 0x00240001:
request.WriteVerbose("WUA was stopped successfully.");
return true;

case 0x00240002:
request.WriteVerbose("WUA updated itself.");
return true;

case 0x00240003:
request.WriteWarning("The operation completed successfully but errors occurred applying the updates.");
return true;

case 0x00240005:
case 3010:
request.WriteWarning("The system must be restarted to complete installation of the update.");
return true;

case 0x00240006:
request.WriteVerbose("The update to be installed is already installed on the system.");
return false;

case 0x00240007:
request.WriteVerbose("The update to be removed is not installed on the system.");
return false;

case 0x00240008:
request.WriteVerbose("The update to be downloaded has already been downloaded.");
return true;

case 0x00242015:
request.WriteVerbose("The installation operation for the update is still in progress.");
return true;

default:
request.WriteVerbose($"The operation failed with exit code: {exitCode}");
return false;
}
case 0:
request.WriteVerbose("The operation completed successfully.");
return true;

case 1223:
request.WriteVerbose("The operation was canceled by the user.");
return false;

case 0x00240001:
request.WriteVerbose("WUA was stopped successfully.");
return true;

case 0x00240002:
request.WriteVerbose("WUA updated itself.");
return true;

case 0x00240003:
request.WriteWarning("The operation completed successfully but errors occurred applying the updates.");
return true;

case 0x00240005:
case 3010:
request.WriteWarning("The system must be restarted to complete installation of the update.");
return true;

case 0x00240006:
request.WriteVerbose("The update to be installed is already installed on the system.");
return false;

case 0x00240007:
request.WriteVerbose("The update to be removed is not installed on the system.");
return false;

case 0x00240008:
request.WriteVerbose("The update to be downloaded has already been downloaded.");
return true;

case 0x00242015:
request.WriteVerbose("The installation operation for the update is still in progress.");
return true;

default:
request.WriteVerbose($"The operation failed with exit code: {exitCode}");
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/code/MsuProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net461</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<Copyright2023 Thomas Nieto. All rights reserved.</Copyright>
<Copyright2024 Thomas Nieto. All rights reserved.</Copyright>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 65d243d

Please sign in to comment.