我正在尝试将各种系统指标从PowerShell脚本提取到Python中以进行进一步处理。我已经选择了我需要的数据并使用echo将其放入我试图用Python阅读的txt文件中,但是当我查看Python数据时,我只是得到了胡言乱语。当Powershell输出到txt文件时,它会使用一些奇怪的编码吗?如果是这样,有没有办法让PowerShell以Python可以读取的格式输出或指示Python读取PowerShell使用的格式?
PowerShell代码如下所示:
$Make = Get-CimInstance CIM_ComputerSystem | Select Manufacturer
echo $Make.manufacturer > c:\Mike\Output\SysConfig.txt
$Model = Get-CimInstance CIM_ComputerSystem | Select Model
echo $Model.model >> c:\Mike\Output\SysConfig.txt
$Os = Get-CimInstance Win32_OperatingSystem | Select-Object Caption
echo $Os.caption >> c:\Mike\Output\SysConfig.txt
$CPU = Get-WmiObject Win32_Processor | Select *
echo $CPU.Name >> c:\Mike\Output\SysConfig.txt
echo $CPU.NumberOfCores >> c:\Mike\Output\SysConfig.txt
echo $CPU.NumberOfLogicalProcessors >> c:\Mike\Output\SysConfig.txt
echo $CPU.ThreadCount >> c:\Mike\Output\SysConfig.txt
echo $CPU.MaxClockSpeed >> c:\Mike\Output\SysConfig.txt
echo $CPU.CurrentClockSpeed >> c:\Mike\Output\SysConfig.txt
文本输出如下所示:
Hewlett-Packard
HP EliteBook 2570p
Microsoft Windows 10 Pro
Intel(R) Core(TM) i7-3520M CPU @ 2.90GHz
2
4
4
2901
2901
Python代码如下所示:
with open('c:\mike\output\SysConfig.txt', mode='r') as f:
Manufacturer = f.readlines(1)
Make = f.readlines(2)
OS = f.readlines(3)
CPU_Name = f.readlines(4)
CPU_Phys_Cores = f.readlines(5)
CPU_Log_Cores = f.readlines(6)
CPU_Threads = f.readlines(7)
CPU_Max_Clock = f.readlines(8)
CPU_Curr_Clock = f.readlines(9)
print(Manufacturer)
print(Make)
print(OS)
print(CPU_Name)
print(CPU_Phys_Cores)
print(CPU_Log_Cores)
print(CPU_Threads)
print(CPU_Max_Clock)
print(CPU_Curr_Clock)
答案 0 :(得分:2)
您的问题是重定向运算符的编码是unicode(UTF16-LE)。
$CS = Get-CimInstance -ClassName CIM_ComputerSystem
$OS = (Get-CimInstance -ClassName Win32_OperatingSystem).Caption
$CPU = Get-CimInstance -ClassName Win32_Processor
@"
$($CS.Manufacturer)
$($CS.Model)
$OS
$($CPU.Name)
$($CPU.NumberOfCores)
$($CPU.NumberOfLogicalProcessors)
$($CPU.ThreadCount)
$($CPU.MaxClockSpeed)
$($CPU.CurrentClockSpeed)
"@ | Out-File -FilePath C:\Mike\Output\SysConfig.txt -Encoding UTF8
清理:
$CS = Get-CimInstance -ClassName CIM_ComputerSystem
$OS = (Get-CimInstance -ClassName Win32_OperatingSystem).Caption
$CPU = Get-CimInstance -ClassName Win32_Processor
"{0}`n{1}`n{2}`n{3}`n{4}`n{5}`n{6}`n{7}`n{8}" -f @( $CS.Manufacturer
$CS.Model
$OS
$CPU.Name
$CPU.NumberOfCores
$CPU.NumberOfLogicalProcessors
$CPU.ThreadCount
$CPU.MaxClockSpeed
$CPU.CurrentClockSpeed
) |
Out-File -FilePath C:\Mike\Output\SysConfig.txt -Encoding UTF8
答案 1 :(得分:1)
阅读Python中的内容没问题。
定义一些标签,用于呈现输出。将整个输入文件a作为字节数组读取,适当地解码字节以形成字符串,并丢弃行结束(如果存在)。在线端分割字符串,并使用适当的标签打印每个结果字符串。
vars = ["Manufacturer", "Make", "OS", "CPU_Name", "CPU_Phys_Cores", "CPU_Log_Cores", "CPU_Threads", "CPU_Max_Clock", "CPU_Curr_Clock"]
with open('SysConfig.txt', 'rb') as f:
content = f.read().decode(encoding='UTF16').rstrip()
for var, line in zip(vars, content.split('\n')):
print (var, line.strip())
输出:
Manufacturer Hewlett-Packard
Make HP Compaq Elite 8300 SFF
OS Microsoft Windows 10 Pro
CPU_Name Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz
CPU_Phys_Cores 4
CPU_Log_Cores 4
CPU_Threads 4
CPU_Max_Clock 3201
CPU_Curr_Clock 2700