PowerShell | Conformité de Office Update vs Windows Update
Vous trouverez dans cet article, les paramètres utilisés pour tester la conformité des mises à jour sur les postes de travail.
Cela me permet de vérifier la présence des patchs Windows Update à partir d'une date d'installation.
Le poste est conforme suite à 2 critères :
- Je considère ici que 3 patchs Windows Update (KBxxxxx), doivent être installés sur la période des 30 derniers jours.
- Je veux que 3 mises à jour de Software Update (ayant la référence "Microsoft") doivent être installés sur la période des 30 derniers jours. Pour les mises à jour de Microsoft Office, Microsoft Excel, etc. par exemple.
DetectSoftwareUpdate.ps1 :
$HTT = -30
$CountDateSoftUpdate = (Get-WmiObject -Namespace ROOT\CIMV2 -Class Win32_Product| Where-Object {$_.Name -match "Microsoft*"} | Where-Object {$_.installdate -gt (Get-Date).AddDays($HTT)}).count
If ($CountDateSoftUpdate -gt 2) {
# Write-Host $CountDateHotFix -ForegroundColor green
Write-Output $true
}
Else {
# Write-Host $CountDateHotFix -ForegroundColor red
Write-Output $false
}
DetectWindowsUpdate.ps1 :
$HTT = -30
$CountDateHotFix = (Get-HotFix | Where-Object {$_.InstalledOn -gt (Get-Date).AddDays($HTT)}).count
If ($CountDateHotFix -gt 2) {
# Write-Host $CountDateHotFix -ForegroundColor Yellow
Write-Output $true
}
Else {
# Write-Host $CountDateHotFix -ForegroundColor Red
Write-Output $false
}
Attention :
Il faut noté que les systèmes Windows 7 n'étant plus maintenu depuis le 14 Janvier 2020, remontent comme étant False "non conforme" via le script pour les Windows Update.
Pour ces postes encore existants sous Windows 7 dans certains parcs, il faudrait alors qu'ils soient conforme à cette date en modifiant le script comme ceci :
DetectWindowsUpdateFix.ps1 :
$HTT = (Get-Date -Year 2020 -Month 01 -Day 14 -Hour 00 -Minute 01)
$CountDateHotFix = (Get-HotFix | Where-Object {$_.InstalledOn -gt $HTT }).count
If ($CountDateHotFix -gt 2) {
# Write-Host $CountDateHotFix -ForegroundColor green
Write-Output $true
}
Else {
# Write-Host $CountDateHotFix -ForegroundColor Red
Write-Output $false
}
Autres solutions :
Get-Hotfix utilise Win32_QuickFixEngineering - QFE, la mise à jour rapide. Cela affiche les mises à jour du système Windows via Programme Files, la base de Registre, des fichiers (exe, dll, etc.) mais, c'est moins pratique.
Get-WindowsUpdateLog fusionne les mises à jour de Windows Update qu'il y a dans le journal d’événement afin de les afficher :
Observateur d’événements > Journaux des applications et des services > Microsoft > Windows > WindowsUpdateClient > Operational
Je voulais utiliser un script tel que dans mon précédent article qui permet de vérifier les fichiers modifiés à partir d'une date. Je voulais qu'il soit le plus simple possible et pas forcément précis... au départ !
Ensuite, les correctifs Office n’apparaissent pas avec la commande Get-HotFix et les Windows Update ne sont pas toutes visibles. Get-HotFix interroge uniquement les patchs du système d'exploitation.
Faire un script pour voir les dates de fichiers *.exe ou *.dll n'est pas suffisamment précis, mais qui a-t-il pour les KB de la suite bureautique MS Office et des Windows Update ?
Si vous trouvez, faîtes moi signe.
La solution :
Alors, j'ai fini par adapter le script Get-MicrosoftUpdate.ps1 que j'ai trouvé sur GitHub. Il permet d'Afficher les mises à jour installées du panneau de configuration.
# Gives a list of all Microsoft Updates sorted by KB number/HotfixID
# By Tom Arbuthnot. Lyncdup.com
# https://github.com/tomarbuthnot/Get-MicrosoftUpdate/blob/master/Get-MicrosoftUpdate.ps1
# http://lyncdup.com/2013/09/list-all-microsoftwindows-updates-with-powershell-sorted-by-kbhotfixid-get-microsoftupdate/
### Modify by Olivier Clotaire - blog4me.fr ###
$wu = new-object -com "Microsoft.Update.Searcher"
$totalupdates = $wu.GetTotalHistoryCount()
$all = $wu.QueryHistory(0,$totalupdates)
# Define a new array to gather output
$OutputCollection= @()
Foreach ($update in $all)
{
$string = $update.title
$Regex = "KB\d*"
$KB = $string | Select-String -Pattern $regex | Select-Object { $_.Matches }
$output = New-Object -TypeName PSobject
$output | add-member NoteProperty "HotFixID" -value $KB.' $_.Matches '.Value
$output | add-member NoteProperty "Title" -value $string
$output | add-member NoteProperty "Date" -value $update.date
$OutputCollection += $output
}
# Oupput the collection sorted and formatted:
# $OutputCollection | Sort-Object HotFixID | Format-Table -AutoSize
# Write-Host "$($OutputCollection.Count) Updates Found"
# If you want to output the collection as an object, just remove the two lines above and replace them with "$OutputCollection"
# credit/thanks:
# http://www.gfi.com/blog/windows-powershell-extracting-strings-using-regular-expressions/
##################
# By Olivier CLOTAIRE - blog4me.fr
#
$HTT = -30
$CountDateHotFix = ($OutputCollection | Where-Object {$_.Date -gt (Get-Date).AddDays($HTT)}).count
If ($CountDateHotFix -gt 2) {
Write-Host $CountDateHotFix -ForegroundColor Yellow
Write-Output $true
}
Else {
Write-Host $CountDateHotFix -ForegroundColor Red
Write-Output $false
}
Pour rappel :
Affiche les applications dans Programmes et fonctionnalités :
Get-WmiObject -Namespace ROOT\CIMV2 -Class Win32_Product | Select-Object name, installdate
Affiche les Software Update par dates et par noms :
Get-WmiObject -Namespace ROOT\CIMV2 -Class Win32_Product| Where-Object {$_.Name -match "Microsoft*"}
Affiche les Software Update :
Get-WmiObject -Namespace ROOT\CIMV2 -Class Win32_Product | Select-Object Name, installdate | Where-Object {$_.Name -like "Microsoft*"}