Author Posts

December 8, 2017 at 4:27 pm

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!

December 8, 2017 at 5:00 pm

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.

February 21, 2018 at 5:30 am

@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

February 21, 2018 at 7:39 pm

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

February 22, 2018 at 10:41 am

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"

February 22, 2018 at 12:05 pm

Hi

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

Regards

Jake

February 22, 2018 at 1:08 pm

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

February 22, 2018 at 2:24 pm

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"

 }

 }
 }

February 23, 2018 at 7:52 am

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

February 23, 2018 at 1:55 pm

You would probably need to do some sort of try / catch statement read this 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.