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

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

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