Welcome‎ > ‎Scripting‎ > ‎

Netwerk configuratie ESX 5 PowerCli updated

posted Feb 3, 2012, 3:04 PM by Jurien Bosman   [ updated Feb 4, 2012, 12:52 PM ]
cls
#variabelen
$hostname = Read-Host "Type de naam in van de te configureren ESX-host"
$ntpserver = "ntp.domain.local.com"
$esxdomein = "domain.local"

$gethostnameip = Import-Csv c:\script\vmware\hosts.csv | where-object {$_.hostname -eq $hostname}
$iscsiip = Import-Csv c:\script\vmware\iscsi_ip.csv | where-object {$_.hostname -eq $hostname}
$HostnameIP = $gethostnameip.ipnummer
$vmotionip = $gethostnameip.vmotionip
$vmkip1 = $iscsiip.iscsi1
$vmkip2 = $iscsiip.iscsi2
$vmkip3 = $iscsiip.iscsi3
$vmkip4 = $iscsiip.iscsi4

#vaste waarden

#connecteren met de te configureren esx-host
Connect-VIServer $HostnameIP -User root -Password xxxxxxxxx

#hostname en domein instellen
$esxcli = Get-EsxCli
$esxcli.system.hostname.set($esxdomein,$null,$hostname)

#Standaard aangemaakte portgroup VM Network verwijderen
$DefaultPG = Get-VirtualPortgroup -Name 'VM Network' -ErrorAction SilentlyContinue
Remove-VirtualPortGroup -VirtualPortGroup $DefaultPG -Confirm:$false -ErrorAction SilentlyContinue

#Vmnic toevoegen aan vSwitch0
Set-VirtualSwitch -VirtualSwitch vSwitch0 -Nic vmnic0,vmnic6 -Confirm:$false

#vMotion toevoegen aan vSwitch0
New-VirtualPortGroup -Name vMotion -VirtualSwitch vSwitch0 -VLanId 240
New-VMHostNetworkAdapter -VirtualSwitch vSwitch0 -PortGroup vMotion -IP $vmotionip -VMotionEnabled:$true -SubnetMask 255.255.255.0 -ErrorAction SilentlyContinue


## Definiëren vSwitch1
New-VirtualSwitch -Name vSwitch1 -nic vmnic1,vmnic2 -NumPorts 256 -ErrorAction SilentlyContinue
# Definiëren NicTeamingPolicy
$policy = Get-VirtualSwitch -Name vSwitch1 | Get-NicTeamingPolicy
$policy | Set-NicTeamingPolicy -LoadBalancingPolicy LoadBalanceIP
# Definiëren Portgroepen
New-VirtualPortGroup -Name ServerLan -VirtualSwitch vSwitch1
New-VirtualPortGroup -Name "VLAN50" -VirtualSwitch vSwitch1 -VLanId 50
New-VirtualPortGroup -Name "VLAN100" -VirtualSwitch vSwitch1 -VLanId 100
New-VirtualPortGroup -Name "VLAN150" -VirtualSwitch vSwitch1 -VLanId 150
New-VirtualPortGroup -Name "VLAN200" -VirtualSwitch vSwitch1 -VLanId 200

# Definiëren vSwitch2
New-VirtualSwitch -Name vSwitch2 -nic vmnic3,vmnic4,vmnic7,vmnic8 -NumPorts 64 -ErrorAction SilentlyContinue

# Definiëren NicTeamingPolicy
$policy = Get-VirtualSwitch -Name vSwitch2 | Get-NicTeamingPolicy
$policy | Set-NicTeamingPolicy -LoadBalancingPolicy LoadBalanceIP

