Skip to content

Commit 1884d13

Browse files
Use runspace to acquire version to avoid CLM issues (#1869)
1 parent d209bd2 commit 1884d13

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/code/InternalHooks.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,10 @@ public static void SetTestHook(string property, object value)
2222
var fieldInfo = typeof(InternalHooks).GetField(property, BindingFlags.Static | BindingFlags.NonPublic);
2323
fieldInfo?.SetValue(null, value);
2424
}
25+
26+
public static string GetUserString()
27+
{
28+
return Microsoft.PowerShell.PSResourceGet.Cmdlets.UserAgentInfo.UserAgentString();
29+
}
2530
}
26-
}
31+
}

src/code/ServerFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ internal static class UserAgentInfo
1212
{
1313
static UserAgentInfo()
1414
{
15-
using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace))
16-
{
17-
_psVersion = ps.AddScript("$PSVersionTable").Invoke<Hashtable>()[0]["PSVersion"].ToString();
18-
}
19-
15+
_psVersion = System.Management.Automation.Runspaces.Runspace.DefaultRunspace.Version.ToString();
2016
_psResourceGetVersion = typeof(UserAgentInfo).Assembly.GetName().Version.ToString();
2117
_distributionChannel = System.Environment.GetEnvironmentVariable("POWERSHELL_DISTRIBUTION_CHANNEL") ?? "unknown";
2218
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
# In future, we will add more tests to be executed in Constrained Language Mode.
5+
Describe "Test UserAgentInfo" {
6+
It "GetUserString returns a non-null, non-empty string" {
7+
$userAgentString = [Microsoft.PowerShell.PSResourceGet.InternalHooks]::GetUserString()
8+
$userAgentString | Should -Not -BeNullOrEmpty
9+
}
10+
}

0 commit comments

Comments
 (0)