PowerShell/VMware/setNewESXiHostConfiguration.ps1
2025-02-19 17:53:28 +00:00

230 lines
13 KiB
PowerShell

<#
#############################################################################################################################################
# #
# ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! #
# #
# Am Ende der Durchführung dieses Skripts werden die zu konfigurierenden ESXi Hosts AUTOMATISCH NEU GESTARTET !! #
# Bitte ALLE zu konfigurierenden ESXi Hosts vorher in den Maintenance Mode fahren! #
# #
# ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! W A R N U N G ! ! ! #
# #
#############################################################################################################################################
.SYNOPSIS
Skript zum initialen Konfigurieren von neuen ESX-Hosts in der ASL.
.DESCRIPTION
Dieses Skript konfiguriert automatisiert die folgenden Einstellungen für neue in Betrieb zu nehmende ESXi Host anhand einer CSV Datei mit Hostnamen:
- NTP Server konfigurieren: ntp.akqui.net
- NTP Dienst auf Autostart und durchstarten des Dienstes
- Firewallfreischaltung für Update Manager
- SSH aktivieren
- Shell Warnings unterdrücken
- DNS entsprechned Standort konfigurieren
- Storage Adapter konfigurieren
- Firewallregeln und Freischaltung für NTP
- Host rebooten
.NOTES
Historie: v0.1 : 23.01.2019
erste laufende Version
v1.0 : 23.01.2019
Author : Axel Weichenhain
last change : 23.01.2019
Testlauf mit erchpxpes01.asl.local
.INPUTS
Skript muss über die Kommandozeile mit bestimmten Parametern aufgerufen werden (siehe unten).
.OUTPUTS
Rückmeldung über Aktionen zur Laufzeit
.PARAMETER vcenter
Optional: Name des abzufragenden vCenters. Wird dieser nicht angegeben, verbindet sich das Skript automatisch mit allen vCentern in der ASL.LOCAL
Dies sind zur Zeit:
"admhpwvvc03.asl.local",
"oss0plvvc01.asl.local",
"oss0plvvc02.asl.local",
"dcc0plvvc01.asl.local",
"del0plvvc01.asl.local",
"cjd0plvvc01.asl.local"
.EXAMPLE
setNewESXiHostConfiguration.ps1 -vCenter "oss0plvvc01.asl.local"
.PARAMETER Username
Mandatory: Benutzer zum Verbinden mit vCenter(n)
Wird dieser Parameter nicht gleich beim Aufruf des Skripts angegeben, so wird dieser vom Skript interaktiv abgefragt.
.EXAMPLE
setNewESXiHostConfiguration.ps1 -vCenter "oss0plvvc01.asl.local" -Username "asl\adm.<nachname>"
.PARAMETER Password
Mandatory: Kennwort zum Verbinden mit vCenter(n)
Wird dieser Parameter nicht gleich beim Aufruf des Skripts angegeben, so wird dieser vom Skript interaktiv abgefragt.
.EXAMPLE
setNewESXiHostConfiguration.ps1 -vCenter "oss0plvvc01.asl.local" -Username "asl\adm.<nachname>" -Password "<yourpasswordhere>"
.PARAMETER ImportFile
Mandatory: Pfad und Dateiname der zu importierenden Liste von Hostnamen
Einfache Textdatei mit Hostname pro Zeile:
host1.asl.local
host2.asl.local
.
.
.
.EXAMPLE
setNewESXiHostConfiguration.ps1 -vCenter "oss0plvvc01.asl.local" -Username "asl\adm.<nachname>" -Password "<yourpasswordhere>" -ImportFile "c:\temp\import.txt"
#>
# Parameterabfrage
PARAM (
[Parameter(HelpMessage="vCenter Server Hostname/IP Address", Mandatory=$false)][string] $vCenter,
[Parameter(HelpMessage="Pfad zur Hostliste", Mandatory=$true)][string] $ImportFile,
[Parameter(HelpMessage="vCenter account's username.", Mandatory=$true)][string] $Username,
[Parameter(HelpMessage="vCenter account's password.", Mandatory=$true)][string] $Password
)
#Primäre DNS Server der einzelnen Standorte
$nor01 = "172.20.6.51"
$ham02 = "172.20.6.50"
$ham01 = "172.20.6.11"
$iz = "172.21.6.11"
cls
#Initialisierung der Powershell Module
import-module vmware.vimautomation.core
#PowerCLI für ungültige Zertifikate und Verbindung mit mehreren vCentern konfigurieren
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -DefaultVIServerMode Multiple -Scope User, allusers -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
#Wenn der Parameter "vCenter" mit angegeben wurde:
if($vCenter -ne ""){
$vcenters = $vCenter
}
#Wenn der Parameter "vCenter" nicht mit angegeben wurde
else{
$vcenters = @(
"admhpwvvc03.asl.local",
"oss0plvvc01.asl.local",
"oss0plvvc02.asl.local",
"dcc0plvvc01.asl.local",
"del0plvvc01.asl.local",
"cjd0plvvc01.asl.local"
)
}
#Verbindung mit vCenter
Connect-VIServer $vcenters -user $Username -Password $Password
cls
#Hostliste einlesen
#CSV-Datei importieren
Write-Host "Importiere Liste der zu konfigurierenden ESXi Hosts" -ForegroundColor Green
$HostList = get-content $ImportFile
Write-Host "Hostliste wurde importiert" -ForegroundColor Green
# Hosts abarbeiten
Write-Host "Starte Verarbeitung der zu konfigurierenden ESXi Hosts" -ForegroundColor Green
# Jeden Host in der Liste bearbeiten
foreach($entry in $HostList){
#Host aus vCenter einlesen
$vmhost = Get-VMHost $entry
# Schritt Nummer 1 - Standort und Kennwort ermitteln
$hostname = $vmhost.Name
Write-Host "Konfiguriere " $hostname -ForegroundColor Green
#Standort
Write-Host "Ermittle Standort von " $hostname -ForegroundColor Green
$hostlocation = $hostname.substring(3,1)
#Root Kennwort
Write-Host "Ermittle Root-Kennwort von " $hostname -ForegroundColor Green
$hostnumber = $hostname.substring(($hostname.Length - 12),2)
$hostpw = "getXS2ES" + $hostnumber + "!"
# Schritt Nummer 2 - NTP konfigurieren
Write-Host "Konfiguriere NTP von " $hostname -ForegroundColor Green
$ntpsrvs = $vmhost | Get-VMHostNtpServer
foreach ($ntpsrv in $ntpsrvs){
Remove-VMHostNtpServer $ntpsrv -VMHost $vmhost -Confirm:$false
}
$vmhost | Add-VMHostNtpServer ntp.akqui.net #NTP Server 1
# Schritt Nummer 3 - Firewall und Dienste konfigurieren
Write-Host "Konfiguriere Firewall und starte Dienste von " $hostname -ForegroundColor Green
$vmhost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
$vmhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
$vmhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | ReStart-VMHostService -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
$vmhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
$vmhost | Get-VMHostFirewallException | where {$_.Name -eq "vCenter Update Manager"} | Set-VMHostFirewallException -Enabled:$true -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
# Schritt Nummer 4 - SSH aktivieren und Shell Warnings unterdrücken
Write-Host "Unterdruecke Shell Warnings von " $hostname -ForegroundColor Green
$vmhost | Get-VmHostService | Where-Object {$_.key -eq "TSM"} | Set-VMHostService -policy "on"
Start-VMHostService -HostService ($vmHost | Get-VMHostService | Where { $_.Key -eq "TSM"}) | Out-null
$vmhost | Get-VmHostService | Where-Object {$_.key -eq "TSM-SSH"} | Set-VMHostService -policy "on"
Start-VMHostService -HostService ($vmHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"}) | Out-null
$vmhost | Get-AdvancedSetting UserVars.SuppressShellWarning | Set-AdvancedSetting -Value 1 -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
# Schritt Nummer 5 - DNS Konfiguration
Write-Host "Prüfe Standort von " $hostname -ForegroundColor Green
#Prüfen ob Host in Darmstadt -> muss manuell gepflegt werden.
if($hostlocation -eq "d"){
Write-Host "Standort Darmstadt wird für DNS nicht beachtet! Host muss manuell konfiguriert werden!" -ForegroundColor Red
}
else{
# DNS Kpnfiguration entsprechend Standort vornehmen
# Nordersted abarbeiten
if($hostlocation -eq "n"){
Write-Host "Konfiguriere DNS und Domain Name von $vmhost fuer NOR01" -ForegroundColor Green
Get-VMHostNetwork -VMHost $vmhost | Set-VMHostNetwork -DomainName "asl.local" -DNSAddress $nor01 , $ham02 -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
}
if($hostlocation -eq "z"){
Write-Host "Konfiguriere DNS und Domain Name von $vmhost fuer HAM02" -ForegroundColor Green
Get-VMHostNetwork -VMHost $vmhost | Set-VMHostNetwork -DomainName "asl.local" -DNSAddress $ham02 , $nor01 -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
}
if($hostlocation -eq "h"){
Write-Host "Konfiguriere DNS und Domain Name von $vmhost fuer HAM01" -ForegroundColor Green
Get-VMHostNetwork -VMHost $vmhost | Set-VMHostNetwork -DomainName "asl.local" -DNSAddress $ham01 , $nor01 -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
}
if($hostlocation -eq "i"){
Write-Host "Konfiguriere DNS und Domain Name von $vmhost fuer IZ" -ForegroundColor Green
Get-VMHostNetwork -VMHost $vmhost | Set-VMHostNetwork -DomainName "asl.local" -DNSAddress $iz , $nor01 -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue
}
#Per SSH mit Host verbidnen und resolv.conf anpassen [Experimentell!]
#Write-Host "Passe resolv.conf an fuer " $vmhost -foregroudColor -Green
#launch the command to append the options to the /etc/resolv.conf file
#echo y | .\plink.exe -v $hostname -l $esxuser -pw $hostpw echo “options timeout:1 attempts:1 rotate >> /etc/resolv.conf”
}
# Schritt Nummer 6 - Storage Adapter Config
Write-Host "Konfiguriere Storage Adapter von $vmhost" -ForegroundColor Green
$esxcli = Get-EsxCli -VMHost $vmhost
$esxcli.storage.nmp.satp.set($null,"VMW_PSP_RR","VMW_SATP_DEFAULT_AA")
$esxcli = Get-EsxCli -VMHost $vmhost
# Schritt Nummer 7 - Firewallregeln NTP VIB file
Write-Host "Konfiguriere Firewallregeln für NTP von $vmhost" -ForegroundColor Green
$esxcli.software.acceptance.set("CommunitySupported")
$esxcli.software.vib.install($null,$null,$null,$null,$null,$null,$null,$null,"http://files.v-front.de/fwenable-ntpd-1.2.0.x86_64.vib")
#enabling firewall rule
$FirewallExceptions = Get-VMHostFirewallException -VMHost $vmhost | where {$_.Name.StartsWith('NTP D')}
$FirewallExceptions | Set-VMHostFirewallException -Enabled $true
#Reboot ESXi Host to make settings permanent
Write-Host "Konfiguration von $vmhost abgeschlossen" -ForegroundColor Green
Write-Host "Achtung! der ESXi Host " $vmhost " wird jetzt neu gestartet! Du hattest Deine Chance....." -ForegroundColor red
Restart-VMHost -VMHost $vmhost -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue -Force
}
#vom vCenter trennen
disconnect-viserver -Server * -Confirm:$false -ErrorAction SilentlyContinue -WarningAction SilentlyContinue -InformationAction SilentlyContinue