# Definiëren Portgroepen
#iSCSI1
New-VirtualPortGroup -Name iSCSI1 -VirtualSwitch vSwitch2 -VLanId 250 -ErrorAction SilentlyContinue
$policy = Get-VirtualSwitch -Name vSwitch2 | Get-VirtualPortGroup -Name iSCSI1 | Get-NicTeamingPolicy
$policy | Set-NicTeamingPolicy -MakeNicUnused vmnic7,vmnic8,vmnic4 -MakeNicActive vmnic3
#iSCSI2
New-VirtualPortGroup -Name iSCSI2 -VirtualSwitch vSwitch2 -VLanId 250 -ErrorAction SilentlyContinue
$policy = Get-VirtualSwitch -Name vSwitch2 | Get-VirtualPortGroup -Name iSCSI2 | Get-NicTeamingPolicy
$policy | Set-NicTeamingPolicy -MakeNicUnused vmnic7,vmnic8,vmnic3 -MakeNicActive vmnic4
#iSCSI3
New-VirtualPortGroup -Name iSCSI3 -VirtualSwitch vSwitch2 -VLanId 250 -ErrorAction SilentlyContinue
$policy = Get-VirtualSwitch -Name vSwitch2 | Get-VirtualPortGroup -Name iSCSI3 | Get-NicTeamingPolicy
$policy | Set-NicTeamingPolicy -MakeNicUnused vmnic4,vmnic8,vmnic3 -MakeNicActive vmnic7
#iSCSI4
New-VirtualPortGroup -Name iSCSI4 -VirtualSwitch vSwitch2 -VLanId 250 -ErrorAction SilentlyContinue
$policy = Get-VirtualSwitch -Name vSwitch2 | Get-VirtualPortGroup -Name iSCSI4 | Get-NicTeamingPolicy
$policy | Set-NicTeamingPolicy -MakeNicUnused vmnic7,vmnic4,vmnic3 -MakeNicActive vmnic8

#Omzetten vm network naar vmk
$vs = Get-VirtualSwitch -Name "vSwitch2" -ErrorAction SilentlyContinue
New-VMHostNetworkAdapter -VirtualSwitch $vs -PortGroup iSCSI1 -IP $vmkip1 -SubnetMask 255.255.255.0 -ErrorAction SilentlyContinue
New-VMHostNetworkAdapter -VirtualSwitch $vs -PortGroup iSCSI2 -IP $vmkip2 -SubnetMask 255.255.255.0 -ErrorAction SilentlyContinue
New-VMHostNetworkAdapter -VirtualSwitch $vs -PortGroup iSCSI3 -IP $vmkip3 -SubnetMask 255.255.255.0 -ErrorAction SilentlyContinue
New-VMHostNetworkAdapter -VirtualSwitch $vs -PortGroup iSCSI4 -IP $vmkip4 -SubnetMask 255.255.255.0 -ErrorAction SilentlyContinue

# enable iSCSI adapter
Get-VMHostStorage -VMHost $HostnameIP | Set-VMHostStorage -SoftwareIScsiEnabled $True -ErrorAction SilentlyContinue
#Lees het HBA nummer van de iSCSI Software Adapter
$HBANumber = Get-VMHostHba -VMHost $HostnameIP -Type iSCSI | %{$_.Device}

#Laat PowerCLI gebruik maken van esxcli commandos
$esxcli = Get-EsxCli

#vKernel Nummers iSCSI
$vmk1nummer = Get-VMHostNetworkAdapter | Where-object { $_.IP -match $vmkip1 } | %{ $_.Name }
$vmk2nummer = Get-VMHostNetworkAdapter | Where-object { $_.IP -match $vmkip2 } | %{ $_.Name }
$vmk3nummer = Get-VMHostNetworkAdapter | Where-object { $_.IP -match $vmkip3 } | %{ $_.Name }
$vmk4nummer = Get-VMHostNetworkAdapter | Where-object { $_.IP -match $vmkip4 } | %{ $_.Name }

#Binds VMKernel ports aan de iSCSI Software Adapter HBA
$EsxCli.iscsi.networkportal.add($HbANumber,$null,$vmk1nummer)
$EsxCli.iscsi.networkportal.add($HbANumber,$null,$vmk2nummer)
$EsxCli.iscsi.networkportal.add($HbANumber,$null,$vmk3nummer)
$EsxCli.iscsi.networkportal.add($HbANumber,$null,$vmk4nummer)

#timeserver configuratie
$arrNTPList = Get-VMHostNtpServer -VMHost $HostnameIP
Remove-VMHostNtpServer -VMHost $HostnameIP -NtpServer $arrNTPList -Confirm:$false -ErrorAction SilentlyContinue
Add-VMHostNtpServer -VMHost $HostnameIP -NtpServer $ntpserver -Confirm:$false
Set-VMHostService -HostService (Get-VMHostservice -VMHost (Get-VMHost $HostnameIP) | Where-Object {$_.key -eq "ntpd"}) -Policy "Automatic"
Get-VmhostFirewallException -VMHost $HostnameIP -Name "NTP Client" | Set-VMHostFirewallException -enabled:$true
$ntpd = Get-VMHostService -VMHost $HostnameIP | where {$_.Key -eq 'ntpd'}
Restart-VMHostService $ntpd -Confirm:$false

#reboot ESX host
Get-VMHost $HostnameIP |%{Get-View $_.ID} | %{$_.RebootHost_Task($TRUE)}

#disconnect
Disconnect-VIServer -Confirm:false

Comments