Skip to content

Commit

Permalink
Fixing install and adding reboot flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Bpoe committed Apr 6, 2023
1 parent 293302f commit 3abb149
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/wucli/CommandExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,27 @@ public static RootCommand AddInstallCommand(this RootCommand rootCommand)
var criteriaOption = new Option<string>("--criteria", "The criteria for updates (Default: \"IsInstalled=0 AND IsHidden=0\"");
criteriaOption.AddAlias("-c");

var rebootOption = new Option<bool>("--reboot", "Reboot the machine if any updates require a reboot.");
rebootOption.AddAlias("-r");

var installCommand = new Command("install", "Install all available updates on this machine.");
installCommand.AddOption(criteriaOption);
installCommand.AddOption(rebootOption);
rootCommand.Add(installCommand);
installCommand.SetHandler(
(criteria) =>
(criteria, reboot) =>
{
var options = new WuOptions
{
Criteria = criteria,
Reboot = reboot,
};
var result = WindowsUpdates.Install(options).Result;
Environment.Exit(result);
},
criteriaOption);
criteriaOption,
rebootOption);

return rootCommand;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wucli/WindowsUpdate/WindowsUpdateClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public WindowsUpdateClient()
}

public WindowsUpdateClient(ServerSelection updateServerSelection)
: this(updateServerSelection, new UpdateSession { ClientApplicationID = "AzFacts" })
: this(updateServerSelection, new UpdateSession { ClientApplicationID = "wucli" })
{
}

Expand Down
38 changes: 37 additions & 1 deletion src/wucli/WindowsUpdates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,46 @@ public static async Task<int> Install(WuOptions options)
var result = await client
.SearchAsync(options.Criteria, CancellationToken.None);

if (result.Updates.Count == 0)
{
Console.WriteLine("No updates to install.");
return 0;
}

Console.WriteLine($"{result.Updates.Count} updates to be installed.");

var downloadResults = await client
.DownloadAsync(result.Updates, CancellationToken.None);

if (downloadResults.ResultCode == OperationResultCode.orcFailed)
{
Console.WriteLine($"Download failed. 0x{downloadResults.HResult.ToString("X")}");
return -1;
}

var installResults = await client
.InstallAsync(result.Updates, CancellationToken.None);

if (installResults.ResultCode == OperationResultCode.orcFailed)
{
Console.WriteLine($"Installation failed. 0x{installResults.HResult.ToString("X")}");
return -1;
}

Console.WriteLine("Installation complete.");

if (installResults.RebootRequired)
{
Console.WriteLine("Reboot Required");
if (options.Reboot)
{
Console.WriteLine("Rebooting...");
NativeMethods.Reboot();
}
else
{
Console.WriteLine("Reboot Required");
}

return 1;
}

Expand All @@ -38,6 +68,12 @@ public static async Task<int> ListAvailable(WuOptions options)
var result = await new WindowsUpdateClient()
.SearchAsync(options.Criteria, CancellationToken.None);

if (result.Updates.Count == 0)
{
Console.WriteLine("No updates available.");
return 0;
}

var updates = result
.Updates
.Select(o => (IUpdate)o);
Expand Down
2 changes: 2 additions & 0 deletions src/wucli/WuOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public class WuOptions
{
public string Criteria { get; set; } = "IsInstalled=0 AND IsHidden=0";

public bool Reboot { get; set; } = false;
}
4 changes: 3 additions & 1 deletion src/wucli/wucli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<RootNamespace>wucli</RootNamespace>
<AssemblyName>wucli</AssemblyName>
<Version>0.1.0</Version>

<PublishSingleFile>true</PublishSingleFile>
<!-- IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract -->
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<PlatformTarget>AnyCPU</PlatformTarget>
<!-- TargetLatestRuntimePatch>False</TargetLatestRuntimePatch -->
<!-- RuntimeFrameworkVersion>6.0.0</RuntimeFrameworkVersion -->

Expand Down

0 comments on commit 3abb149

Please sign in to comment.