New-Folder in datacenter of vm type

by Grzegorz Kulikowski

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 😉

You may also like

13 comments

Steve January 15, 2013 - 3:54 pm

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?

Reply
psvmware January 16, 2013 - 11:46 am

(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’)

Reply
CJ March 25, 2014 - 5:03 pm

Any thoughts on how to make a nested folder rather than create it at the root?

Reply
psvmware March 25, 2014 - 8:01 pm

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’)

Reply
MicaH April 9, 2014 - 5:22 pm

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!

Reply
psvmware April 9, 2014 - 8:58 pm

Nice one MicaH. Well spotted!

Reply
MicaH April 9, 2014 - 9:36 pm

Thanx! Happy to be of service.

Reply
Bruno August 24, 2016 - 7:51 am

You got similar methods for Network and Datastore? I cant seem to fine one that works. Worked fine with GetVMFolder..

Reply
psvmware August 24, 2016 - 4:11 pm

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’)

Reply
Bruno August 25, 2016 - 11:13 am

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?

Reply
Kristian H September 21, 2017 - 10:12 am

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

Reply
Kristian H September 21, 2017 - 10:28 am

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

Reply
psvmware September 21, 2017 - 9:00 pm

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

Reply

Leave a Reply

Chinese (Simplified)EnglishFrenchGermanHindiPolishSpanish
Streaming live on Twitch right now.
CURRENTLY OFFLINE