I have a code to use powershell to export domain controller's policy using Get-GPOReport. However, I can never use findstr on this exported HTML file. The only way it works is if I change the extension of the HTML file to .txt, then copy all the content in it to another newly created .txt file (e.g. test.txt).
Only then, the findstr function works. Does anyone know why it doesn't work on the original file?
import os, subprocess
subprocess.Popen(["powershell","Get-GPOReport -Name 'Default Domain Controllers Policy' -ReportType HTML -Path 'D:\Downloads\Project\GPOReport.html'"],stdout=subprocess.PIPE)
policyCheck = subprocess.check_output([power_shell,"-Command", 'findstr /c:"Minimum password age"', "D:\Downloads\Project\GPOReport.html"]).decode('utf-8')
print(policyCheck)
# However if I copy all the content in D:\Downloads\Project\GPOReport.html to a newly created test.txt file (MANUALLY - I've tried to do it programmatically, findstr wouldn't work too) under the same directory and use:
power_shell = os.path.join(os.environ["SYSTEMROOT"], "System32","WindowsPowerShell", "v1.0", "powershell.exe")
policyCheck = subprocess.check_output([power_shell,"-Command", 'findstr /c:"Minimum password age"', "D:\Downloads\Project\test.txt"]).decode('utf-8')
print(policyCheck)
# Correct Output Will Show
What I got:
subprocess.CalledProcessError: Command '['C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', '-Command', 'findstr /c:"Minimum password age"', 'D:\Downloads\Project\GPOReport.html']' returned non-zero exit status 1.
Expected Output:
<tr><td>Minimum password age</td><td>1 days</td></tr>
I'm not a Python guy, but I think this may be an encoding issue. Based on the fact that findstr is not Unicode compatible. As @iRon suggested Select-String
should do the trick though you may have to reference the .Line
property to get the expected output you mentioned. Other wise it will return match objects.
I'll leave it to you to transpose this into the Python code, but Select-String
command should look something like:
(Select-String -Path "D:\Downloads\Project\GPOReport.html" -Pattern "Minimum password age" -SimpleMatch).Line
If there are multiple matches this will return an array of strings; the lines where the matches were made. Let me know if that's helpful.