Hello,
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"
$wrs_path="C:\WindowsRepairSource"
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
Clear-WindowsCorruptMountPoint
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
Healthy
Healthy
Note:
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.
"Healthy
Repairable
"
If I create the text file first:
New-Item "$wrs_path\ImageHealthState.txt"
The Check/Scan output issue persists.
Workaround:
For now, I’ve reordered my script:
-
CheckHealth
this creates the text file ScanHealth
-
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.