PowerShellHelpers.cs (3590B)
1 // ----------------------------------------------------------------------------- 2 // <copyright file="PowerShellHelpers.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.Helpers 8 { 9 using System.Collections; 10 using Microsoft.Management.Configuration.Processor.Helpers; 11 using Microsoft.PowerShell.Commands; 12 using static Microsoft.Management.Configuration.Processor.PowerShell.Constants.PowerShellConstants; 13 14 /// <summary> 15 /// General PowerShell helpers. 16 /// </summary> 17 internal static class PowerShellHelpers 18 { 19 /// <summary> 20 /// Creates a module specification object. 21 /// </summary> 22 /// <param name="moduleName">Module name.</param> 23 /// <param name="version">Optional version.</param> 24 /// <param name="minVersion">Optional min version.</param> 25 /// <param name="maxVersion">Optional max version.</param> 26 /// <param name="guid">Optional guid.</param> 27 /// <returns>ModuleSpecification.</returns> 28 public static ModuleSpecification CreateModuleSpecification( 29 string moduleName, 30 string? version = null, 31 string? minVersion = null, 32 string? maxVersion = null, 33 string? guid = null) 34 { 35 if (version is null && 36 minVersion is null && 37 maxVersion is null) 38 { 39 // Otherwise will fail with MissingMemberException... 40 return new ModuleSpecification(moduleName); 41 } 42 43 var moduleInfo = new Hashtable 44 { 45 { Parameters.ModuleName, moduleName }, 46 }; 47 48 if (!string.IsNullOrEmpty(version)) 49 { 50 var semanticVersion = new SemanticVersion(version); 51 moduleInfo.Add(Parameters.RequiredVersion, semanticVersion.Version); 52 } 53 54 if (!string.IsNullOrEmpty(minVersion)) 55 { 56 var semanticVersion = new SemanticVersion(minVersion); 57 moduleInfo.Add(Parameters.ModuleVersion, semanticVersion.Version); 58 } 59 60 if (!string.IsNullOrEmpty(maxVersion)) 61 { 62 var semanticVersion = new SemanticVersion(maxVersion); 63 64 // For some reason, the constructor of ModuleSpecification that takes 65 // a hashtable calls ModuleCmdletBase.GetMaximumVersion. This method will 66 // validate the max version and replace * for 999999999 only if its the last 67 // char in the string. But then the returned value is not assigned to the 68 // ModuleSpecification's MaximumVersion property. If we want to set a 69 // MaximumVersion with a wildcard and pass this to Install-Module it will 70 // fail with "Cannot convert value 'x.*' to type 'System.Version'." 71 moduleInfo.Add(Parameters.MaximumVersion, semanticVersion.Version.ToString()); 72 } 73 74 if (!string.IsNullOrEmpty(guid)) 75 { 76 moduleInfo.Add(Parameters.Guid, guid); 77 } 78 79 // Using the Hashtable constructor will verify that RequiredVersion is used properly. 80 return new ModuleSpecification(moduleInfo); 81 } 82 } 83 }