PowerShell / PowerShell/PSScriptAnalyzer

Rule Idea: Warn on `[xml](Get-Content <File>)` pattern

Open
#1,667 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area - Rules Issue - Enhancement Up-for-Grabs
Dominant language
C#
Stars
2.2k
Forks
414
Avg merge
13h 1m
Merged PRs (30d)
2

Description

Summary of the new feature

Warn on Pattern: [xml](Get-Content <File>)
Suggest Pattern: $a = [xml]::new(); $a.Load(<File>)

This pattern is currently in wide use, but it has some problems and should be discouraged.

  1. This pattern, due to using Get-Content, has problems if the (perfectly valid) xml file being parsed is not utf8 encoded. See: https://github.com/PowerShell/PowerShell/issues/14505 & https://stackoverflow.com/questions/65263942/how-to-load-or-read-an-xml-file-using-convertto-xml-and-select-xml/65264118#65264118

  2. This pattern is very inefficient. I know that PSSA does not generally worry about this - but since the encoding issue above exists, feels worth it to mention. Testing against a ~1mb file shows [xml](Get-Content <File>) as ~12x slower, and against a 170mb file as 22x slower (loading took 46 seconds!).

What is the latest version of PSScriptAnalyzer at the point of writing
PSSA 1.19.1

Powershell Versions
This currently affects both Windows Powershell 5.1 and Powershell 7.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue targets PSScriptAnalyzer and the PowerShell pattern [xml](Get-Content <File>). Begin by finding the analyzer's existing rule implementations and tests for pattern-based diagnostics; done means the pattern is warned on and the suggested XML-loading form is reported consistently on Windows PowerShell 5.1 and PowerShell 7.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.