-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix PSUserAgent Generation Exception on Windows 7 #5256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| using System.Management.Automation; | ||
| using System.Runtime.InteropServices; | ||
| using System.Globalization; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Microsoft.PowerShell.Commands | ||
| { | ||
|
|
@@ -131,7 +132,8 @@ internal static string PlatformName | |
| // only generate the windows user agent once | ||
| if(s_windowsUserAgent == null){ | ||
| // find the version in the windows operating system description | ||
| string versionText = OS.Substring(OS.LastIndexOf(" ") +1); | ||
| Regex pattern = new Regex(@"\d+(\.\d+)+"); | ||
| string versionText = pattern.Match(OS).Value; | ||
| Version windowsPlatformversion = new Version(versionText); | ||
| s_windowsUserAgent = $"Windows NT {windowsPlatformversion.Major}.{windowsPlatformversion.Minor}"; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we parse? Maybe just return OS (OSDescription) ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the format expected in User-Agents. the OS portion should be
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please clarify for me what is "the format expected in User-Agents"? My understanding is that the string is sent to a web server as UserAgent string and falls in a web log file so if I see the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User-Agents have an expected format. The first part, Platform, has a specific format to detect Windows/Linux/macOS. the full windows 10: windows 7 The change in this PR is for the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @markekraus I think we must merge and fix the bug and I'll open new Issue to discuss User-Agents. |
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we trust OSDescription? Maybe add Assert at least?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we can for all the versions of Windows this will run on. What assert did you have in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Diagnostics.Assert(versionText != null, "...")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm it would probably be better to wrap it in a try/catch because if the system was detected as Windows, then returning
Windows NTwithout the version is better than nothing at all.