Hi All,
We are automating the process of Provisioning new host and configure NIC/Switch on the host. Once the host is configured it will be moved to cluster.
Below script is used for same configuration. Is there a way we can make the script generic for all configuration by making values as variable and passing them from external source or anyone have power-shell script the performs the same activity.
Any help is appreciated.
trap
{
write-output $_
exit 1
}
$vmhostip = $args[0]
$ravmhostip = $args[1]
$octet = ($vmhostip.split("."))[-1]
$vmhostuser = "*****"
$vmhostpass = "*******"
$vmkip1 = "X.X.X.$($octet)"
$vmksub1 = "255.255.255.0"
$vmkvlan1 = "17"
$vmkip2 = "X.X.X.10.14.7.$($octet)"
$vmksub2 = "255.255.255.0"
$vmkvlan2 = "16"
$vmkip3 = "X.X.X.$($octet)"
$vmksub3 = "255.255.255.0"
$vmkvlan3 = "100"
$vmkvsw0 = "vSwitch0"
$vmkvsw1 = "vSwitch1"
Add-PSSnapin VMware.VimAutomation.Core
connect-viserver -server $ravmhostip -user $vmhostuser -password $vmhostpass
$vmhost = get-vmhost $ravmhostip
$vswitch0 = Get-VirtualSwitch -vmhost $ravmhostip
Set-VirtualSwitch -VirtualSwitch $vswitch0 -Nic vmnic0,vmnic1
$vswitch1 = New-VirtualSwitch -VMHost $vmhost -Name vSwitch1 -Nic vmnic2,vmnic3
New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name "VM PRD 2" -VLanID 2
New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name "VM PRD 9" -VLanID 9
New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name "VM PRD 13" -VLanID 13
New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name "VM PRD 18" -VLanID 18
New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name "VM PRD 21" -VLanID 21
New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name "VM PRD 40" -VLanID 40
New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name "VM PRD 41" -VLanID 41
New-VirtualPortGroup -VirtualSwitch $vswitch1 -Name "VM PRD 250" -VLanID 250
New-VMHostNetworkAdapter -VMHost $vmhost -PortGroup "Management" -VirtualSwitch $vmkvsw0 -IP $vmkip1 -SubnetMask $vmksub1
New-VMHostNetworkAdapter -VMHost $vmhost -PortGroup "VMkernel" -VirtualSwitch $vmkvsw0 -IP $vmkip2 -SubnetMask $vmksub2 -VMotionEnabled:$true
New-VMHostNetworkAdapter -VMHost $vmhost -PortGroup "VMkernel2" -VirtualSwitch $vmkvsw1 -IP $vmkip3 -SubnetMask $vmksub3 -VMotionEnabled:$true
$vmkpg1 = Get-VirtualPortgroup -Name ‘Management’
$vmkpg2 = Get-VirtualPortgroup -Name ‘VMkernel’
$vmkpg3 = Get-VirtualPortgroup -Name ‘VMkernel2’
Set-VirtualPortGroup -VirtualPortGroup $vmkpg1 -VlanId $vmkvlan1
Set-VirtualPortGroup -VirtualPortGroup $vmkpg2 -VlanId $vmkvlan2
Set-VirtualPortGroup -VirtualPortGroup $vmkpg3 -VlanId $vmkvlan3
Start-VMHostService -HostService ($vmhost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH"} )
Disconnect-VIServer -Force -Confirm:$false
Thanks and Regards
Riyas Hussain A
You could store all the variables in a CSV file, something like this
VMHostIP,RAVMHostIP,VMHostUser,VMHostPass,Subnet1,Netmask1,VlanID2,Subnet2,Netmask2,VlanID3,Subnet3,Netmask3,VlanID3,vSwitch0,vSwitch1
192.168.1.1,192.168.1.2,root,password,192.168.1,255.255.255.0,17,192.168.2,255.255.255.0,16,192.168.3,255.255.255.0,100,vSwitch0,vSwitch1
192.168.10.1,192.168.10.2,root,password,192.168.10,255.255.255.0,17,192.168.20,255.255.255.0,16,192.168.30,255.255.255.0,100,vSwitch0,vSwitch1
In your original script you would only need to change the assignments in the beginning.
For each row in the CSV file the script will be executed.
Inside the ForEach (alias %) loop, the $_ variable holds the current row.
You address the specific values by using the Property name that corresponds with the Column name in the CSV file
trap
{
write-output $_
exit 1
}
Add-PSSnapin VMware.VimAutomation.Core
Import-Csv -Path esxihost.csv -UseCulture | %{
$vmhostip = $_.VMHostIP
$ravmhostip = $_.RAVMHostIP
$octet = ($vmhostip.split("."))[-1]
$vmhostuser = $_.VMHostUser
$vmhostpass = $_.VMHostPass
$vmkip1 = "$($_.Subnet1).$($octet)"
$vmksub1 = $_.Netmask1
$vmkvlan1 = $_.VlanID1
$vmkip2 = "$($_.Subnet2).$($octet)"
$vmksub2 = $_.Netmask2
$vmkvlan2 = $_.VlanID2
$vmkip3 = "$($_.Subnet2).$($octet)"
$vmksub3 = $_.Netmask2
$vmkvlan3 = $_.VlanID2
$vmkvsw0 = $_.vSwitch0
$vmkvsw1 = $_.vSwitch1
connect-viserver -server $ravmhostip -user $vmhostuser -password $vmhostpass
$vmhost = get-vmhost $ravmhostip
# The rest of you original script
Disconnect-VIServer -Force -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You have multiple options for providing the input parameters through an external file.
From a .txt file (Get-Content), from a CSV file (Import-Csv), from a .json file (Get-Content and ConvertFrom-Json), from an XML file (Get-Content and [XML] casting)...and undoubtedly many others.
Take your pick (a .csv is most probably the easiest to start with)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thanks for your valuable input.
My doubt is whether i can use the script as generic one for configuring any host?
Hope you have heard about HP Operations Orchestration which is an automation tool. I will be passing inputs to the script from the tool.
Thanks and Regards
Riyas Hussain A
I heard of HP Operations Orchestration but never used it I'm afraid.
Not sure what is possible with Workflows and how you could eventually plugin your PowerCLI script in there.
Perhaps you should ask your HW contact ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I have Used OO to perform many VMWare activities. Even in below scenario, i am creating cluster and adding host to the cluster using Power-CLI cmdlets. Thing is whether below script can be used as generic one for configuring any host?
I am not completely aware of VMWare Power-CLI so need confirmation for moving forward.
Thanks and Regards
Riyas Hussain A
You could store all the variables in a CSV file, something like this
VMHostIP,RAVMHostIP,VMHostUser,VMHostPass,Subnet1,Netmask1,VlanID2,Subnet2,Netmask2,VlanID3,Subnet3,Netmask3,VlanID3,vSwitch0,vSwitch1
192.168.1.1,192.168.1.2,root,password,192.168.1,255.255.255.0,17,192.168.2,255.255.255.0,16,192.168.3,255.255.255.0,100,vSwitch0,vSwitch1
192.168.10.1,192.168.10.2,root,password,192.168.10,255.255.255.0,17,192.168.20,255.255.255.0,16,192.168.30,255.255.255.0,100,vSwitch0,vSwitch1
In your original script you would only need to change the assignments in the beginning.
For each row in the CSV file the script will be executed.
Inside the ForEach (alias %) loop, the $_ variable holds the current row.
You address the specific values by using the Property name that corresponds with the Column name in the CSV file
trap
{
write-output $_
exit 1
}
Add-PSSnapin VMware.VimAutomation.Core
Import-Csv -Path esxihost.csv -UseCulture | %{
$vmhostip = $_.VMHostIP
$ravmhostip = $_.RAVMHostIP
$octet = ($vmhostip.split("."))[-1]
$vmhostuser = $_.VMHostUser
$vmhostpass = $_.VMHostPass
$vmkip1 = "$($_.Subnet1).$($octet)"
$vmksub1 = $_.Netmask1
$vmkvlan1 = $_.VlanID1
$vmkip2 = "$($_.Subnet2).$($octet)"
$vmksub2 = $_.Netmask2
$vmkvlan2 = $_.VlanID2
$vmkip3 = "$($_.Subnet2).$($octet)"
$vmksub3 = $_.Netmask2
$vmkvlan3 = $_.VlanID2
$vmkvsw0 = $_.vSwitch0
$vmkvsw1 = $_.vSwitch1
connect-viserver -server $ravmhostip -user $vmhostuser -password $vmhostpass
$vmhost = get-vmhost $ravmhostip
# The rest of you original script
Disconnect-VIServer -Force -Confirm:$false
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference