About Me

My photo
HANUMANGARH, RAJASTHAN, India

Saturday 4 June 2022

Get Windows update status from remote host

 $TestCSVFile=”C:\Temp\DCPatchingStatus.CSV”

Remove-item $TestCSVFile -ErrorAction Ignore

$ThisStr=”Server,Connection,Command Status, Number of Updates Applied Since last 30 Days, Last Update Date,Final Status”

Add-Content “$TestCSVFile” $ThisStr

$GDCList = "C:\Patching\servers.txt"



$TotNo=0

$ItemCount=0

$TestText = “”

$TestStatus=””

$SumVal = “”

$AnyGap = “No”

$ErrorOrNot = “No”

$AnyOneOk = “No”

$TotDCsInError = 0


Foreach ($ItemName in Get-Content “$GDCList”)

{

    $DCConError = “Ok”

    $DCConStatus = “Ok”

    $ProceedOrNot = “Yes”

    $Error.Clear()

    $AllServices = Get-WMIObject Win32_Service -computer $ItemName


    IF ($Error.Count -ne 0)

        {

            $ProceedOrNot = “No”

            $TotDCsInError++

            $DCConError = $Error[0].Exception.Message

            #$FinalStatusNow = “Not OK”

            $FinalSTR = $ItemName+”,Error: $DCConError"


            Add-Content “$TestCSVFile” $FinalSTR

        }

    

    IF ($ProceedOrNot -eq “Yes”)

        {

            $ComConError=”Ok”

            $Error.Clear()

            $TotHotFixes = Get-HotFix -ComputerName $ItemName | Where-Object {$_.Installedon -gt ((Get-Date).Adddays(-30))}

            $AnyOneOk=”Yes”

            $TotHF = $TotHotFixes.Count

            $FinalStatusNow = “OK”


                IF ($TotHF -eq 0)

                    {

                        $IsHFOk = “No”

                        $AnyGap = “Yes”

                        $FinalStatusNow = “WARNING: Not up to date”

                    }

$TotHotFixes = Get-HotFix -ComputerName $ItemName | ?{ $_.installedon } | sort @{e={[datetime]$_.InstalledOn}} | select -last 1

$LastNowAll = $TotHotFixes.InstalledOn.Date


    IF ($AnyGap -eq “Yes”)

        {

            $TotNo++

        }


$ThisSTr = $ItemName+”,”+$DCConError+”,”+$ComConError+”,”+$TotHF+”,”+$LastNowAll+”,”+$FinalStatusNow

Add-Content “$TestCSVFile” $ThisStr

}


    else

        {

            $ComConError = $Error[0].Exception.Message

            $FinalSTR = $ItemName+”,$DCConError #,”+$ComConError

            #Add-Content “$TestCSVFile” $FinalSTR

        }

}

$OthText = “”

    IF ($TotDCsInError -ne 0)

    {

        $OthText = “Some server have not been checked due to connectivity or command issues.”

    }


    IF ($AnyGap -eq “Yes”)

    {

        $TestText = “Some servers have not been patched. $OthText”

        $SumVal = $TotNo

        $TestStatus=”High”

    }


IF ($AnyGap -eq “No”)

    {

    $TestText = “All Servers have been patching since last 30 days. Please load and check result to ensure Last Pathing Date is current. $OthText”

    $SumVal = “”

    $TestStatus=”Passed”


IF ($AnyOneOk -eq “No”)

    {

    $TestText = “Error Executing Dynamic Pack.”

    $SumVal = “”

    $TestStatus=”Completed with Errors.”

    }

}

$STR = $ADTestName +”,”+$TestStartTime+”,”+$TestStatus+”,”+$SumVal +”,”+$TestText

No comments:

Post a Comment

Export contact from Justdial

  Extract Data From JustDial using Selenium Let us see how to extract data from Justdial using Selenium and Python. Justdial is a company th...