ImageHealthState OUTPUT Issues


I’m learning how to use PowerShell to help automate some of my manual tasks related to managing Windows Image (.wim) files for my Deployment Share.

I’m trying to capture the output of the results when I run the Check/Scan/RestoreHealth commands.

# Scan/CheckHealth

$src_wim="D:\OSDBuilder\OSBuilds\Windows 10 Enterprise LTSC 2021 x64 21H2 19044.1566\OS\sources\install.wim"
$src_name="Windows 10 Enterprise LTSC 2021"

Mount-WindowsImage -ImagePath "$src_wim" -Name "$src_name" -Path "C:\Mount" -ReadOnly # Mount WIM File as Read Only

Add-Content -Path "$wrs_path\ImageHealthState.txt" -Value $src_name # Adds Source Name to file

$ImageHealthState = Repair-WindowsImage -Path "C:\Mount" -CheckHealth | Select-Object -Property ImageHealthState # Checks ImageHealthState of wim file
$ImageHealthState.ImageHealthState | Out-File -FilePath "$wrs_path\ImageHealthState.txt" -Append # Appends results of ImageHealthState to text file

$ImageHealthState = Repair-WindowsImage -Path "C:\Mount" -ScanHealth | Select-Object -Property ImageHealthState # Scans Image for corruption
$ImageHealthState.ImageHealthState | Out-File -FilePath "$wrs_path\ImageHealthState.txt" -Append # Appends results of ImageHealthState to text file

Dismount-WindowsImage -Path "C:\Mount" -Discard # Unmount Image

Here’s the basic CheckHealth command:
Repair-WindowsImage -Path "C:\Mount" -CheckHealth

And here’s the console output:

Path : C:\Mount
Online : False
ImageHealthState : Healthy
RestartNeeded : False

What I’m trying to achieve in my Text file:

Windows 10 Enterprise LTSC 2021

When I first run the script, the Add-Content line will create the text file and append the OS “src_name”.

Output issues:
When the CheckHealth and ScanHealth results are appended to the text file, they are added with spaces after each letter, and two new lines with a single space each, like so. Healthy is the first output and Repairable is the second output. Quotes added to discern the two merged outputs.

"H e a l t h y

""R e p a i r a b l e


Interestingly enough, if I let the CheckHealth or ScanHealth commands create the text file, the output is appended to the file correctly, with no spacing after each letter or additional lines.


If I create the text file first:
New-Item "$wrs_path\ImageHealthState.txt"
The Check/Scan output issue persists.

For now, I’ve reordered my script:

  1. CheckHealth this creates the text file
  2. ScanHealth
  3. Add-Content appends the OS src_name

This gives me clean output, but it’s perplexing to say the least that when the file is created using New-Item, or with my first Add-Content command, that the Check/Scan outputs are all messed up.

EDIT: Used correct coding using backticks.

The issue is with encoding. Try using set-content instead and enjoy.

Hi @krzydoug,

The issue was definitely with encoding. I just assumed that PowerShell always outputted plain text to a text file using the same encoding. That may even be true considering Out-File, Add-Content, and Set-Content all show the default encoding as utf8NoBom. However, this doesn’t seem to be a valid option for me to choose. Only utf7,8 & 32 were showing up as available options for me.

I decided to add the -encoding parameter to my existing code. Using utf8 for both the Add-Content and Out-File -append worked out perfectly.

Add-Content -Path "$wrs_local\ImageHealthState_$src_name.txt" -Value "$src_name" -Encoding utf8

$ImageHealthState = Repair-WindowsImage -Path "$mnt_img" -CheckHealth | Select-Object -Property ImageHealthState
$ImageHealthState.ImageHealthState | Out-File -FilePath "$wrs_local\ImageHealthState_$src_name.txt" -Append -Encoding utf8

I played with the Set-Content as well, and I may just use that for my single wim file checks. I can’t use it for my loop that checks 5-10 wim files, else it will keep overwriting that text file.

Thanks to your help, I can now play around with taking that ImageHealthState property value and use it to initiate a Restorehealth when it returns Unhealthy.

That’ll take me some time to work through but will definitely return here if needed.

Thanks again for your help!

1 Like