Friday, July 14, 2017

Using Powershell to install advertised OS Patches from SCCM software Center

#Get a list of server from DBA DW datatbase
$servername = invoke-sqlcmd -ServerInstance 2012eitsqltest -Database DBA_ServerDW `
-Query "select ServerName from DBA_ServerDW.dbo.SQLServers
where servername not like '%\% and'
and OSPatchServerMonthly = 1 and Decommissioned = 0
and Environment in('QA','UAT')"


foreach($server in $servername)

{

                $ServerN=$server.ServerName

             
#Compliance state of the software update that indicates if the software update is missing and needs to be installed.ComplianceState=0, 0 =ciNotPresent

$AvailableUpdates = Get-WmiObject -Class CCM_SoftwareUpdate -Filter ComplianceState=0 -Namespace root\CCM\ClientSDK -Computername $ServerN
              

# The following is done to do 2 things: Get the missing updates (ComplianceState=0)and take the PowerShell object and turn it into an array of WMI objects 

$AvailableUpdatesReformatted = @($AvailableUpdates | ForEach-Object {if($_.ComplianceState -eq 0){[WMI]$_.__PATH}}) 
              
                            
                
# The following is the invoke of the CCM_SoftwareUpdatesManager.InstallUpdates with our found updates 
# NOTE: the command in the ArgumentList is intentional, as it flattens the Object into a System.Array for us 
# The WMI method requires it in this format.


$InstallReturn = Invoke-WmiMethod -Class CCM_SoftwareUpdatesManager -Name InstallUpdates -ArgumentList (,$AvailableUpdatesReformatted) -Namespace root\ccm\clientsdk -Computername $ServerN
                 
                               
                                     
                                
}
  
  

foreach($server in $servername)

{

       $ServerN_Check=$server.ServerName


        # Set a variable to True
        $Allinstalledstatus = $True

#After installation is started ,get completion status of all updates installation by getting evaluationstate
#NOTE : When a software update is not in a progress state, the value of EvaluationState is none or available, depending on whether there was any progress at any point in the past
#if a software update was downloaded at activation time, the value of EvaluationState is none. 

While ($Allinstalledstatus -ne $false)
                
                        {
                
                    
 # Checking EvaluationState( 0=ciJobStateNone,2=ciJobStateSubmitted, 3=ciJobStateDetecting, 4=ciJobStatePreDownload, 5=ciJobStateDownloading, 6=ciJobStateWaitInstall, 7=ciJobStateInstalling, 11=ciJobStateVerifying)
#If Evaluation State is not False stay in while loop

$CCMUpdate = get-wmiobject -query "SELECT EvaluationState FROM CCM_SoftwareUpdate" -namespace "ROOT\ccm\ClientSDK" -Computername $ServerN_Check
                            if(@($CCMUpdate | where {$_.EvaluationState -eq 0 -or  $_.EvaluationState -eq 2 -or $_.EvaluationState -eq 3 -or $_.EvaluationState -eq 4 -or $_.EvaluationState -eq 5 -or $_.EvaluationState -eq 6 -or $_.EvaluationState -eq 7 -or $_.EvaluationState -eq 11 }).length -ne 0) { $Allinstalledstatus=$true } else { $Allinstalledstatus=$false }  
write-host $ServerN_Check
write-host $Allinstalledstatus
                
}
                                                    
#When installation is done, it returns False.

If($Allinstalledstatus -eq $false)
{
            
#Get EvaluationState of each patch, KB name and number, URL

$StatusList = (Get-WmiObject -ComputerName $ServerN_Check -Query 'SELECT EvaluationState,ArticleID,Name,URL,ErrorCode FROM CCM_SoftwareUpdate where ComplianceState = 0' -Namespace ROOT\ccm\ClientSDK);
                
                        foreach ($Status in $StatusList)
                        {
                            $InstalledStatus= $Status.EvaluationState
                            $KBDescription= $Status.ArticleID +' '+ $Status.Name +' '+ $Status.URL
                            $ErrorCode = $Status.ErrorCode
                                       
#Log the updates status to Log Table
                            invoke-sqlcmd -ServerInstance 2012eitsqltest -Database DBA_ServerDW `
                            -Query  "INSERT INTO SCCM_InstalledOSUpdates([ServerName],[KBDescription],[InstalledStatus],ErrorCode)
                            VALUES ('$ServerN_Check','$KBDescription','$InstalledStatus','$ErrorCode')"
                        
                        }

                        }

}    

No comments:

Post a Comment

How to add a Database to AlwaysOn Availability Group with four different options

To add a database to an existing AlwaysOn availability group, MS has given us four options to choose from Automatic seeding Full database an...