jeudi 28 avril 2011

Installer Open Manage sur ESXi

Et voici une fonction Power Shell bien pratique pour installer Open Manage sur un ESXi.
J'ai récupéré la fonction de base sur différents site, puis je l'ai un petit peu modifié :)

Après avoir copié cette fonction dans votre invite, il suffit de lancer la commande suivante pour déployer Open Manage:
installDellOpenManage 'E:\Sources' 'OM-SrvAdmin-Dell-Web-6.5.0-2247.VIB-ESX41i_A01' 'MyCom' 'frves-centreon01' 'frbrs-vc01' 'frbrh-esx73' 'root' 'MyPasswOrd!'

function installDellOpenManage ($DellOMSA, $FolderName, $SNMPCommunity, $SNMPTargetHost, $vCenter, $ESXHost, $User, $Password){
#Input Parameters:
#DellOMSA: Path to the DELL Open Manage folder
#FolderName: Name of the Open Manage folder
#SNMPCommunity: Your SNMP Community
#SNMPTargetHost: Your SNMP Host
#vCenter: The vCenter
#ESXHost: The ESXi host you want
#User: The user to connect to the ESX
#Password: The associated password
#
#Description:
#Install Dell Open Manage and configure the SNMP Community
#
#Exemple: installDellOpenManage 'E:\Sources' 'OM-SrvAdmin-Dell-Web-6.5.0-2247.VIB-ESX41i_A01' 'MyCom' 'frves-centreon01' 'frbrs-vc01' 'frbrh-esx73' 'root' 'MyPasswOrd!'

#Save and change the prompt
$oldPrompt = (Get-Command prompt).definition
set-item -path function:prompt -value {" "}

#Try to ping the host
$ping = new-object System.Net.NetworkInformation.Ping
$Reply = $ping.send($ESXHost)
if ($Reply.status –eq “Success”){
write "Server successfully pinged. Connecting..."
} else {
write "" "The server could not be pinged. Please check your host name" ""
Exit
}

#Connect to vCenter (in order to migrate VMs)
Connect-VIServer $vCenter
Write "Retriving $ESXHost object"
$VMHostObj = Get-VMHost $ESXHost

$hostState = $VMHostObj.State

#Check if host is in maintenance mode
If ($hostState -match "Maintenance"){
write-host "Host is already in Maintenance mode. Continuing..."
} else {
write-host "The host is not in the correct mode. It needs to be in maintenance mode but is currently in " -nonewline $hostState " mode."
write-host "If the host is not in a DRS Cluster, please evacuate the VMs before"
$Maint = Read-Host "Do you wish to put it into Maintenance mode? If so, please type YES: "
If ($Maint -eq "YES"){
Write "Putting host into maintenance Mode"
Set-VMHost $VMHostObj -State Maintenance -Confirm:$false | Out-Null
} Else {
disconnect-viserver $vCenter -Confirm:$false
Exit
}
}

#Restart host
Write "Rebooting host now"
Restart-VMHost $VMHostObj -Confirm:$false | Out-Null
disconnect-viserver $vCenter -Confirm:$false | Out-Null

#Try to ping ...
$ping = new-object System.Net.NetworkInformation.Ping
do{
Start-Sleep -s 5
$Reply = $ping.send($ESXHost)
if ($Reply.status –eq “Success”){
write "Server successfully pinged. Attemping Connect after 20second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Attemping Connect after 15second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Attemping Connect after 10second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Attemping Connect after 5second sleep..."
Start-Sleep -s 5
$VS = Connect-VIServer $ESXHost -user $User -password $Password
} else {
write "Server rebooting..."
}
} while ($VS.isconnected -ne "True")

write "Server is back online and connected."

Write "Retriving $ESXHost object"
start-sleep 30
$VMHostObj = Get-VMHost $ESXHost

#Upload files to a datastore
Write "Checking for OMSA install files"
$DellOMSA = "$DellOMSA\$FolderName"
if (-Not (Test-Path "$DellOMSA\metadata.zip")){
Write "$DellOMSA does not exist, please make sure this is the OMSA zip file extracted contents."
Exit
} Else {
Write "Install files found"
}

Write "Copying OMSA client to the host datastore"
$ds = $VMHostObj | Get-Datastore | Select -first 1
Copy-DatastoreItem $DellOMSA $ds.DatastoreBrowserPath -Recurse

#Install Open Manage
Write "Installing Dell OMSA"
$DSName = $ds.Name
$InstalledOMSA = Install-VMHostPatch -vmhost $VMHostObj -HostPath /vmfs/volumes/$DSName/$FolderName/metadata.zip

#Delete files from Datastore
Write "Removing Install files"
$urlPath = "https://" + $esxhost + "/folder/" + $FolderName + "?dcPath=ha-datacenter&dsName=" + $dsName
$fileMgr = Get-View (Get-View ServiceInstance).Content.FileManager
$fileMgr.DeleteDatastoreFile_Task($urlPath,$null)

#Restart the host
Write "Rebooting host now"
Restart-VMHost $VMHostObj -Confirm:$false | Out-Null
disconnect-viserver $ESXHost -Confirm:$false | Out-Null

#Try to ping...
$ping = new-object System.Net.NetworkInformation.Ping
do{
Start-Sleep -s 5
$Reply = $ping.send($ESXHost)
if ($Reply.status –eq “Success”){
write "Server successfully pinged. Attemping Connect after 20second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Attemping Connect after 15second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Attemping Connect after 10second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Attemping Connect after 5second sleep..."
Start-Sleep -s 5
$VS = Connect-VIServer $ESXHost -user $User -password $Password
} else {
write "Server rebooting..."
}
} while ($VS.isconnected -ne "True")

write "Server is back online and connected."
Write "Retriving $ESXHost object"
$VMHostObj = Get-VMHost $ESXHost

#Enable the CIM
Write "Enabling CIM OEM Providers"
Set-VMHostAdvancedConfiguration -VMHost $VMHostObj -Name UserVars.CIMoemProviderEnabled -Value 1 | Out-Null

#Setup the SNMP
write "Setting up SNMP Object"
$snmpObj = Get-VMHostSnmp

Write "Setting SNMP Configuration"
$snmpObj | Set-VMHostSnmp -ReadOnlyCommunity $SNMPCommunity
$snmpObj | Set-VMHostSnmp -AddTarget -TargetHost $SNMPTargetHost -TargetCommunity $SNMPCommunity -TargetPort 162
$snmpObj | %{ Set-VMHostSnmp -HostSnmp $_ -Enabled:$true }

$snmpObj = Get-VMHostSnmp
$isEnabled = $snmpObj.Enabled
write "" "" "--------------------"
if ($isEnabled -match "False"){
write "SNMP Is Not Enabled"
} elseif ($isEnabled -match "True"){
write "Snmp set to enabled"
} else {
write "Could not check SNMP Enabled Status"
}

Write ""

Write "Checking SNMP Settings..."
If ((Get-VMHostSnmp).ReadOnlyCommunities -ne $SNMPCommunity){
Write "SNMP Read Community Incorrect!"
} elseif ((Get-VMHostSnmp).ReadOnlyCommunities -eq $SNMPCommunity){
Write "SNMP Read Community Successfully Set!"
} else {
Write "Unable to check SNMP Read Community"
}

Write ""

$snmpTraps = $snmpObj.TrapTargets
Write-host "Number of Trap targets: " -nonewline $snmpTraps.length
write ""
$foundTrap = 0
foreach ($trap in $snmpTraps){
$trapHost = $trap.hostname
if ($trapHost -eq $SNMPTargetHost){
$foundTrap = 1
write-host "    " -nonewline $trapHost
} else {
write-host "    " -nonewline $trapHost
}
write ""
}
if ($foundTrap -eq 1){
write "SNMP Trap destination found"
} else {
write "SNMP Trap desination not found!!!"
}
write "" "" "--------------------"

#Restart the host
Write "Rebooting host again to verify that SNMP Settings saved. (This is put in here due to an issue with OMSA wiping settings on first reboot, and sometimes second reboot)"
Restart-VMHost $VMHostObj -Confirm:$false | Out-Null
disconnect-viserver $ESXHost -Confirm:$false | Out-Null

#Try to ping...
$ping = new-object System.Net.NetworkInformation.Ping
do{
Start-Sleep -s 5
$Reply = $ping.send($ESXHost)
if ($Reply.status –eq “Success”){
write "Server successfully pinged. Connecting after 20second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Connecting after 15second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Connecting after 10second sleep..."
Start-Sleep -s 5
write "Server successfully pinged. Connecting after 5second sleep..."
Start-Sleep -s 5
$VS = Connect-VIServer $ESXHost -user $User -password $Password
} else {
write "Server rebooting..."
}
} while ($VS.isconnected -ne "True")
write "Server is back online and connected."

#Check SNMP
$snmpObj = Get-VMHostSnmp
$isEnabled = $snmpObj.Enabled
write "" "" "--------------------"
if ($isEnabled -match "False"){
write "SNMP Is Not Enabled"
} elseif ($isEnabled -match "True"){
write "Snmp set to enabled"
} else {
write "Could not check SNMP Enabled Status"
}

Write ""

Write "Checking SNMP Settings..."
If ((Get-VMHostSnmp).ReadOnlyCommunities -ne $SNMPCommunity){
Write "SNMP Read Community Incorrect!"
} elseif ((Get-VMHostSnmp).ReadOnlyCommunities -eq $SNMPCommunity){
Write "SNMP Read Community Successfully Set!"
} else {
Write "Unable to check SNMP Read Community"
}

Write ""

$snmpTraps = $snmpObj.TrapTargets
Write-host "Number of Trap targets: " -nonewline $snmpTraps.length
write ""
$foundTrap = 0
foreach ($trap in $snmpTraps){
$trapHost = $trap.hostname
if ($trapHost -eq $SNMPTargetHost){
$foundTrap = 1
write-host "    " -nonewline $trapHost
} else {
write-host "    " -nonewline $trapHost
}
write ""
}
if ($foundTrap -eq 1){
write "SNMP Trap destination found"
} else {
write "SNMP Trap desination not found!!!"
}
write "" "" "--------------------"

Write "OMSA Install Completed"

Write "Retriving $ESXHost object"
start-sleep 15
$VMHostObj = Get-VMHost $ESXHost
$hostState = $VMHostObj.State

#Exit the maintenance mode
If ($hostState -match "Maintenance"){
write-host "Host is in Maintenance mode."
$Maint = Read-Host "Do you wish to exit Maintenance mode? If so, please type YES: "
If ($Maint -eq "YES"){
Write "Exit maintenance Mode"
Set-VMHost $VMHostObj -State Connected -Confirm:$false | Out-Null
} Else {
Exit
}
} else {
write-host "Host is not in Maintenance mode."
}

disconnect-viserver $vCenter -Confirm:$false

#Put initial prompt
$oldprompt | Set-Item function:prompt
}

Aucun commentaire:

Enregistrer un commentaire