$file_patch_server="C:\test\powershell\patch_list.txt"
$filepath = "C:\test\powershell\"
$KBNo_2019="KB5028168"
$KBNo_2016="KB5028169"
$filename="patch_list_"+$KBNo+".txt"
$filepath=$filepath+$filename
$ComPList = Get-Content "$file_patch_server"
foreach ($Server in $ComPList)
{ #echo $Server
$version=(Get-WmiObject -ComputerName $Server -class Win32_OperatingSystem).Caption
$hotFixID=""
$installedOn=""
if ($version -match '2019')
{
$patchinfo=Get-HotFix -ComputerName $Server | Where-Object {$_.HotFixID -Like $KBNo_2019}
$installedOn=$patchinfo.InstalledOn
$hotFixID=$patchinfo.HotFixID
#echo $patchinfo.HotFixID,$patchinfo.Source,$patchinfo.InstalledOn
}
if ($version -match '2016')
{
$patchinfo=Get-HotFix -ComputerName $Server | Where-Object {$_.HotFixID -Like $KBNo_2016}
$installedOn=$patchinfo.InstalledOn
$hotFixID=$patchinfo.HotFixID
#echo $patchinfo.HotFixID,$patchinfo.Source,$patchinfo.InstalledOn
}
echo $Server $hotFixID $installedOn
# "$Server $hotFixID $installedOn"|Export-CSV -Path C:\test\powershell\patch_result.csv -Append
$patch_status = New-Object PSObject -Property @{ servername = $Server; hotFixID = $hotFixID;installedOn=$installedOn }
Export-Csv -InputObject $patch_status -Path C:\test\powershell\patch_result.csv -NoTypeInformation -Append
}