GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix PSUserAgent Generation Exception on Windows 7 by markekraus · Pull Request #5256 · PowerShell/PowerShell

Conversation

@markekraus

@markekraus Mark Kraus (markekraus) commented Oct 27, 2017

Copy link
Copy Markdown
Contributor

fixes #5242

On Windows 7 RuntimeInformation.OSDescription produces Microsoft Windows 6.1.7601 S. This resulted in S attempting to be evaluated as a Version and a System.ArgumentException: Version string portion was too short or too long. exception.

This PR switches to a regex capture of the version from the OSDescription String.

I have verfied this is working on windows 10 and Windows 7

Name                           Value
----                           -----
PSVersion                      6.0.0-beta.9
PSEdition                      Core
GitCommitId                    v6.0.0-beta.9-12-gb78af9477186032e675b990df629dbc2f15d0a13
OS                             Microsoft Windows 6.1.7601 S
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Name                           Value
----                           -----
PSVersion                      6.0.0-beta.9
PSEdition                      Core
GitCommitId                    v6.0.0-beta.9-12-gb78af9477186032e675b990df629dbc2f15d0a13
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@markekraus
Use Regex to Capture Windows Version
b78af94
@markekraus
Mark Kraus (markekraus) requested review from James Truher (JamesWTruher), Paul Higinbotham (PaulHigin) and Dan Travison (dantraMSFT) as code owners October 27, 2017 13:41
@markekraus
[Feature] Run feature tests
b642bf1
PaulHigin
Paul Higinbotham (PaulHigin) approved these changes Oct 27, 2017
View reviewed changes
iSazonov
Ilya (iSazonov) reviewed Oct 27, 2017
View reviewed changes
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/PSUserAgent.cs
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}";

@iSazonov Ilya (iSazonov) Oct 27, 2017

Copy link
Copy Markdown
Collaborator

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.

Why do we parse? Maybe just return OS (OSDescription) ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markekraus Mark Kraus (markekraus) Oct 27, 2017

Copy link
Copy Markdown
Contributor Author

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.

Because of the format expected in User-Agents. the OS portion should be Windows NT <Major Version>.<Minor Version> That's not native anywhere in the CoreFX name space, System.Environment.OSVersion.Version reports 6.2 for major/minor on windows 10, but User-Agent expects 10.0. ugh.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iSazonov Ilya (iSazonov) Oct 27, 2017

Copy link
Copy Markdown
Collaborator

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.

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 OSDescription in the log it is useful.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markekraus Mark Kraus (markekraus) Oct 27, 2017

Copy link
Copy Markdown
Contributor Author

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.

User-Agents have an expected format. The first part, Platform, has a specific format to detect Windows/Linux/macOS. the full OSDescription follows the Platform field. here is the full user-agent strings with this change:

windows 10:

Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.15063; en-US) PowerShell/6.0.0

windows 7

Mozilla/5.0 (Windows NT 6.1; Microsoft Windows 6.1.7601 S; en-US) PowerShell/6.0.0

The change in this PR is for the Windows NT 10.0 and Windows NT 6.1 Platform fields.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iSazonov Ilya (iSazonov) Oct 28, 2017

Copy link
Copy Markdown
Collaborator

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.

Mark Kraus (@markekraus) I think we must merge and fix the bug and I'll open new Issue to discuss User-Agents.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iSazonov Ilya (iSazonov) self-assigned this Oct 30, 2017
iSazonov
Ilya (iSazonov) reviewed Oct 30, 2017
View reviewed changes
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/PSUserAgent.cs
string versionText = OS.Substring(OS.LastIndexOf(" ") +1);
Regex pattern = new Regex(@"\d+(\.\d+)+");
string versionText = pattern.Match(OS).Value;
Version windowsPlatformversion = new Version(versionText);

@iSazonov Ilya (iSazonov) Oct 30, 2017

Copy link
Copy Markdown
Collaborator

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markekraus Mark Kraus (markekraus) Oct 30, 2017

Copy link
Copy Markdown
Contributor Author

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?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@iSazonov Ilya (iSazonov) Oct 30, 2017

Copy link
Copy Markdown
Collaborator

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, "...")

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@markekraus Mark Kraus (markekraus) Oct 30, 2017

Copy link
Copy Markdown
Contributor Author

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 NT without the version is better than nothing at all.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iSazonov Ilya (iSazonov) changed the title Fix PSUserAgent Generation Execption on Windows 7 Fix PSUserAgent Generation Exception on Windows 7 Nov 1, 2017
@iSazonov
Ilya (iSazonov) merged commit dc01301 into PowerShell:master Nov 1, 2017
@markekraus
Mark Kraus (markekraus) deleted the FixUserAgent branch January 19, 2018 18:58
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@markekraus @iSazonov
Fix PSUserAgent Generation Exception on Windows 7 (PowerShell#5256)
ea72607
* Use Regex to Capture Windows Version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iSazonov Ilya (iSazonov) iSazonov left review comments
@dantraMSFT Dan Travison (dantraMSFT) Awaiting requested review from dantraMSFT
@JamesWTruher James Truher (JamesWTruher) Awaiting requested review from JamesWTruher
+1 more reviewer
@PaulHigin Paul Higinbotham (PaulHigin) PaulHigin approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@iSazonov Ilya (iSazonov)

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Invoke-RestMethod started to fail in v6-beta.9 on a Windows 7 machine

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@markekraus @PaulHigin @iSazonov
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.