计算自上次使用PowerShell安装程序以来的天数

如何找到所有最近安装/修复/修改的软件?

我一直在尝试从注册表中获取列表,然后对其进行过滤以仅查找相关程序,然后尝试计算在特定阈值之前安装了哪些程序。

这就是我一直在做的:

$Installed_Software=Get-ItemProperty HKLM:\Software\Wow6432Node\microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName,DisplayVersion,Publisher,InstallDate
$SoftwareInstall_Days = 40
$TheDate = (([datetime]::Now))

$Installed_recently= @()
$Installed_recently=($Installed_Software |
Where-Object {($_.DisplayName -match ('Something') -or $_.Publisher -match ('SomeOtherThing'))} |
where (($TheDate - $_.InstallDate) -le $SoftwareInstall_Days)

if ($Installed_recently) {
    Write-Output "Relevant software was recently installed and/or repaired / modified.`nThese are the items:`n$Installed_recently"
}
else {
    Write-Output "No relevant software was recently installed and/or repaired / modified."
}

当然,以下情况只是我无法执行的操作的说明:

where (($TheDate - $_.InstallDate) -le $SoftwareInstall_Days)

这怎么办?

我这里的方向是否正确?

poilkjm 回答:计算自上次使用PowerShell安装程序以来的天数

为了能够比较日期,您需要解析从注册表获得的InstallDate字符串,然后从当前日期中减去它。

此字符串可以有几种格式。在我的英语Win10 x86计算机上,我看到2种日期格式:yyyyMMddMM/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

大家都在问