我必须为我的学校写一个代码,在安装软件后比较2个.txt文件。它应该不会在第一个.txt上显示我安装的x软件,但在第二个应该会显示。我写了这篇文章,但我无法对它们进行比较,因为在两个.txt文件中都缺少x软件。我怎么才能让它工作呢?
$array1 = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* |
sort -Property installdate -Descending |
Format-Table DisplayName, @{Name="InstallDate"; Expression={([DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)).ToShortDateString()}}
$array2 = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* |
sort -Property installdate -Descending |
Format-Table DisplayName, @{Name="InstallDate"; Expression={([DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd', $null)).ToShortDateString()}}
$array1 | Export-Clixml C:\Users\Qendrim\Desktop\lb02_test.txt
$counter = 0
Start-Sleep -Seconds 10
$array2 | Export-Clixml C:\Users\Qendrim\Desktop\lb02_test_dif.txt
#Compare-object -ReferenceObject (Import-Clixml C:\Users\Qendrim\Desktop\lb02_test.txt) -DifferenceObject ($array2)
Compare-Object -ReferenceObject $(Get-Content C:\Users\Qendrim\Desktop\lb02_test.txt) -DifferenceObject $(Get-Content C:\Users\Qendrim\Desktop\lb02_test_dif.txt)
发布于 2018-01-29 02:30:29
对于那些在x32版本上是x32的应用程序,或者在windows的x64版本上是windows或x64的应用程序,您的脚本可以很好地工作。
如果该应用程序是在windows的x64版本上运行的x32,则该应用程序将列在下面的键中。
基于HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\的x64操作系统上的x32程序
尝试与新密钥进行比较,看看您的应用程序是否在那里。
我还会使用" out -file“而不是"export-Clixml”将输出格式化为文本,因为它更容易与所有额外的格式进行比较。文本文档比相同的XML格式化文件小21倍。
$array1 | export-Clixml C:\Users\Qendrim\Desktop\lb02_test.txt
$array1 | out-file C:\Users\Qendrim\Desktop\lb02_test.txt
发布于 2018-01-29 12:01:45
我认为你遇到的问题其实很简单。在执行以下命令时:
$array2 = Get-ItemProperty
HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* |
sort -Property installdate -Descending |
Format-Table DisplayName, @{Name="InstallDate"; Expression=
{([DateTime]::ParseExact($_.InstallDate, 'yyyyMMdd',
$null)).ToShortDateString()}}
它会当场将数据保存到该变量。当您在休眠时间段之后导出到文件时,您将导出之前保存的数据,而不是在休眠时间段之后的数据。
修复很简单,假设您在休眠期间安装/卸载软件。你所需要做的就是将这些行向下移动到“开始-睡眠10”下面。
再加上DonBennettJr建议使用"Out-File“而不是"Export-Clixml”,应该会让你走上正确的道路。
https://stackoverflow.com/questions/48489508
复制相似问题