winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

PowerShellExtensions.cs (3557B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="PowerShellExtensions.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.Management.Configuration.Processor.PowerShell.Extensions
      8 {
      9     using System.Collections.ObjectModel;
     10     using System.Management.Automation;
     11     using System.Text;
     12 
     13     /// <summary>
     14     /// Extensions methods for <see cref="PowerShell"/> class.
     15     /// </summary>
     16     internal static class PowerShellExtensions
     17     {
     18         /// <summary>
     19         /// Calls Invoke with <see cref="ActionPreference.Stop"/> to stop execution of commands.
     20         /// </summary>
     21         /// <param name="pwsh">PowerShell.</param>
     22         /// <returns>Collection of PSObjects representing output.</returns>
     23         public static Collection<PSObject> InvokeAndStopOnError(this PowerShell pwsh)
     24         {
     25             var settings = new PSInvocationSettings()
     26             {
     27                 ErrorActionPreference = ActionPreference.Stop,
     28             };
     29 
     30             return pwsh.Invoke(null, settings);
     31         }
     32 
     33         /// <summary>
     34         /// Calls Invoke with <see cref="ActionPreference.Stop"/> to stop execution of commands.
     35         /// </summary>
     36         /// <typeparam name="T">Type of output object(s) expected from the command invocation.</typeparam>
     37         /// <param name="pwsh">PowerShell.</param>
     38         /// <returns>Collection of the type output representing output.</returns>
     39         public static Collection<T> InvokeAndStopOnError<T>(this PowerShell pwsh)
     40         {
     41             var settings = new PSInvocationSettings()
     42             {
     43                 ErrorActionPreference = ActionPreference.Stop,
     44             };
     45 
     46             return pwsh.Invoke<T>(null, settings);
     47         }
     48 
     49         /// <summary>
     50         /// Gets the error stream message if any.
     51         /// </summary>
     52         /// <param name="pwsh">PowerShell.</param>
     53         /// <returns>Error message. Null if none.</returns>
     54         public static string? GetErrorMessage(this PowerShell pwsh)
     55         {
     56             if (pwsh.HadErrors)
     57             {
     58                 var psStreamBuilder = new StringBuilder();
     59                 foreach (var line in pwsh.Streams.Error)
     60                 {
     61                     psStreamBuilder.AppendLine(line.ToString());
     62                 }
     63 
     64                 return psStreamBuilder.ToString();
     65             }
     66 
     67             return null;
     68         }
     69 
     70         /// <summary>
     71         /// Determines if the given shell contains a property error, meaning that the source of this error is the
     72         /// configuration values and not the configuration unit itself.
     73         /// </summary>
     74         /// <param name="pwsh">The shell to inspect.</param>
     75         /// <returns>True if it only contains property errors; false otherwise.</returns>
     76         public static bool ContainsPropertyError(this PowerShell pwsh)
     77         {
     78             if (!pwsh.HadErrors)
     79             {
     80                 return false;
     81             }
     82 
     83             bool result = true;
     84 
     85             foreach (ErrorRecord? error in pwsh.Streams.Error)
     86             {
     87                 if (error?.FullyQualifiedErrorId == "PropertyAssignmentException")
     88                 {
     89                     result = result && true;
     90                 }
     91             }
     92 
     93             return result;
     94         }
     95     }
     96 }