Voici un script qui automatise la mise à jour des agents SCOM lors d’un upgrade de version de la plateforme ou quand un agent a été installé manuellement et /ou avec une version antérieure à celle actuelle.
Il suffit de créer un fichier TXT avec la liste des agents à modifier et lancer le script ci-dessous.
Cela facilite le travail de maintenance quand on n’a un grand nombre d’agents et quand on veut faire des vagues de mise à jour.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
#======================================================================= # NAME: Opsmgr 2012 Agent approval # Created by M.JERBI # # Requirements: Module Operation manager 2012R2 # TXT file with the list of agents which must be updated # # # COMMENT: This script is designed to approve Opsmgr 2012 Agents after an update of the plateform. # #======================================================================= $MS= $null $credential = $null $result = @() #Creation of output file $ScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition $CurrentDate = Get-Date -format yyyyMMdd_hhmmss $ResultFile = $ScriptPath+"\"+ $CurrentDate + "_scom_agent_update_result.csv" #Function declaration #-------------GET FILE NAME Function Get-FileName($initialDirectory) { [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = "Text files (*.TXT)|*.TXT" $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename if ($OpenFileDialog.filename -eq "") { Write-Host "file not found" -BackgroundColor Red exit } } #------------TEST CREDENTIALS Function Test-ADCredentials { $userdomain = $cred.username -split '\\' Add-Type -AssemblyName System.DirectoryServices.AccountManagement $ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain $pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($ct, $userdomain[0]) $psw = New-Object PSObject -Property @{ UserName = $userdomain[1]; IsValid = $pc.ValidateCredentials($userdomain[1],($cred.GetNetworkCredential().password).ToString()) } return $psw } #---------- APPROVE SCOM PENDING MANAGEMENT Function Approve-SCOMpendingagent { param( [Parameter(Mandatory=$false, ValueFromPipeline=$false)] $Management_server, $server, $credential ) if ($credential -ne $null) { if ($testcred.isvalid -ne $true) { $cred = Get-Credential -Credential $credential $testcred = Test-ADCredentials $cred If($testcred.isvalid -eq $false) { Write-Host -BackgroundColor red "Bad password" return } } } #Variables $agents_to_be_update = @() if ($server -ne $null) {$MS = $server} #Get the list of agents which must be updated $Inputfile = Get-FileName "c:\" $targets_to_update = Get-Content $Inputfile $ErrorActionPreference = "silentlycontinue" #Import PowerShell Modules import-module OperationsManager #Connect to OpsMgr Management Group New-SCOMManagementGroupConnection -ComputerName $MS #Get the list of agent which need to be updated $agents_to_be_update = (Get-SCOMPendingManagement | select-object agentname) #checking if the target is in the list of agent which need to be updated foreach ($target_to_update in $targets_to_update) { #If the target is in the list of agent which need to be updated if ($agents_to_be_update -match $target_to_update ) { $erroraction = $null try { if ($credential -ne $null) { Get-SCOMPendingManagement | where {$_.AgentName -eq $target_to_update} | Approve-SCOMPendingManagement -Verbose -ActionAccount $cred} else { Get-SCOMPendingManagement | where {$_.AgentName -eq $target_to_update} | Approve-SCOMPendingManagement -Verbose } } catch { $erroraction = $_.Exception.Message write-host $target_to_update $erroraction -ForegroundColor red $result = $target_to_update +";" +$erroraction |Out-File -FilePath $ResultFile -Append } # Test if the agent has correctly patched if ($erroraction -eq $null) { $test_after_push = Get-SCOMPendingManagement | where {$_.AgentName -eq $target_to_update} if ($test_after_push -ne $false) { $result = $target_to_update +"; error during the agent update process" |Out-File -FilePath $ResultFile -Append write-host $target_to_update $erroraction "error during the agent update process" -ForegroundColor red } Else { write-host -ForegroundColor green $target_to_update "has been updated" $result = $target_to_update +"; has been updated" |Out-File -FilePath $ResultFile -Append } } } #If the target isn't in the list of agent which need to be updated else { write-host -ForegroundColor magenta $target_to_update " no need to be update"|Out-File -FilePath $ResultFile -Append $result= $target_to_update +";"+" no need to be update" } } } |
#POWERSHELL
#SCOM
#AGENT APPROVAL