Hi all,
If interested, here is the full script to configure my ESXi 5.1 host. Fell free to modify, adapt or improve ...
I put the licence at the last step because, when it s done, you loose a lot of functionnality :/
###########################################################################
# NAME : ESXi Agencies Post-Configuration Script
# AUTHOR : JLE
# COMMENT :
# VERSION HISTORY : 1.1 07/03/2013 Added SanpIn check + DNS & Routing Tab
###########################################################################
CLS
#### Custom Properties
If ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ){Add-PSSnapIn VMware.VimAutomation.Core}
#### Functions
Function Sleep-Time {
##==============================================
## Pause Program for 15 min - Matt Brown, 2008
##==============================================
$x = 900 ; $length = $x / 100
While($x -gt 0) {
$min = [Int](([String]($x/60)).split('.')[0]) ; $text = " " + $min + " Minutes " + ($x % 60) + " Seconds Left"
Write-Progress "Pausing Script" -status $text -perc ($x/$length) ; Start-Sleep -S 1 ; $x--}}
#### Variables
$ESXi = "IP or Hostname"
$AD = "Domain.com"
$DNS1 = "DNSIP1"
$DNS2 = "DNSIP2"
$User = "root"
$Pass = ""
$NTP1 = "NTP1.Domain.com"
$NTP2 = "NTP2.Domain.com"
$NTP3 = "NTP3.Domain.com"
$MNic = "vmnic0","vmnic1"
$VNic = "vmnic2","vmnic3"
$Vs1N = "LAN Name"
$DS1 = "Datastore Name1"
$DS2 = "Datastore Name2"
$Lic = "Licence Number"
## Start Of The Script
Write-Host "Connecting to $ESXi Server as Root with a Blank Password" ; Connect-VIServer $ESXi -User $User -Password ""
Sleep -Seconds 5
##### Set DNS & Routing Tab
Write-Host "Setting Up DNS & Routing Tab "
Get-VMHost | Get-VMHostNetwork | Set-VMHostNetwork -SearchDomain $AD -DomainName $AD -DnsAddress $DNS1, $DNS2 -IPv6Enabled:$false -Confirm:$false | Out-Null
##### Adding ESXi to AD Domain
Write-Host "Adding $ESXi to $AD ( ADMxxxxxx/password) "
$CredAD = $Host.UI.PromptForCredential("AD Credentials", "Enter a login/password to join $AD", "", "")
$CredUser = $CredAD.GetNetworkCredential().UserName ; $CredPass = $CredAD.GetNetworkCredential().Password
Get-VMHostAuthentication | Set-VMHostAuthentication -Domain $AD -JoinDomain -Confirm:$false -Username $CredUser -Password $CredPass | Out-Null
##### Change root Password
Write-Host "Changing Root Password on $ESXi"
Set-VMHostAccount -UserAccount $User -Password $Pass -Confirm:$false | Out-Null
##### Configure vSwitch0 & vSwitch1
Write-Host "Configuring vSwitch0"
$Vs0 = Get-VirtualSwitch -Name vSwitch0
Write-Host "Adding $MNic to vSwitch0"
Set-VirtualSwitch -VirtualSwitch $Vs0 -Nic $MNic -Confirm:$false | Out-Null
Write-Host "Removing VM Network from vSwitch0"
Get-VirtualPortGroup | Where { $_.Name -like "VM Network"} | Remove-VirtualPortGroup -Confirm:$false | Out-Null
Write-Host "Creating vSwitch1 and Add $VNic"
$Vs1 = New-VirtualSwitch -Name vSwitch1 -nic $VNic -Confirm:$false
Write-Host "Adding $Vs1N to vSwitch1"
New-VirtualPortGroup -VirtualSwitch $Vs1 -Name $Vs1N -Confirm:$false | Out-Null
##### Configure NTP
Write-Host "Configuring NTP Servers"
Write-Host "Adding NTP Servers"
Add-VmHostNtpServer -NtpServer $NTP1,$NTP2,$NTP3 -Confirm:$false
Write-Host "Set NTP Daemon to Start with Host and Starting it"
$NTP = Get-VMHostService | where {$_.Key -eq "ntpd"}
Set-VMHostService -HostService $NTP -Policy "On" -Confirm:$false | Out-Null
Restart-VMHostService -HostService $NTP -Confirm:$false | Out-Null
##### Rename Local Datastore1 & Create Local Datastore2
Write-Host "Rename default local datastore1 to $DS1"
Get-Datastore -Name "datastore1" | Set-Datastore -Name $DS1 | Out-Null
Write-Host "Creating VM Datastore on RAID5, Datastore name will be $DS2 "
New-Datastore -Name $DS2 -Vmfs -FileSystemVersion 5 -Path (Get-ScsiLun -LunType disk | Where {$_.CapacityGB -cgt 300}).CanonicalName | Out-Null
##### Adding AD Groups to Server
Write-Host "Add AD's Domain Groups to ESXi Permission"
Write-Host "Pausing Script for 15 Minutes for AD replication to be Done ... " ; Sleep-Time
Write-Host "Add AD's Domain Admins to Admin Role"
New-VIPermission -Principal "Domain\Domain Admins" -Role "Admin" -Confirm:$false -Propagate:$true -Entity "ha-folder-root" -ErrorAction SilentlyContinue |Out-Null
Write-Host "Add AD's vSphere.ReadOnly to Read Only Role"
New-VIPermission -Principal "Domain\vsphere.readonly" -Role "ReadOnly"-Confirm:$false -Propagate:$true -Entity "ha-folder-root" -ErrorAction SilentlyContinue |Out-Null
##### Licence
Write-Host "Setting ESXi License on Server"
$LicenseManagerView = Get-View -Id "LicenseManager-ha-license-manager" ; $LicenseManagerView.UpdateLicense($Lic,$null) |Out-Null
<#VC PART
$LicMgr = Get-View LicenseManager
$TESXi = Get-VMHost $ESXi | Get-View
$LicKey = New-Object VMware.Vim.LocalLicenseSource
$LicKey.licenseKeys = "xxxxx-xxxxx-xxxxx-xxxxx-xxxxx"
$LicMgr.ConfigureLicenseSource($TESXi.MoRef, $LicKey)#>
## End Of The Script
Write-Host "Move $ESXi from OU Computers to the right OU " -BackgroundColor Red -ForegroundColor Green
Write-Host "Don't forget to Reboot $ESXi before doing anything else !!!! " -BackgroundColor Red -ForegroundColor Green
Write-Host "Disconnecting ESXi Server" ; Disconnect-VIServer $ESXi -Confirm:$false -Force
Ce message a été modifié par: juliennas