# Powershell script to check whether multiple KB are installed or not in servers

**URL:** <https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827>\
**Category:** PowerShell Help\
**Created:** [December 8, 2017, 10:57am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827 "2017-12-08T10:57:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![shalomon](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@shalomon](https://forums.powershell.org/u/shalomon)\
**Post date:** [December 8, 2017, 10:57am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/1 "2017-12-08T10:57:20Z")

</div>

Powershell Script to find patched and unpatched server!

I have multiple KB’s, I am trying to find whether those KB’s have been patched in those servers.

I have two text file, One contains servers list and other one contains KB list.

Please find my script below.

$computers = Get-Content -path “C:\Temp\Shalomon\MultipleKB\server\_list.txt”  
$Patch = Get-Content -path “C:\Temp\Shalomon\MultipleKB\kb\_list.txt”  
foreach ($computer in $computers)  
{  
if (get-hotfix -id $Patch -ComputerName $computer -ErrorAction SilentlyContinue)  
{  
Add-content “$Patch is Present in $computer” -path “C:\Temp\Shalomon\MultipleKB\Hotfix-Present.txt”  
}  
Else  
{  
Add-content “$Patch is not Present in $computer” -path “C:\Temp\Shalomon\MultipleKB\Missing-Hotfix.txt”  
}  
}

\*\*\*\*\*\*\*\*\*\*\*\*\*\*But i get the output as

KB4025337 KB4025252 is Present in vmpip-xxxx  
KB4025337 KB4025252 is Present in vmpip-yyyy  
KB4025337 KB4025252 is Present in VMPIP-zzzz

Note: I do not get the servers in which the KB’s are missing, Can someone help me out in this code!

---

<div class="post-metadata">

**Author:** ![jacorbello](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@jacorbello](https://forums.powershell.org/u/jacorbello)\
**Post date:** [December 8, 2017, 11:19am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/2 "2017-12-08T11:19:47Z")

</div>

Here, use this one. Made it a while back to do exactly that.  
[https://www.jeremycorbello.com/index.php/powershell/general-scripts/5-get-mshotfix-ps1](https://www.jeremycorbello.com/index.php/powershell/general-scripts/5-get-mshotfix-ps1)

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [December 8, 2017, 11:30am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/3 "2017-12-08T11:30:06Z")

</div>

Change this:

```
get-hotfix -id $Patch -ComputerName $computer -ErrorAction SilentlyContinue
```

To:

```
get-hotfix -id $Patch -ComputerName $computer -ErrorAction SilentlyContinue -outvariable patchresult
```

Then do if your if/else on the $patchresult variable, if the variable is not null then you know you have the patch, if it’s null then you don’t have it.

---

<div class="post-metadata">

**Author:** ![shalomon](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@shalomon](https://forums.powershell.org/u/shalomon)\
**Post date:** [February 21, 2018, 12:00am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/4 "2018-02-21T00:00:46Z")

</div>

@Jon

I tried doing if/else on $patchresult variable as per your comment. But still it is not working out.  
Can you help me out

Thanks,  
Shalomon.N

---

<div class="post-metadata">

**Author:** ![jarkko](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@jarkko](https://forums.powershell.org/u/jarkko)\
**Post date:** [February 21, 2018, 2:09pm UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/5 "2018-02-21T14:09:47Z")

</div>

Hi

If you have -errorAction why not to use Stop on that and use Try, Catch? I don’t know what the get-hotfix is and never tried but if this could help or then remove the $patch froeach and change $kb to $patch.

Below typed here not tested in any way:

```
$computers = Get-Content -path "C:\Temp\Shalomon\MultipleKB\server_list.txt"
$Patch = Get-Content -path "C:\Temp\Shalomon\MultipleKB\kb_list.txt"

$data = foreach ($computer in $computers) {

    ForEach ($kb in $patch) {

    Try {
        get-hotfix -id $kb -ComputerName $computer -ErrorAction Stop

        [PSCustomObject]@{
            Server = $computer
            KB = $kb
            Status = 'Installed'
        }

    } Catch {
        [PSCustomObject]@{
            Server = $computer
            KB = $kb
            Status = 'Not Installed'
        }
    }
}
}

$data | Export-csv alldata.csv -notypeinformation -delimiter ';' -encoding utf8
```

Regards

Jake

---

<div class="post-metadata">

**Author:** ![shalomon](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@shalomon](https://forums.powershell.org/u/shalomon)\
**Post date:** [February 22, 2018, 5:11am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/6 "2018-02-22T05:11:42Z")

</div>

Hi Jake,

Thanks for your reply,

I tried the above script, It has worked out in someway.

The only issue is that the information which is received in alldata.csv file is not arranged in a proper way.

PSComputerName;“InstalledOn”;“\_\_GENUS”;“\_\_CLASS”;“\_\_SUPERCLASS”;“\_\_DYNASTY”;“\_\_RELPATH”;“\_\_PROPERTY\_COUNT”;“\_\_DERIVATION”;“\_\_SERVER”;“\_\_NAMESPACE”;“\_\_PATH”;“Caption”;“CSName”;“Description”;“FixComments”;“HotFixID”;“InstallDate”;“InstalledBy”;“Name”;“ServicePackInEffect”;“Status”  
12345-xxxxxx;“8/6/2017 12:00:00 AM”;“2”;“Win32\_QuickFixEngineering”;“CIM\_LogicalElement”;“CIM\_ManagedSystemElement”;“Win32\_QuickFixEngineering.HotFixID=”“KB4025337"”  
;;;;;;;;;;;;;;;;;;;;;“Installed”  
;;;;;;;;;;;;;;;;;;;;;“Not Installed”  
VM123-yyyyyy;“8/6/2017 12:00:00 AM”;“2”;“Win32\_QuickFixEngineering”;“CIM\_LogicalElement”;“CIM\_ManagedSystemElement”;“Win32\_QuickFixEngineering.HotFixID=”“KB4025337"”  
;;;;;;;;;;;;;;;;;;;;;“Installed”  
;;;;;;;;;;;;;;;;;;;;;“Not Installed”  
;;;;;;;;;;;;;;;;;;;;;“Not Installed”  
VM456-zzzzzz;“8/6/2017 12:00:00 AM”;“2”;“Win32\_QuickFixEngineering”;“CIM\_LogicalElement”;“CIM\_ManagedSystemElement”;“Win32\_QuickFixEngineering.HotFixID=”“KB4025252"”  
;;;;;;;;;;;;;;;;;;;;;“Installed”

---

<div class="post-metadata">

**Author:** ![jarkko](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@jarkko](https://forums.powershell.org/u/jarkko)\
**Post date:** [February 22, 2018, 6:35am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/7 "2018-02-22T06:35:44Z")

</div>

Hi

What your kblist.txt includes? I think that needs to be sort out after get-content.

Regards

Jake

---

<div class="post-metadata">

**Author:** ![shalomon](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@shalomon](https://forums.powershell.org/u/shalomon)\
**Post date:** [February 22, 2018, 7:38am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/8 "2018-02-22T07:38:33Z")

</div>

The KB’s which are mentioned in kb\_list.txt file, We are using it to check whether those KB’s are installed or not in those virtual machines

kb\_list.txt includes the KB number

Example  
Kb567234  
Kb562435  
Kb724314

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [February 22, 2018, 8:54am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/9 "2018-02-22T08:54:32Z")

</div>

This works for me

```
$computers = gc C:\scripts\computers\computers.txt
$patches = gc C:\scripts\computers\kb.txt

foreach ($computer in $computers){
 foreach ($patch in $patches){

 Get-HotFix -id $patch -ComputerName $computer -OutVariable results -ErrorAction SilentlyContinue

 if ($results -ne $null) {

 $results | Out-File C:\scripts\computers\report.txt -Append -Force

 }

 else {

 Add-content "$Patch is not Present in $computer" -path "C:\scripts\computers\report2.txt"

 }

 }
 }
```

---

<div class="post-metadata">

**Author:** ![shalomon](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@shalomon](https://forums.powershell.org/u/shalomon)\
**Post date:** [February 23, 2018, 2:22am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/10 "2018-02-23T02:22:59Z")

</div>

Hi Jon,

Thanks for your timely reply, Above script works fine.

Just need a help with small modification, I need to catch VM’s separately in a text file which i am not able to connect.

Is it possible to do it in above script?

Thanks  
Shalomon.N

---

<div class="post-metadata">

**Author:** ![sanchez](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@sanchez](https://forums.powershell.org/u/sanchez)\
**Post date:** [February 23, 2018, 8:25am UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/11 "2018-02-23T08:25:46Z")

</div>

You would probably need to do some sort of try / catch statement read [this](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_try_catch_finally?view=powershell-6) to find out about it and how to implement it in your code. Also check the ‘articles’ section at the top and look through the iron scripter preqeuel commentary articles, they have some good examples of try catch in there.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:36pm UTC](https://forums.powershell.org/t/powershell-script-to-check-whether-multiple-kb-are-installed-or-not-in-servers/9827/12 "2024-05-16T20:36:19Z")

</div>


