VMware Cloud Community
dudi_cog
Contributor
Contributor
Jump to solution

Fail to create a folder in VMFS datastore using a .Net PowerShell object

Hi friends,

I'm using .Net System.Management.Automation.PowerShell object to invoke:
New-Item -Path "vmstore:\Test-DC\ESX_Log\ESX10.161.47.228" -ItemType Directory -Force
The returned error is:
The specified path does not refer to a drive created by this provider.

However, from PowerShell console the same command it works fine as shown below.

New-Item -Path "vmstore:\SiltecRX2-DC\ESX_Log\ESX10.161.47.229" -ItemType Directory -Force

Name Type Id
---- ---- --
ESX10.161.47.229 DatastoreFolder

Any help will be appreciated!

0 Kudos
1 Solution

Accepted Solutions
dudi_cog
Contributor
Contributor
Jump to solution

The following workaround worked for me:

1. Create a PSDrive with VimDatastore as the PS Provider.

2. create the directory using New-Item cmdlet with the created PSDrive as the drive letter.

3. Remove the PSDrive.

Here is the C# code I use:

PowerShell _powerShell;

_powerShell = PowerShell.Create();

_powerShell.AddScript($"Get-Datastore {datastoreName} | New-PSDrive -Name {PSDriveName} -PSProvider VimDatastore -Root \"\\\\\"");
_powerShell.AddScript($"New-Item -Path {PSDriveName}:\\{folderName} -ItemType Directory -Force");
_powerShell.AddScript($"Remove-PSDrive -Name {PSDriveName}");

 

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Looks like you don't have the VimDatastore provider in that environment.
Did you check with Get-PSProvider?

That provider should normally be created when you run the Connect-VIServer


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

0 Kudos
dudi_cog
Contributor
Contributor
Jump to solution

Thanks, I will check.

0 Kudos
dudi_cog
Contributor
Contributor
Jump to solution

dudi_cog_0-1706637013404.png

It seems I have VimDatastore as one of the providers:

VMware.VimAutomation.Core\VimDatastore

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you try if some basic functionality with the VimDatastore provider works?

Like

Get-Item -Path vmstore:
Get-childItem -Path vmstore:


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

0 Kudos
dudi_cog
Contributor
Contributor
Jump to solution

I tried it and here are the results:

Get-Item -Path "vmstore:"

Cannot find path 'vmstore:\' because it does not exist

 

Get-childItem -Path "vmstore:"

Cannot find path '\LastConnectedVCenterServer\' because it does not exist.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That again seems to show that the provider is not set up correctly in that environment.
Sorry but I have no further experience with calling PS from a .Net environment.


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

0 Kudos
dudi_cog
Contributor
Contributor
Jump to solution

Thanks for trying man:-)

It seems like I have some environment issue.

I will try to reinstall PowerCLI on my machine and update.

0 Kudos
dudi_cog
Contributor
Contributor
Jump to solution

The following workaround worked for me:

1. Create a PSDrive with VimDatastore as the PS Provider.

2. create the directory using New-Item cmdlet with the created PSDrive as the drive letter.

3. Remove the PSDrive.

Here is the C# code I use:

PowerShell _powerShell;

_powerShell = PowerShell.Create();

_powerShell.AddScript($"Get-Datastore {datastoreName} | New-PSDrive -Name {PSDriveName} -PSProvider VimDatastore -Root \"\\\\\"");
_powerShell.AddScript($"New-Item -Path {PSDriveName}:\\{folderName} -ItemType Directory -Force");
_powerShell.AddScript($"Remove-PSDrive -Name {PSDriveName}");

 

0 Kudos