Skip to content
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

Fix exception when writing messages #2438

Merged
merged 2 commits into from
Oct 10, 2022

Conversation

jackpoz
Copy link
Contributor

@jackpoz jackpoz commented Oct 9, 2022

Type

  • Bug Fix
  • New Feature
  • Sample

Related Issues?

Fixes #2185

What is in this Pull Request ?

Fix a "Index out of range" exception when writing messages to ConsoleHost with a Window width less than 8.
This happens in Azure Automation Runbooks as cmdlet.Host.UI.RawUI.MaxWindowSize.Width is set to 0, but happens also locally when setting a Width of 7.

#Investigation
I added some logs to WordWrap to see how the "Index out of range" exception was thrown and it was caused by passing maxLineLength parameter with value -8 , calling text.LastIndexOfAny(... , startIndex: -8) which is invalid
image

This means cmdlet.Host.UI.RawUI.MaxWindowSize.Width is set to 0 in Azure Automation, which makes sense as there is no UI.
I was able to trigger the same exception locally setting the Window Size of PowerShell 7.1 to 7 pixels:
image

image

The error is more readable when expanding the window after having triggered the exception:
image

#Tests
After excluding the logic of special formatting in case of a too small window, this is how it looks:

Width 120:
image

Width 9:
image

Width 7:
image
After expanding the window:
image

Testing it in Azure Automation connections correctly, see the output of Get-PnPDiagnostics with no exception and a connection to a site:
image

Fix a "Index out of range" exception when writing messages to ConsoleHost with a Window width less than 8.
@gautamdsheth gautamdsheth merged commit 5456a36 into pnp:dev Oct 10, 2022
@jackpoz jackpoz deleted the fix/AzureAutomationException branch December 4, 2022 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Connect-PnPOnline is not working in azure automation runbook with powershell 7.0
2 participants