Skip to content

Commit d8b755c

Browse files
SteveL-MSFTdaxian-dbw
authored andcommitted
Fix Enter-PSSession -ContainerId for the latest Windows (PowerShell#7883)
Hyper-V team changed the type used to get the `runtimeid`. We need to check for new type and fall back to old type.
1 parent 8be8b99 commit d8b755c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/System.Management.Automation/engine/remoting/common/RunspaceConnectionInfo.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3122,7 +3122,7 @@ public void CreateContainerProcess()
31223122
ContainerId));
31233123

31243124
case ContainersFeatureNotEnabled:
3125-
throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.ContainersFeatureNotEnabled));
3125+
throw new PSInvalidOperationException(RemotingErrorIdStrings.ContainersFeatureNotEnabled);
31263126

31273127
// other errors caught with exception
31283128
case OtherError:
@@ -3163,7 +3163,7 @@ public void GetContainerProperties()
31633163
break;
31643164

31653165
case ContainersFeatureNotEnabled:
3166-
throw new PSInvalidOperationException(StringUtil.Format(RemotingErrorIdStrings.ContainersFeatureNotEnabled));
3166+
throw new PSInvalidOperationException(RemotingErrorIdStrings.ContainersFeatureNotEnabled);
31673167

31683168
case OtherError:
31693169
throw new PSInvalidOperationException(ErrorMessage);
@@ -3177,15 +3177,30 @@ public void GetContainerProperties()
31773177
/// <summary>
31783178
/// Dynamically load the Host Compute interop assemblies and return useful types.
31793179
/// </summary>
3180-
/// <param name="computeSystemPropertiesType">The Microsoft.HyperV.Schema.Compute.System.Properties type.</param>
3180+
/// <param name="computeSystemPropertiesType">The HCS.Compute.System.Properties type.</param>
31813181
/// <param name="hostComputeInteropType">The Microsoft.HostCompute.Interop.HostComputeInterop type.</param>
31823182
private static void GetHostComputeInteropTypes(out Type computeSystemPropertiesType, out Type hostComputeInteropType)
31833183
{
31843184
Assembly schemaAssembly = Assembly.Load(new AssemblyName("Microsoft.HyperV.Schema, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"));
3185-
computeSystemPropertiesType = schemaAssembly.GetType("Microsoft.HyperV.Schema.Compute.System.Properties");
3185+
3186+
// The type name was changed in newer version of Windows so we check for new one first,
3187+
// then fallback to previous type name to support older versions of Windows
3188+
computeSystemPropertiesType = schemaAssembly.GetType("HCS.Compute.System.Properties");
3189+
if (computeSystemPropertiesType == null)
3190+
{
3191+
computeSystemPropertiesType = schemaAssembly.GetType("Microsoft.HyperV.Schema.Compute.System.Properties");
3192+
if (computeSystemPropertiesType == null)
3193+
{
3194+
throw new PSInvalidOperationException(RemotingErrorIdStrings.CannotGetHostInteropTypes);
3195+
}
3196+
}
31863197

31873198
Assembly hostComputeInteropAssembly = Assembly.Load(new AssemblyName("Microsoft.HostCompute.Interop, Version=10.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"));
31883199
hostComputeInteropType = hostComputeInteropAssembly.GetType("Microsoft.HostCompute.Interop.HostComputeInterop");
3200+
if (hostComputeInteropType == null)
3201+
{
3202+
throw new PSInvalidOperationException(RemotingErrorIdStrings.CannotGetHostInteropTypes);
3203+
}
31893204
}
31903205

31913206
/// <summary>
@@ -3350,7 +3365,23 @@ private void GetContainerPropertiesInternal()
33503365

33513366
var computeSystemPropertiesHandle = getComputeSystemPropertiesInfo.Invoke(null, new object[] { ComputeSystem });
33523367

3353-
RuntimeId = (Guid)computeSystemPropertiesType.GetProperty("RuntimeId").GetValue(computeSystemPropertiesHandle);
3368+
// Since Hyper-V changed this from a property to a field, we can optimize for newest Windows to see if it's a field,
3369+
// otherwise we fall back to old code to be compatible with older versions of Windows
3370+
var fieldInfo = computeSystemPropertiesType.GetField("RuntimeId");
3371+
if (fieldInfo != null)
3372+
{
3373+
RuntimeId = (Guid)fieldInfo.GetValue(computeSystemPropertiesHandle);
3374+
}
3375+
else
3376+
{
3377+
var propertyInfo = computeSystemPropertiesType.GetProperty("RuntimeId");
3378+
if (propertyInfo == null)
3379+
{
3380+
throw new PSInvalidOperationException(RemotingErrorIdStrings.CannotGetHostInteropTypes);
3381+
}
3382+
3383+
RuntimeId = (Guid)propertyInfo.GetValue(computeSystemPropertiesHandle);
3384+
}
33543385

33553386
//
33563387
// Get container object root for Windows Server container.

src/System.Management.Automation/resources/RemotingErrorIdStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,4 +1667,7 @@ All WinRM sessions connected to PowerShell session configurations, such as Micro
16671667
<data name="ProcessInfoNotRecoverable" xml:space="preserve">
16681668
<value>Information about the process could not be read: '{0}'.</value>
16691669
</data>
1670+
<data name="CannotGetHostInteropTypes" xml:space="preserve">
1671+
<value>Host system does not have the correct version of Hyper-V schema.</value>
1672+
</data>
16701673
</root>

0 commit comments

Comments
 (0)