From 66e85df97f38907554dcf97f5ff4504bfa0aae27 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 27 Sep 2018 14:37:17 -0700 Subject: [PATCH 1/4] Hyper-V team changed the type used to get the runtimeid, so need to check for new type and fall back to old type --- .../remoting/common/RunspaceConnectionInfo.cs | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index 9257d4b1511..087e4141a86 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -3177,12 +3177,18 @@ public void GetContainerProperties() /// /// Dynamically load the Host Compute interop assemblies and return useful types. /// - /// The Microsoft.HyperV.Schema.Compute.System.Properties type. + /// The HCS.Compute.System.Properties type. /// The Microsoft.HostCompute.Interop.HostComputeInterop type. private static void GetHostComputeInteropTypes(out Type computeSystemPropertiesType, out Type hostComputeInteropType) { Assembly schemaAssembly = Assembly.Load(new AssemblyName("Microsoft.HyperV.Schema, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")); - computeSystemPropertiesType = schemaAssembly.GetType("Microsoft.HyperV.Schema.Compute.System.Properties"); + // The type name was changed in newer version of Windows so we check for new one first, + // then fallback to previous type name to support older versions of Windows + computeSystemPropertiesType = schemaAssembly.GetType("HCS.Compute.System.Properties"); + if (computeSystemPropertiesType == null) + { + computeSystemPropertiesType = schemaAssembly.GetType("Microsoft.HyperV.Schema.Compute.System.Properties"); + } Assembly hostComputeInteropAssembly = Assembly.Load(new AssemblyName("Microsoft.HostCompute.Interop, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")); hostComputeInteropType = hostComputeInteropAssembly.GetType("Microsoft.HostCompute.Interop.HostComputeInterop"); @@ -3350,7 +3356,18 @@ private void GetContainerPropertiesInternal() var computeSystemPropertiesHandle = getComputeSystemPropertiesInfo.Invoke(null, new object[] { ComputeSystem }); - RuntimeId = (Guid)computeSystemPropertiesType.GetProperty("RuntimeId").GetValue(computeSystemPropertiesHandle); + // Since Hyper-V changed this from a property to a field, we can optimize for newest Windows to see if it's a field, + // otherwise we fall back to old code to be compatible with older versions of Windows + var fieldInfo = computeSystemPropertiesType.GetField("RuntimeId"); + if (fieldInfo != null) + { + RuntimeId = (Guid)fieldInfo.GetValue(computeSystemPropertiesHandle); + } + else + { + var propertyInfo = computeSystemPropertiesType.GetProperty("RuntimeId"); + RuntimeId = (Guid)propertyInfo.GetValue(computeSystemPropertiesHandle); + } // // Get container object root for Windows Server container. From 223975ce8d1838cc87fcfa14e93ea6a8e42f1804 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 27 Sep 2018 15:03:03 -0700 Subject: [PATCH 2/4] address CodeFactor issue --- .../engine/remoting/common/RunspaceConnectionInfo.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index 087e4141a86..fe86e05ddda 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -3182,6 +3182,7 @@ public void GetContainerProperties() private static void GetHostComputeInteropTypes(out Type computeSystemPropertiesType, out Type hostComputeInteropType) { Assembly schemaAssembly = Assembly.Load(new AssemblyName("Microsoft.HyperV.Schema, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")); + // The type name was changed in newer version of Windows so we check for new one first, // then fallback to previous type name to support older versions of Windows computeSystemPropertiesType = schemaAssembly.GetType("HCS.Compute.System.Properties"); From db88f4b81b2d51158fc22d566aef5f8fe461ac4d Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Wed, 3 Oct 2018 12:45:53 -0700 Subject: [PATCH 3/4] address Paul's feedback --- .../remoting/common/RunspaceConnectionInfo.cs | 13 +++++++++++++ .../resources/RemotingErrorIdStrings.resx | 3 +++ 2 files changed, 16 insertions(+) diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index fe86e05ddda..6548c62841a 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -3189,10 +3189,18 @@ private static void GetHostComputeInteropTypes(out Type computeSystemPropertiesT if (computeSystemPropertiesType == null) { computeSystemPropertiesType = schemaAssembly.GetType("Microsoft.HyperV.Schema.Compute.System.Properties"); + if (computeSystemPropertiesType == null) + { + throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotGetHostInteropTypes)); + } } Assembly hostComputeInteropAssembly = Assembly.Load(new AssemblyName("Microsoft.HostCompute.Interop, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")); hostComputeInteropType = hostComputeInteropAssembly.GetType("Microsoft.HostCompute.Interop.HostComputeInterop"); + if (hostComputeInteropType == null) + { + throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotGetHostInteropTypes)); + } } /// @@ -3367,6 +3375,11 @@ private void GetContainerPropertiesInternal() else { var propertyInfo = computeSystemPropertiesType.GetProperty("RuntimeId"); + if (propertyInfo == null) + { + throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotGetHostInteropTypes)); + } + RuntimeId = (Guid)propertyInfo.GetValue(computeSystemPropertiesHandle); } diff --git a/src/System.Management.Automation/resources/RemotingErrorIdStrings.resx b/src/System.Management.Automation/resources/RemotingErrorIdStrings.resx index bd712339b79..37f976684fd 100644 --- a/src/System.Management.Automation/resources/RemotingErrorIdStrings.resx +++ b/src/System.Management.Automation/resources/RemotingErrorIdStrings.resx @@ -1667,4 +1667,7 @@ All WinRM sessions connected to PowerShell session configurations, such as Micro Information about the process could not be read: '{0}'. + + Host system does not have the correct version of Hyper-V schema. + From bee122130b67c2c2b33f049d479f22cea612f865 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Tue, 9 Oct 2018 11:59:16 -0700 Subject: [PATCH 4/4] address Dongbo's feedback --- .../engine/remoting/common/RunspaceConnectionInfo.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs index 6548c62841a..445b994bea0 100644 --- a/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs +++ b/src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs @@ -3122,7 +3122,7 @@ public void CreateContainerProcess() ContainerId)); case ContainersFeatureNotEnabled: - throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.ContainersFeatureNotEnabled)); + throw new PSInvalidOperationException(RemotingErrorIdStrings.ContainersFeatureNotEnabled); // other errors caught with exception case OtherError: @@ -3163,7 +3163,7 @@ public void GetContainerProperties() break; case ContainersFeatureNotEnabled: - throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.ContainersFeatureNotEnabled)); + throw new PSInvalidOperationException(RemotingErrorIdStrings.ContainersFeatureNotEnabled); case OtherError: throw new PSInvalidOperationException(ErrorMessage); @@ -3191,7 +3191,7 @@ private static void GetHostComputeInteropTypes(out Type computeSystemPropertiesT computeSystemPropertiesType = schemaAssembly.GetType("Microsoft.HyperV.Schema.Compute.System.Properties"); if (computeSystemPropertiesType == null) { - throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotGetHostInteropTypes)); + throw new PSInvalidOperationException(RemotingErrorIdStrings.CannotGetHostInteropTypes); } } @@ -3199,7 +3199,7 @@ private static void GetHostComputeInteropTypes(out Type computeSystemPropertiesT hostComputeInteropType = hostComputeInteropAssembly.GetType("Microsoft.HostCompute.Interop.HostComputeInterop"); if (hostComputeInteropType == null) { - throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotGetHostInteropTypes)); + throw new PSInvalidOperationException(RemotingErrorIdStrings.CannotGetHostInteropTypes); } } @@ -3377,7 +3377,7 @@ private void GetContainerPropertiesInternal() var propertyInfo = computeSystemPropertiesType.GetProperty("RuntimeId"); if (propertyInfo == null) { - throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.CannotGetHostInteropTypes)); + throw new PSInvalidOperationException(RemotingErrorIdStrings.CannotGetHostInteropTypes); } RuntimeId = (Guid)propertyInfo.GetValue(computeSystemPropertiesHandle);