VMware Cloud Community
francisco1080
Contributor
Contributor

Create a new directory in datastore - vSphere v6.7

Hi!

I have a script that did the following correctly:

$vCenterFqdn = 'vcenter.fqdn'

$vCenterUser = 'admin'

$vCenterPass = 'pass'

$viserver = Connect-VIServer -Server $vCenterFqdn -User $vCenterUser -Password $vCenterPass -ErrorAction Stop

$ds = Get-Datastore 'datastore'

$tgtFolder = 'destFolder'

New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null

New-Item -ItemType Directory -Path "DS:/$($tgtFolder)" | Out-Null

But since vSphere was updated to v6.7 it fails to execute the New-Item line:

New-Item -ItemType Directory -Path "DS:/$($tgtFolder)" | Out-Null

New-Item : 7/19/19 10:20:21 AM  VimDatastore            The specified location '/datastore/destFolder' does not exist.

At line:1 char:1

+ New-Item -ItemType Directory -Path "DS:/$($tgtFolder)" | Out-Null

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo          : ObjectNotFound: (:) [New-Item], PathException

+ FullyQualifiedErrorId : Core_VmStoreProviderCache_GetItem_ObjectNotFound,Microsoft.PowerShell.Commands.NewItemCommand

The directory is created in the vCenter with the friendly name "destFolder" and a random name but the execution of the script fails anyway. I think it's because now the attribute name 'destFolder' is the friendly-name and the id-name is expected.

On the other hand, when I execute a 'Get-ChildItem DS: /' only the name is printed, but I want the friendly name.

Is there any way to solve this? Thanks!

0 Kudos
11 Replies
LucD
Leadership
Leadership

Not sure what you mean by

The directory is created in the vCenter with the friendly name "destFolder" and a random name but the execution of the script fails anyway. I think it's because now the attribute name 'destFolder' is the friendly-name and the id-name is expected.

Could you perhaps include some screenshots to show what you mean?

Btw, your script is working perfectly for me.


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

0 Kudos
francisco1080
Contributor
Contributor

Thanks for your reply.

I going to reproduce my scenario. First, i try to create a directory in the datastore and i got this:

pastedImage_1.png

but nevertheless, the directory in the datastore was created succesfully with the previous line of code:

pastedImage_2.png

For some reason, the New-Item cmdlet don't know that the directory was created with a "Name" that is different that "Friendly name".

On the previous versions its work fine. This is my new version of vSphere:

pastedImage_3.png

0 Kudos
francisco1080
Contributor
Contributor

Also, when I made a Get-ChildItem I got the Name field. Before the update when creating an element, I was able to see the name that I assigned it with New-Item, now I can't:

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

That is a VSAN datastore if I'm not mistaken?


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

0 Kudos
francisco1080
Contributor
Contributor

Yes, is a VSAN datastore.

I found this script to list the folders' friendly name of the datastore, so I can loop to find the "Name" having the "Friendly Name". But I still have the problem of creating the folder.

List Folders on a VSAN Datastore using PowerCLI - Samples - VMware {code}

0 Kudos
LucD
Leadership
Leadership

On a VSAN datastore, you can use this to create subfolders

$dsName = 'vsanDatastore'

$parent = 'ParentFolder'

$newFolder = 'ChildFolder'


$ds = Get-Datastore -Name $dsName


# Get the folder name from the FriendlyName

$dsBrowser = Get-View -Id $ds.ExtensionData.Browser

$spec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

$spec.Query += New-Object VMware.Vim.FolderFileQuery

$folderObj = ($dsBrowser.SearchDatastore("[$($ds.Name)] \",$spec)).File | where{$_.FriendlyName -eq $parent}


New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root "\" | Out-Null

New-Item -ItemType Directory -Path "DS:/$($folderObj.Path)/$newFolder"

Remove-PSDrive -Name DS -Confirm:$false


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

0 Kudos
francisco1080
Contributor
Contributor

Thanks,

$dsBrowser = Get-View -Id $ds.ExtensionData.Browser

$spec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec

$spec.Query += New-Object VMware.Vim.FolderFileQuery

$folderObj = ($dsBrowser.SearchDatastore("[$($ds.Name)] \",$spec)).File | where{$_.FriendlyName -eq $parent}

that's helped me and it's perfect to work with friendly names, but I still missing the part of the create the folder error with the same behavior:

pastedImage_2.png

I think that is possible this version of vSphere can't support the New-Item cmdlet to create folders in a VSAN.

0 Kudos
LucD
Leadership
Leadership

That code doesn't come from the snippet I provided earlier.
What do you have in $newFolder?
Looks like it still says 'testFran2', which is a FriendlyName


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

0 Kudos
francisco1080
Contributor
Contributor

I had changed the value of the var.

Copy/pasting the snippet that you provides throws the following error that is the same behavior, the code fails and the folder is created in VSAN:

pastedImage_0.png

0 Kudos
francisco1080
Contributor
Contributor

the VSAN:

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

You are mixing two things here.
To create a folder in the root of the datastore, you can use your original script.

To create a (sub)folder in a folder on a VSAN datastore, you have to use the script I provided.


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

0 Kudos