Hi Guys,
I am trying to create multiple VMs with unique IP address and NetBIOS name for each VMs thrugh a Powershell script. Here is my scenario:
I have 2 VMs on one of my server x.x.x.x
1. Autodeploy-1 – The VM on which I am running the script through vSphere PowerCLI
2. AutoDeployVM – Template I am using for creating other VMs.
#Script
# Virtual Center Details
$server_address = “x.x.x.x”
$username = “xxx”
$password = “xxxxxx”
# Vm Details
$destination_host = “x.x.x.x”
$template_name = “AutodeployVM”
$datastore_name = “dataStore1 (9)”
$customization = "customfile"
# Name the VMs in this array
$array = “VPS-Test1″,”VPS-Test2″
$iparray = “x.x.x.1″, “x.x.x.2″
$a= 0
Connect-VIServer -Server $server_address -Protocol https -User $username -Password $password
foreach ($vm in $array)
{
Get-OSCustomizationSpec $customization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IParray[$a] -SubnetMask z.z.0.0 -DefaultGateway y.y.y.y -Dns x.x.x.35,x.x.x.36
$vm=New-VM -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -OSCustomizationSpec $customization
-Confirm:$false $a = $a + 1
}
I have placed the customization file (customfile) in the same folder where the script is. Below is the content of my customfile:
New-OSCustomizationSpec -Name Spec -OSType Windows -FullName Administrator -AdminPassword ****** -Domain abcd.com -DomainUsername abc -DomainPassword ********
I am getting following errors:
1. Set-OSCustomizationSpec : A parameter cannot be found that matches parameter name 'IpMode'.
2. New-VM Could not find Template with name 'AutodeployVM'.
3. New-VM Template parameter: Could not find any of the objects specified by name.
Can you guys help me with this. Am I missing anything. I am very new to this thing. Appreciate your help.
Thanks,
Kiala
The New-OSCustomization cmdlet creates the customization specs in the vCenter database, not in a file.
You have to create that customization spec by executing
New-OSCustomizationSpec -Name Spec -OSType Windows -FullName Administrator -AdminPassword ****** -Domain abcd.com -DomainUsername abc -DomainPassword ********
from the PowerCLI prompt.
Once that entry is created, you can run your deployment script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The New-OSCustomization cmdlet creates the customization specs in the vCenter database, not in a file.
You have to create that customization spec by executing
New-OSCustomizationSpec -Name Spec -OSType Windows -FullName Administrator -AdminPassword ****** -Domain abcd.com -DomainUsername abc -DomainPassword ********
from the PowerCLI prompt.
Once that entry is created, you can run your deployment script.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks LucD for your reply.
Is it necessary to run this cmdlet. I mean what if I don't want any customization. I want the new VMs to be exactly the same as the template mentioned and using the same resources. Can I remove the customization part from the script and just run directly without it?
Appreciate your help.
Yes, you can leave out the customisation.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
I tried running the below script in PowerCLI prompt, but it gave me an error "This functionality is only available when connected to VMware vCenter Server."
New-OSCustomizationSpec -Name Spec -OSType Windows -FullName Administrator -AdminPassword ****** -Domain abcd.com -DomainUsername abc -DomainPassword ********
Although before running the above script, I was connected to my vCenter server by Connect-VIServer command
I tried running the script removing the customfile, and it still gave me the same errors. Below is the script without customfile.
# Virtual Center Details
$server_address = “x.x.x.x”
$username = “xxx”
$password = “xxxxxx”
# Vm Details
$destination_host = “x.x.x.x”
$template_name = “AutodeployVM”
$datastore_name = “dataStore1 (9)”
# Name the VMs in this array
$array = “VPS-Test1″,”VPS-Test2″
$iparray = “x.x.x.1″, “x.x.x.2″
$a= 0
Connect-VIServer -Server $server_address -Protocol https -User $username -Password $password
foreach ($vm in $array)
{
Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IParray[$a] -SubnetMask z.z.0.0 -DefaultGateway y.y.y.y -Dns x.x.x.35,x.x.x.36
$vm=New-VM -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -Confirm:$false $a = $a + 1
}
It still gave me the same errors:
1. Set-OSCustomizationSpec : A parameter cannot be found that matches parameter name 'IpMode'.
2. New-VM Could not find Template with name 'AutodeployVM'.
3. New-VM Template parameter: Could not find any of the objects specified by name.
Could it be that you're connected to multiple vSphere servers ?
Check the contents of
$defaultVIServers
The Get-OSCustomizationNicMapping and the Set-OSCustomizationNicMapping are only usable in combination with the customisation spec.
See the examples for those 2 cmdlets.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
You were correct. I was connected to multiple servers. I restarted the prompt and got connected to vCenter server. Also I was able to run that script and was successfully able to create the customization file in the vCenter database.
After that I tried running my deployment script including the customfile and it gave multiple errors:
1. Set-OSCustomizationSpec : A parameter cannot be found that matches parameter name 'IpMode'.
2. New-VM The specified parameter 'VMHost' expects a single value, but your name criteria 'x.x.x.x' corresponds to multiple values.
3. New-VM The specified parameter 'Datastore' expects a single value, but your name criteria 'datastore1 (9)' corresponds to multiple values.
4. New-VM Datastore parameter: Could not find any of the objects specified by name.
The Server with IP "x.x.x.x" is already added to vCenter server and it also has a datastore named "datastore1 (9)"
Don't understand what's causing the error.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Great news LucD, with all your help we able to rectify the errors and was successfully able to create VMs.
Unfortunately, as I expected it didn't gave the VMs unique IP address and unique NetBIOS name. It took the defualt from the template gave the same to all the VMs.
Any idea what could be wrong, why it didn't took the parameteres we mentioned in the script for NIC settings and instead it took from the template?
Appreciate your help.
Not sure how the script that you use now looks but I think there is a problem with the way you used the Set-OSCustomizationNicMapping cmdlet.
This cmdlet works on a subset of settings in the customisation specs.
For that reason you always have to start from the customisation spec.
Like this
Get-OSCustomizationSpec -Name Spec | Get-OSCustomizationNicMapping | `
Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IParray[$a] -SubnetMask z.z.0.0 -DefaultGateway y.y.y.y -Dns x.x.x.35,x.x.x.36
This will update the Nic specific settings in the customisation spec called Spec.
In your New-VM you refer to this Spec.
$vm=New-VM -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -OSCustomizationSpec (GetOSCustomizationSpec -Name Spec)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hey...I added OSCustomizationSpec -Name Spec as you said in the script below
$vm=New-VM -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -OSCustomizationSpec -OSCustomizationSpec -Name Spec
It gave me some weird error and didn't even created the VMs
New-VM The operation for the entity VirtualMachine-vm failed with the following message: "Customization of the guest operating system 'winNetDatacenter64Guest' is not supported in this configuration. Microsoft Vista (TM) and Linux guests with Logical Volume Manager are supported only for recent ESX host and VMware Tools versions. Refer to vCenter documentation for supported configurations."
At C:\scripts\vm.ps1:23 char:17
+ $vm=New-VM <<<< -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -OSCustomizationSpec Spec
I am using Windows Server 2003, Datacenter Edition (64-bit). Host is on ESXi 4.0, also VMware tools looks updated.
Are you by any chance using vCenter 3.5 or older ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry for the late reply. I didn't had internet access during the weekend so couldn't reply.
I am using vCenter 4.0. Don't understand why but strangely I again started getting the prvious error,
"New-VM : A parameter cannot be found that matches parameter name '0'. At C:\scripts\vm.ps1:24 char:17 + $vm=New-VM <<<< -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -OSCustomizationSpec $customization -Confirm:$false $a = $a + 1"
Customization file "test" has been successfully created in the vCenter database. Below if my latest script I am running,
# Virtual Center Details
$server_address = “x.x.x.x”
$username = “xxx”
$password = “xxxxxx”
# Vm Details
$destination_host = “x.x.x.x”
$customization = "test"
Connect-VIServer -Server $server_address -Protocol https -User $username -Password $password
foreach ($vm in $array)
{
Get-OSCustomizationSpec $customization | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IParray[$a] -SubnetMask z.z.0.0 -DefaultGateway y.y.y.y -Dns x.x.x.35,x.x.x.36
$vm=New-VM -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -OSCustomizationSpec $customization
-Confirm:$false $a = $a + 1
}
It looks as if the Host parameter has a problem.
Is $destination_host perhaps empty ?
Btw the $a = $a + 1 at the end belongs on a new line.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, putting $a = $a + 1 to new line solved that error Now again back to the main error
"New-VM : 3/21/2011 9:35:33 AM New-VM The operation for the entity VirtualMachine-vm-4861 failed with the following message: "Customization of the guest operating system 'winNetDatacenter64Guest' is not supported in this configuration. Microsoft Vista (TM) and Linux guests with Logical Volume Manager are supported only for recent ESX host and VMware Tools versions. Refer to vCenter documentation for supported configurations."
At C:\scripts\vm.ps1:24 char:17 + $vm=New-VM <<<< -Name $vm -Template $template_name -Host $destination_host -Datastore $datastore_name -OSCustomizationSpec $customization -Confirm:$false"
I believe having vCenter 4.0 shouldn't give this error, right?
Do you have Update 1 installed for vCenter 4.0 ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I don't think so. See the screenshot below for the vCenter version.
There are many other hosts on it which has live VMs which clients are actively using. Can we install the update without any downtime or affecting anything?
Normally all guests keep running when you stop/start vCenter.
You will of course temporarily loose the vCenter functions.
But you have to decide yourself if that is acceptable in your environment, I'm afraid.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok. I think I should prefer doing it during maitenance period in couple of weeks. I will update you after I run the script on the updated version. Thanks for all your help.