diff --git a/src/code/InternalHooks.cs b/src/code/InternalHooks.cs index 0578485ca..99d6104ce 100644 --- a/src/code/InternalHooks.cs +++ b/src/code/InternalHooks.cs @@ -22,5 +22,10 @@ public static void SetTestHook(string property, object value) var fieldInfo = typeof(InternalHooks).GetField(property, BindingFlags.Static | BindingFlags.NonPublic); fieldInfo?.SetValue(null, value); } + + public static string GetUserString() + { + return Microsoft.PowerShell.PSResourceGet.Cmdlets.UserAgentInfo.UserAgentString(); + } } -} \ No newline at end of file +} diff --git a/src/code/ServerFactory.cs b/src/code/ServerFactory.cs index 10c43b1a3..7be9d1ca1 100644 --- a/src/code/ServerFactory.cs +++ b/src/code/ServerFactory.cs @@ -12,11 +12,7 @@ internal static class UserAgentInfo { static UserAgentInfo() { - using (System.Management.Automation.PowerShell ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace)) - { - _psVersion = ps.AddScript("$PSVersionTable").Invoke()[0]["PSVersion"].ToString(); - } - + _psVersion = System.Management.Automation.Runspaces.Runspace.DefaultRunspace.Version.ToString(); _psResourceGetVersion = typeof(UserAgentInfo).Assembly.GetName().Version.ToString(); _distributionChannel = System.Environment.GetEnvironmentVariable("POWERSHELL_DISTRIBUTION_CHANNEL") ?? "unknown"; } diff --git a/test/ConstrainedLanguageMode.Tests.ps1 b/test/ConstrainedLanguageMode.Tests.ps1 new file mode 100644 index 000000000..17ecb1bd4 --- /dev/null +++ b/test/ConstrainedLanguageMode.Tests.ps1 @@ -0,0 +1,10 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +# In future, we will add more tests to be executed in Constrained Language Mode. +Describe "Test UserAgentInfo" { + It "GetUserString returns a non-null, non-empty string" { + $userAgentString = [Microsoft.PowerShell.PSResourceGet.InternalHooks]::GetUserString() + $userAgentString | Should -Not -BeNullOrEmpty + } +}