Hi,
I am new to PowerCLI and exited to see the way it works.
i have about 20 VMs to be deplyed from template and needs to configure IP/netmask/gateway on each vm, they are all on rhel 5.
i have the script for the deploying vm's but not to configure the IPs.
Connect-VIServer VI-server
$vmlist = Import-CSV C:\saal.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
$data = $item.datastore
$Randomhost = Get-Cluster hopub1 | Get-VMHost | get-random
New-VM -Name $vmname -Template linux_64bit -VMHost $Randomhost -Datastore $data -Location Elm
}
could some one help me please.
Thanks in advance,
Govinda.
The first example looks something like this
Connect-VIServer VI-server
$vmlist = Import-CSV C:\saal.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
$data = $item.datastore
$Randomhost = Get-Cluster hopub1 | Get-VMHost | get-random
New-VM -Name $vmname -Template linux_64bit -VMHost $Randomhost -Datastore $data -Location Elm
sleep 300
Get-VM $vmname | Get-VMGuestNetworkInterface | Set-VMGuestNetworkInterface -Ip 192.168.1.1 -IPPolicy Static -GuestUser MyAccount -GuestPassword pswd
}
Notice the 'sleep' command, after the New-VM, the new guest will be cloned and then the guest OS will reboot to execute the sysprep utility.
It's hard to determine when that finishes, hence the sleep command. The duration can be shorter or longer, depending on your environment.
The other option is a bit easier, but you have to create an OS customization spec in advance.
Connect-VIServer VI-server
$vmlist = Import-CSV C:\saal.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
$ipAddr = $item.ipaddress
$data = $item.datastore
$Randomhost = Get-Cluster hopub1 | Get-VMHost | get-random
$tempCust = Get-OSCustomizationSpec -Name MyCustSpec | `
Get-OSCustomizationNicMapping | `
Set-OSCustomizationNicMapping -IpAddress $ipAddr -IpMode UseStaticIp `
-SubnetMask 255.255.255.0 -DefaultGateway 192.168.1.254
New-VM -Name $vmname -Template linux_64bit -OSCustomizationSpec $tempCust -VMHost $Randomhost`
-Datastore $data -Location Elm
}
The script creates a temporary copy of the spec and changes the IP settings.
Then the new guest is created with this OS customization spec.
Note that I assumed that you will store the IP address in the CSV file.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Since you are running Red Hat 5 as the guest OS, one option is to use the Set-VMGuestNetworkInterface cmdlet after the guest is created.
You can also customise your OSCustomizationSpec with the Set-OSCustomizationNicMapping cmdlet before starting the cloning process.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
Thank you for the quick response, could you provide me any examples how we i can use these in my script?
The first example looks something like this
Connect-VIServer VI-server
$vmlist = Import-CSV C:\saal.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
$data = $item.datastore
$Randomhost = Get-Cluster hopub1 | Get-VMHost | get-random
New-VM -Name $vmname -Template linux_64bit -VMHost $Randomhost -Datastore $data -Location Elm
sleep 300
Get-VM $vmname | Get-VMGuestNetworkInterface | Set-VMGuestNetworkInterface -Ip 192.168.1.1 -IPPolicy Static -GuestUser MyAccount -GuestPassword pswd
}
Notice the 'sleep' command, after the New-VM, the new guest will be cloned and then the guest OS will reboot to execute the sysprep utility.
It's hard to determine when that finishes, hence the sleep command. The duration can be shorter or longer, depending on your environment.
The other option is a bit easier, but you have to create an OS customization spec in advance.
Connect-VIServer VI-server
$vmlist = Import-CSV C:\saal.csv
foreach ($item in $vmlist) {
$vmname = $item.vmname
$ipAddr = $item.ipaddress
$data = $item.datastore
$Randomhost = Get-Cluster hopub1 | Get-VMHost | get-random
$tempCust = Get-OSCustomizationSpec -Name MyCustSpec | `
Get-OSCustomizationNicMapping | `
Set-OSCustomizationNicMapping -IpAddress $ipAddr -IpMode UseStaticIp `
-SubnetMask 255.255.255.0 -DefaultGateway 192.168.1.254
New-VM -Name $vmname -Template linux_64bit -OSCustomizationSpec $tempCust -VMHost $Randomhost`
-Datastore $data -Location Elm
}
The script creates a temporary copy of the spec and changes the IP settings.
Then the new guest is created with this OS customization spec.
Note that I assumed that you will store the IP address in the CSV file.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot LucD, that was really helpful, i will complete the script and test the same, will post the results.
hi,
I am getting the following error : i dont have the OS customisation so i used the first option.
looks like Set-VMGuestNetworkInterface does not take piped input,
Set-VMGuestNetworkInterface : The input object cannot be bound to any parameter
s for the command either because the command does not take pipeline input or th
e input and its properties do not match any of the parameters that take pipelin
e input.
At C:\setip.ps1:15 char:46
+ Get-VM $vmname | Set-VMGuestNetworkInterface <<<< -Ip $ipadd -IPPolicy S
tatic -Netmask $netmask -Gateway $gw -GuestUser root -GuestPassword "H0t$d0g,"
+ CategoryInfo : InvalidArgument: (sappixtrn201:PSObject) [Set-VM
GuestNetworkInterface], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.
Cmdlets.Commands.SetVmGuestNetworkInterface
Set-VMGuestNetworkInterface : The input object cannot be bound to any parameter
s for the command either because the command does not take pipeline input or th
e input and its properties do not match any of the parameters that take pipelin
e input.
At C:\setip.ps1:15 char:46
+ Get-VM $vmname | Set-VMGuestNetworkInterface <<<< -Ip $ipadd -IPPolicy S
tatic -Netmask $netmask -Gateway $gw -GuestUser root -GuestPassword "H0t$d0g,"
+ CategoryInfo : InvalidArgument: (sapscmtrn201:PSObject) [Set-VM
GuestNetworkInterface], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.
Cmdlets.Commands.SetVmGuestNetworkInterface
My fault, I forgot the Get-VMGuestNetworkInterface cmdlet you have to do first.
I corrected the code sample.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i m getting following error.
Get-VMGuestNetworkInterface : 7/18/2011 10:26:04 PM Get-VMGuestNetworkInterf
ace While performing operation 'Connect to host service 'https://duavcsp
rd01/sdk' at port 902' the following error occured: 'Failed to resolve host'
At C:\setip.ps1:18 char:46
+ Get-VM $vmname | Get-VMGuestNetworkInterface <<<< | Set-VMGuestNetworkIn
terface -Ip $ipadd -IPPolicy Static -Netmask $netmask -Gateway $gw -GuestUser r
oot -GuestPassword "H0t$d0g,"
+ CategoryInfo : OperationStopped: (:) [Get-VMGuestNetworkInterfa
ce], VimException
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_VixWaitForJob_VixErr
or,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVmGuestNetworkInterface
I saw that error before.
Did you do the Connect-VIServer with a short name or the FQDN ?
I had this on some occasions when I used the short name.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference