I was trying to create a vm folder in Datacenter. This can not be achieved using new-folder directly without specifying proper location(as per MicaH comment) , because as per get-help from new-folder, this will result in creating a “yellow” host and cluster folder. In order to create a vm folder i had to use method CreateFolder(string)
Lets say i have a datacenter with name: “myDC” in which i want to create a folder “myTopLevelFolder”
[sourcecode language=”powershell”]
(get-view (get-view -ViewType datacenter -Filter @{"name"="myDC"}).VmFolder).CreateFolder("myTopLevelFolder")
[/sourcecode]
get-view -ViewType datacenter -Filter @{“name”=”myDC”}
We want to select only datacenter with name ‘myDC’
Then, we take the vmfolder from this datacenter
(get-view -ViewType datacenter -Filter @{“name”=”myDC”}).VmFolder
Then we create view from this:
get-view (get-view -ViewType datacenter -Filter @{“name”=”myDC”}).VmFolder
After that we can invoke method from this object using CreateFolder
It’s done 😉
14 comments
I just tried this command and am getting the following error:
Get-View : Cannot validate argument on parameter ‘VIObject’. The argument is null or empty. Supply an argument that is
not null or empty and then try the command again.
Thoughts?
(get-view (get-datacenter ‘myDC’).ExtensionData.VmFolder).CreateFolder(‘myTEST’) can you try to do it in this exact way ? just repleace the ‘mydc’ with your existing datacenter. I think something got wrong while getting the vmfolder from the datacenter object. you can try to troubleshoot it like this
1
$dc=get-datacenter ‘my_dc’
2
$dcvmfolder=$dc.extensiondata.vmfolder
3
$folderv=get-view $dcvmfolder
4
$folderv.createFolder(‘test_one’)
Any thoughts on how to make a nested folder rather than create it at the root?
In the same way as described. Take a get-view object from a Folder X, then use the method ‘CreateFolder’ with argument of name of the folder that should be created.
$somefolder=get-view -ViewType folder -Filter @{“nameâ€=â€SomeExistingFolderâ€}
$somefolder.CreateFolder(‘A_new_folder’)
Actually, you can use New-Folder for this! The trick is to get the datacenter object and use the GetVMFolder method. Like so:
$Datacenter = Get-Datacenter -Name ‘myDC’
New-Folder -Name ‘myTopLevelFolder’ -Location $Datacenter.GetVmFolder()
Works like a charm in my environment!
Nice one MicaH. Well spotted!
Thanx! Happy to be of service.
You got similar methods for Network and Datastore? I cant seem to fine one that works. Worked fine with GetVMFolder..
Hey, i am on holidays now, so i can’t test it but, it should be something like this:
1
$dc=get-datacenter ‘my_dc’
2
$dcvmfolder=$dc.extensiondata.networkFolder #<- this will point to network folders
3
$folderv=get-view $dcvmfolder
4
$folderv.createFolder(‘test_one’)
for ds:
1
$dc=get-datacenter ‘my_dc’
2
$dcvmfolder=$dc.extensiondata.datastoreFolder #<- this will point to datastore folders
3
$folderv=get-view $dcvmfolder
4
$folderv.createFolder(‘test_one’)
Was hoping for an method like MicaH explained here! Works perfectly on my side, but cant figure out how to put the folders in datastore and network. Cant get your way to work on my side.
$Datacenter = Get-Datacenter -Name ‘myDC’
New-Folder -Name ‘myTopLevelFolder’ -Location $Datacenter.GetVmFolder()
This ons is short and not complicated. Is there a way of getting Network and Datastore folder like he did with the VM folder here?
This is also a possibility:
$Location = Get-Folder -Location -Name vm
New-Folder -Name -Location $Location
Even though it is not visible, the top folder for all VM folders is “vm”.
I have not tested, but if you only have one datacenter, this would probably work:
New-Folder -Name -Location vm
I forgot some info in my last post…
Multiple datacenters
$Location = Get-Folder -Location MyDataCenterName -Name vm
New-Folder -Name MyFolderName -Location $Location
One datacenter:
New-Folder -Name MyFolderName -Location vm
Hi Kristian, you are correct in this. Datacenter object contains a folder named ‘vm’, in which it keeps the VMs. You also pointed correctly that if you have 1 dc it’s ok, and while having even more dcs, it will work while specifing the DC folder first. As for principal this is ‘ok’. Why ‘ ‘ ? Because you never know if there are folders called ‘vm’ in that datacenter 😉 If i will create a folder in my datacenter , in the vm view, called ‘vm’, and going further, i can create subfolders called ‘vm’ etc etc etc, your command will fail due to the fact that location will be an array and not a single variable pointing at the ‘vm’ folder location .Hence the approach ‘(get-view -ViewType datacenter -Filter @{“nameâ€=â€myDCâ€}).VmFolder’ , which points to the ‘vm’ folder of the datacenter structure, completely ignoring fact if there are any other ‘vm’ folders inside the ‘vm’ folder 😉 Sorry i confused you.
You can give it a try, and create some folders called ‘vm’ , in your DC, and try it out.
Many thanks for your reply!
Greg
I don’t even know how you guys figure this out. But it worked for me!