VMware Cloud Community
frozenbubble
Contributor
Contributor

new-folder function

I'm a bit confused. I try to create a new folder on one of my datastores. But this always stops with an exception. Anyone can provide me with a solution on how you create a folder? I even tried with the Datastore Provider. What says that this allows creation of new folders.

Tags (2)
0 Kudos
3 Replies
LucD
Leadership
Leadership

The New-Folder cmdlet can be used to create a new folder in your vSPhere or VI not on your datastores.

For creating folders on a datastore, allthough I don't see why you would want to do that, you have to fall back on the APIs.

With the MakeDirectory method it can be done.

This is a sample script that uses this method

$dsName = <datastore-name>
$dirPath = <folder-path>          # Can be "Test" or something like "Test/SubTest" 

$fileMgr = Get-View FileManager 
$dc = Get-Datacenter -VMHost (Get-VMHost (Get-View (Get-Datastore -Name $dsName | Get-View).Host[0].Key).Name) | Get-View
$fileMgr.MakeDirectory((" " + $dirPath), $dc.MoRef, $true) 

Notice the third parameter on the MakeDirectoy method.

When this is $true the method will create all missing parent directories in the path as well.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
frozenbubble
Contributor
Contributor

I can tell you for what I will use this. We've got a ESXi server with two datastores at the same size. At the moment I can keep several copies of Virtual Machines on the seperate datastore. (I know this is not an offside copy, and therefore not to be considered as a backup)

Therefore I'd like to have a folder structure to keep my copies.

Backup

|_ 20090701

|_ server 1

|_ server 2

|_ server 3

|_ 20090702

|_ 20090703

and so on..

edit:

There seem to be a configuration issue somewhere on my server.

Get-VMHost : 01.07.2009 00:33:38 Get-VMHost VMHost with name 'zgvm1.my.domain.com' not found, using the specified filter(s).

At line:1 char:41

+ $dc = Get-Datacenter -VMHost (Get-VMHost <<<< (Get-View (Get-Datastore -Name

$dsName | Get-View).Host[0].Key).Name) | Get-View

+ CategoryInfo : ObjectNotFound: (Smiley Happy , VimException

+ FullyQualifiedErrorId : Core_ContainerCmdletBase_ObjectNotFoundByName,VM

ware.VimAutomation.Commands.GetVMHost

0 Kudos
LucD
Leadership
Leadership

Luckily there is another way of doing this without using the hostname.

It seems that the forum SW again played up with some of the square brackets I had in the script.

Use the attached file to make sure you have a correct version.

$dsName = <datastore-name>
$dirPath = <folder-path>          # Can be "Test" or something like "Test/SubTest" 

$fileMgr = Get-View FileManager
$dc = Get-Datacenter -VMHost (Get-VIObjectByVIView -MORef (Get-View (Get-Datastore -Name $dsName | Get-View).Host[0].Key).MoRef) | Get-View
$fileMgr.MakeDirectory((" " + $dirPath), $dc.MoRef, $true) 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos