MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Management.Automation;6using System.Management.Automation.Internal;7using System.Management.Automation.Remoting;8using System.Management.Automation.Runspaces;9 10using Microsoft.Win32;11 12namespace Microsoft.PowerShell.Commands13{14 /// <summary>15 /// This enum is used to distinguish two sets of parameters on some of the remoting cmdlets.16 /// </summary>17 internal enum RunspaceParameterSet18 {19 /// <summary>20 /// Use ComputerName parameter set.21 /// </summary>22 ComputerName,23 /// <summary>24 /// Use Runspace Parameter set.25 /// </summary>26 Runspace27 }28 29 /// <summary>30 /// This is a static utility class that performs some of the common chore work for the31 /// the remoting cmdlets.32 /// </summary>33 internal static class RemotingCommandUtil34 {35 internal static bool HasRepeatingRunspaces(PSSession[] runspaceInfos)36 {37 if (runspaceInfos == null)38 {39 throw PSTraceSource.NewArgumentNullException(nameof(runspaceInfos));40 }41 42 if (runspaceInfos.GetLength(0) == 0)43 {44 throw PSTraceSource.NewArgumentException(nameof(runspaceInfos));45 }46 47 for (int i = 0; i < runspaceInfos.GetLength(0); i++)48 {49 for (int k = 0; k < runspaceInfos.GetLength(0); k++)50 {51 if (i != k)52 {53 if (runspaceInfos[i].Runspace.InstanceId == runspaceInfos[k].Runspace.InstanceId)54 {55 return true;56 }57 }58 }59 }60 61 return false;62 }63 64 internal static bool ExceedMaximumAllowableRunspaces(PSSession[] runspaceInfos)65 {66 if (runspaceInfos == null)67 {68 throw PSTraceSource.NewArgumentNullException(nameof(runspaceInfos));69 }70 71 if (runspaceInfos.GetLength(0) == 0)72 {73 throw PSTraceSource.NewArgumentException(nameof(runspaceInfos));74 }75 76 return false;77 }78 79 /// <summary>80 /// Checks the prerequisites for a cmdlet and terminates if the cmdlet81 /// is not valid.82 /// </summary>83 internal static void CheckRemotingCmdletPrerequisites()84 {85#if UNIX86 // TODO: check that PSRP requirements are installed87 return;88#else89 bool notSupported = true;90 const string WSManKeyPath = "Software\\Microsoft\\Windows\\CurrentVersion\\WSMAN\\";91 92 CheckHostRemotingPrerequisites();93 94 try95 {96 // the following registry key defines WSMan compatibility97 // HKLM\Software\Microsoft\Windows\CurrentVersion\WSMAN\ServiceStackVersion98 string wsManStackValue = null;99 RegistryKey wsManKey = Registry.LocalMachine.OpenSubKey(WSManKeyPath);100 if (wsManKey != null)101 {102 wsManStackValue = (string)wsManKey.GetValue("ServiceStackVersion");103 }104 105 Version wsManStackVersion = !string.IsNullOrEmpty(wsManStackValue) ?106 new Version(wsManStackValue.Trim()) :107 System.Management.Automation.Remoting.Client.WSManNativeApi.WSMAN_STACK_VERSION;108 109 // WSMan stack version must be 2.0 or later.110 if (wsManStackVersion >= new Version(2, 0))111 {112 notSupported = false;113 }114 }115 catch (FormatException)116 {117 notSupported = true;118 }119 catch (OverflowException)120 {121 notSupported = true;122 }123 catch (ArgumentException)124 {125 notSupported = true;126 }127 catch (System.Security.SecurityException)128 {129 notSupported = true;130 }131 catch (ObjectDisposedException)132 {133 notSupported = true;134 }135 136 if (notSupported)137 {138 // WSMan is not supported on this platform139 throw new InvalidOperationException(140 "Windows PowerShell remoting features are not enabled or not supported on this machine.\nThis may be because you do not have the correct version of WS-Management installed or this version of Windows does not support remoting currently.\n For more information, type 'get-help about_remote_requirements'.");141 }142#endif143 }144 145 /// <summary>146 /// Facilitates to check if remoting is supported on the host machine.147 /// PowerShell remoting is supported on all Windows SQU's except WinPE.148 /// </summary>149 /// <exception cref="InvalidOperationException">150 /// When PowerShell is hosted on a WinPE machine, the execution151 /// of this API would result in an InvalidOperationException being152 /// thrown, indicating that remoting is not supported on a WinPE machine.153 /// </exception>154 internal static void CheckHostRemotingPrerequisites()155 {156 // A registry key indicates if the SKU is WINPE. If this turns out to be true,157 // then an InValidOperation exception is thrown.158 bool isWinPEHost = Utils.IsWinPEHost();159 if (isWinPEHost)160 {161 // WSMan is not supported on this platform162 // throw new InvalidOperationException(163 // "WinPE does not support Windows PowerShell remoting");164 ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.WinPERemotingNotSupported)), null, ErrorCategory.InvalidOperation, null);165 throw new InvalidOperationException(errorRecord.ToString());166 }167 }168 }169}170 