Remove code related to #requires -pssnapin (#19320)

This commit is contained in:
Steve Lee
2023-04-03 14:56:40 -07:00
committed by GitHub
parent 04b93af4ae
commit 7c0fe762a9
4 changed files with 8 additions and 121 deletions
@@ -316,92 +316,25 @@ namespace System.Management.Automation
}
}
private static Collection<string> GetPSSnapinNames(IEnumerable<PSSnapInSpecification> PSSnapins)
{
Collection<string> result = new Collection<string>();
foreach (var PSSnapin in PSSnapins)
{
result.Add(BuildPSSnapInDisplayName(PSSnapin));
}
return result;
}
private CommandProcessorBase CreateScriptProcessorForSingleShell(ExternalScriptInfo scriptInfo, ExecutionContext context, bool useLocalScope, SessionStateInternal sessionState)
{
VerifyScriptRequirements(scriptInfo, Context);
IEnumerable<PSSnapInSpecification> requiresPSSnapIns = scriptInfo.RequiresPSSnapIns;
if (requiresPSSnapIns != null && requiresPSSnapIns.Any())
if (!string.IsNullOrEmpty(scriptInfo.RequiresApplicationID))
{
Collection<string> requiresMissingPSSnapIns = null;
VerifyRequiredSnapins(requiresPSSnapIns, context, out requiresMissingPSSnapIns);
if (requiresMissingPSSnapIns != null)
{
ScriptRequiresException scriptRequiresException =
new ScriptRequiresException(
scriptInfo.Name,
requiresMissingPSSnapIns,
"ScriptRequiresMissingPSSnapIns",
true);
throw scriptRequiresException;
}
}
else
{
// If there were no PSSnapins required but there is a shellID required, then we need
// to error
ScriptRequiresException sre =
new ScriptRequiresException(
scriptInfo.Name,
string.Empty,
string.Empty,
"RequiresShellIDInvalidForSingleShell");
if (!string.IsNullOrEmpty(scriptInfo.RequiresApplicationID))
{
ScriptRequiresException sre =
new ScriptRequiresException(
scriptInfo.Name,
string.Empty,
string.Empty,
"RequiresShellIDInvalidForSingleShell");
throw sre;
}
throw sre;
}
return CreateCommandProcessorForScript(scriptInfo, Context, useLocalScope, sessionState);
}
private static void VerifyRequiredSnapins(IEnumerable<PSSnapInSpecification> requiresPSSnapIns, ExecutionContext context, out Collection<string> requiresMissingPSSnapIns)
{
requiresMissingPSSnapIns = null;
Dbg.Assert(context.InitialSessionState != null, "PowerShell should be hosted with InitialSessionState");
foreach (var requiresPSSnapIn in requiresPSSnapIns)
{
var loadedPSSnapIn = context.InitialSessionState.GetPSSnapIn(requiresPSSnapIn.Name);
if (loadedPSSnapIn is null)
{
requiresMissingPSSnapIns ??= new Collection<string>();
requiresMissingPSSnapIns.Add(BuildPSSnapInDisplayName(requiresPSSnapIn));
}
else
{
// the requires PSSnapin is loaded. now check the PSSnapin version
Dbg.Assert(loadedPSSnapIn.Version != null,
string.Format(
CultureInfo.InvariantCulture,
"Version is null for loaded PSSnapin {0}.", loadedPSSnapIn));
if (requiresPSSnapIn.Version != null)
{
if (!AreInstalledRequiresVersionsCompatible(
requiresPSSnapIn.Version, loadedPSSnapIn.Version))
{
requiresMissingPSSnapIns ??= new Collection<string>();
requiresMissingPSSnapIns.Add(BuildPSSnapInDisplayName(requiresPSSnapIn));
}
}
}
}
}
// This method verifies the following 3 elements of #Requires statement
// #Requires -RunAsAdministrator
// #Requires -PSVersion
@@ -481,32 +414,6 @@ namespace System.Management.Automation
}
}
/// <summary>
/// Used to determine compatibility between the versions in the requires statement and
/// the installed version. The version can be PSSnapin or msh.
/// </summary>
/// <param name="requires">Versions in the requires statement.</param>
/// <param name="installed">Version installed.</param>
/// <returns>
/// true if requires and installed's major version match and requires' minor version
/// is smaller than or equal to installed's
/// </returns>
/// <remarks>
/// In PowerShell V2, script requiring PowerShell 1.0 will fail.
/// </remarks>
private static bool AreInstalledRequiresVersionsCompatible(Version requires, Version installed)
{
return requires.Major == installed.Major && requires.Minor <= installed.Minor;
}
private static string BuildPSSnapInDisplayName(PSSnapInSpecification PSSnapin)
{
return PSSnapin.Version == null ?
PSSnapin.Name :
StringUtil.Format(DiscoveryExceptions.PSSnapInNameVersion,
PSSnapin.Name, PSSnapin.Version);
}
/// <summary>
/// Look up a command using a CommandInfo object and return its CommandProcessorBase.
/// </summary>
@@ -458,15 +458,6 @@ namespace System.Management.Automation
get { return 0; }
}
internal IEnumerable<PSSnapInSpecification> RequiresPSSnapIns
{
get
{
var data = GetRequiresData();
return data?.RequiresPSSnapIns;
}
}
/// <summary>
/// Gets the original contents of the script.
/// </summary>
@@ -753,14 +753,6 @@ namespace System.Management.Automation.Language
/// </summary>
public ReadOnlyCollection<ModuleSpecification> RequiredModules { get; internal set; }
/// <summary>
/// The snapins this script requires, specified like:
/// <code>#requires -PSSnapin Snapin</code>
/// <code>#requires -PSSnapin Snapin -Version 2</code>
/// If no snapins are required, this property is an empty collection.
/// </summary>
public ReadOnlyCollection<PSSnapInSpecification> RequiresPSSnapIns { get; internal set; }
/// <summary>
/// The assemblies this script requires, specified like:
/// <code>#requires -Assembly path\to\foo.dll</code>
@@ -1976,9 +1976,6 @@ namespace System.Management.Automation.Language
RequiredPSEditions = requiredEditions != null
? new ReadOnlyCollection<string>(requiredEditions)
: ScriptRequirements.EmptyEditionCollection,
RequiresPSSnapIns = requiredSnapins != null
? new ReadOnlyCollection<PSSnapInSpecification>(requiredSnapins)
: ScriptRequirements.EmptySnapinCollection,
RequiredAssemblies = requiredAssemblies != null
? new ReadOnlyCollection<string>(requiredAssemblies)
: ScriptRequirements.EmptyAssemblyCollection,