New-Folder in datacenter of vm type


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”

(get-view (get-view -ViewType datacenter -Filter @{"name"="myDC"}).VmFolder).CreateFolder("myTopLevelFolder")

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 😉

Advertisements

13 thoughts on “New-Folder in datacenter of vm type

  1. 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?

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

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

  3. 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!

      • 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?

  4. 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s