为了能够比较日期,您需要解析从注册表获得的InstallDate
字符串,然后从当前日期中减去它。
此字符串可以有几种格式。在我的英语Win10 x86计算机上,我看到2种日期格式:yyyyMMdd
和MM/dd/yyyy
。
在下面的代码中,我创建了一个数组,您可以添加其他格式来解析。
这是修改后的代码-我使用“ Microsoft”字符串进行测试:
# Add additional property InstallDateObj that will hold the parsed DateTime object
$Installed_Software=Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,Publisher,InstallDate,InstallDateObj
$SoftwareInstall_Days = 40
$TheDate = (([datetime]::Now))
# Try to parse dates.
$Installed_Software.ForEach({
# add more formats if you need
[string[]] $formats = @("yyyyMMdd","MM/dd/yyyy")
$installDate = $_.InstallDate
$installedDateObj = $null;
$formats.ForEach({ [DateTime] $dt = New-Object DateTime; if([datetime]::TryParseExact($installDate,$_,[System.Globalization.CultureInfo]::InvariantCulture,[System.Globalization.DateTimeStyles]::None,[ref]$dt)) { $installedDateObj = $dt} });
$_.InstallDateObj = $installedDateObj
})
$Installed_recently= @()
$Installed_recently=($Installed_Software |
Where-Object {($_.DisplayName -match ('Microsoft') -or $_.Publisher -match ('Microsoft') -and ($_.InstallDateObj -ne $null) -and ($TheDate - $_.InstallDateObj).Days -le $SoftwareInstall_Days)})
if($Installed_recently.Count -gt 0) {
Write-Output "Relevant software was recently installed and/or repaired / modified.`nThese are the items:`n"
Write-Output $Installed_recently
}
else {
Write-Output "No relevant software was recently installed and/or repaired / modified."
}
,
日期时间对象有一些方法可以从日期时间添加或删除分钟,小时,天,年,请参见下面的脚本。
对于当前从注册表获取软件的示例,需要将InstallDate转换为日期时间格式,然后将其与报表中的所需日期进行比较。
#Report date 40 days
$SoftwareInstall_Days = (([datetime]::Now)).AddDays(-40)
#Getting installed software and filtering based on report date
$Installed_recently=Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,@{Name = "InstalledDate" ; Expression = {[DateTime]::ParseExact($_.InstallDate,'yyyyMMdd',[Globalization.CultureInfo]::InvariantCulture) } }| where {$_.installeddate -ge $SoftwareInstall_Days}
#Output
$Installed_recently
还可以从不同地方收集已安装的软件:
#Using WMI
Get-WmiObject -Class Win32_Product
#Using PS Software provider
Get-Package
本文链接:https://www.f2er.com/3168476.html