Hi All
I'm very new to power CLI scripting and in recent days I've been reading a lot of help in order to automate creation of a new VM based on the VM template. Scripting that is not that hard, even locating new VM on a particular datastore and in a particular resource pool is not hard. To do that I've come up with this very basic script/command:
"New-VM -ResourcePool <resource_pool> -Location <folder_name> -Datastore <datastore_name> -Name <VM_name> -Template <template_name>"
However, I've come across a couple of problems that I can't seem to find any information in help nor in forums. Problem with the above command is that VM will be created in appropriate resource pool and folder as long as they are unique, but if there are multiple instances of resource pool or folder with the same name than the script spits a dummy.
So for example if I want to create a new VM in following location Data_Centre => RootFolder1 => SubFolder1 => SubFolder2 and the name of SubFolder2 is unique than no issues, the VM is created in that location. However if the SubFolder2 exists in Data_Centre => RootFolder2 => SubFolder1 as well than the VM is created in the root folder of the template location. For example if the template is in "DC => Folder1 => Subfolder1 => Subfolder2" the VM will be created in Folder1
Any suggestion on how to ensure that the VM is created in desired location/folder as well as resource pool? I see there are some really smart guys here and I suspect addressing this issue wouldn't be a problem at all. Any assistance would be much appreciated.
Cheers
It will be a matter of following the righ path.
You can use the Location parameter to get the exact folder you want. For example
$dc = Get-Datacenter -Name DC1
$folder = Get-Folder -Name Folder1 -Location $dc
$subfolder = Get-Folder -Name Folder2 -Location $Folder1
or
$dc = Get-Datacenter -Name DC1
$subfolder = Get-Folder -Name Folder2 -Location $dc
You will have to find a "location" in the path to your destination folder where it will be unique.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It will be a matter of following the righ path.
You can use the Location parameter to get the exact folder you want. For example
$dc = Get-Datacenter -Name DC1
$folder = Get-Folder -Name Folder1 -Location $dc
$subfolder = Get-Folder -Name Folder2 -Location $Folder1
or
$dc = Get-Datacenter -Name DC1
$subfolder = Get-Folder -Name Folder2 -Location $dc
You will have to find a "location" in the path to your destination folder where it will be unique.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the reply LucD and my hat off to you, you are a damn genius!
I used the first example you provided, as it's easier for me to understand since I'm so new to scripting, and it worked a charm. I did have to make one minor change tho, see below
$dc = Get-Datacenter -Name DC1
$folder = Get-Folder -Name Folder1 -Location $dc
$subfolder = Get-Folder -Name Folder2 -Location $folder
To get to for example third level subfolder I ended up using following in the script
$dc = Get-Datacenter -Name DC1
$folder = Get-Folder -Name Folder1 -Location $dc
$subfolder = Get-Folder -Name Folder2 -Location $folder
$subfolder = Get-Folder -Name Folder3 -Location $subfolder
This allowed me to get to, for example, to a folder called "Test_Servers" located in DC1 => Server_2008 => Finance and not to "Test_Servers" located in "DC1 => Server_2008 => Accounting
I suspect the same would work for resource pools so the code would be something like this:
$dc = Get-Datacenter -Name DC1
$rpool = Get-Resourcepool -Name RPool1 - Location $dc
$rpool1 = Get-Resourcepool -Name RPool2 - Location $rpool
$rpool2 = Get-Resourcepool -Name RPool3 - Locaiton $rpool1
Thank you so much LucD, you are a lifesaver. 200+ VMs that I've to deploy by end of the week using this code will help me do so in matter of minutes not days.
Cheers
Same folder name can reside on different branches of an inventory tree structure. To ensure working
on the right folder, I normally find out where they are different, then get to it by:
$targetFolder = get-folder -name <my desired folder name> | where {$_.parent.name -eq "xxx" -and $_.parent.parent.name -eq "yyy" -and ...}
I would like to pose a question along the same lines as this original question hopefully answering both questions.
So I have two datacenters with a template in one that I want to clone to the other. The issue is I use the New-Template command and for the -Location I put the DC name or a variable such as the below $dc with identifies the same DC name. If I specify the folder I want it to go to instead of the DC with -Location it states it can only copy a template on the same DC. If I just put the DC in the location it copies to the appropriate Datacenter but drops the clone in the root of the DC. I want to be able to use the New-Template command to not only clone the template to a new template on a different DC but in the same command I want to tell it to go to the appropriate folder in that datacenter. Sorry if this is confusing.
Manually in vSphere I can easily accomplish this through the interface, its through CLI that it has been tricky. Trying to automate the process basically this is the only hurdle I am running into. I can Move-item easily enough I would just like to wrap it all up if possible in the New-Template command..
My command looks basically like this:
Template is in DC2
$dc = Get-Datacenter DC1
$folder = Get-Folder -Name "~INT dc2 to dc1 clone staging" -Location $dc
$Temp = "TemplateName"
$ds = "DS Name"
$vmhost = "VM Host Name"
New-Template -Template $Temp -Name "New Template Name" -Location $folder -VMHost $vmhost -Datastore $ds -DiskStorageFormat Thin -Confirm:$false
I think it's just a redefined variable statement that will let the New-Template command know that the template needs to go to the DC1 as well as go to the Folder in DC1. Just not sure how to contruct it.
Afaik that is not possible in 1 cmdlet.
But that is not a PowerCLI limitation, but is in fact a limitation of the underlying CloneVM_Task method.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So would piping a follow-up Move-Item command be feasible or would it not work well following up the New-Template command since it is a CloneVM_task?
The Move-Item cmdlet will not vMotion the template. In fact Move-Item is not a PowerCLI cmdlet but a native PowerShell cmdlet.
And the Move-Template cmdlet will not vMotion the template across datacenters either I'm afraid, at least not in vSphere 5.x
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference