Hi there i'm trying to find a script to make a folder in a datastore
I've tried the following
Connect-VIServer -Server 192.168.200.### -Protocol https -User ### -Password ###
$DataStore = Get-VMHost 192.168.200.### Get-Datastore datastore1
New-Folder -Name ISO $DataStore.DatastoreBrowserPath
i can connect to the host and the datastore but it fails on creating the folder
any ideas on how this is possible ?
You can use the datastore provider for that.
See example 5 in the help
thanks for your reply i've tried the following command
mkdir -Path ds:\Datastore1 -ISO -Type Folder
i'm getting an error
mkdir : A parameter cannot be found that matches parameter name 'ISO'.
At line:1 char:28
+ mkdir -Path ds:\Datastore1 -ISO -Type Folder
+ CategoryInfo : InvalidArgument: (:) [mkdir], ParameterBindingEx
+ FullyQualifiedErrorId : NamedParameterNotFound,mkdir
It looks like there is a typo in that example
I assume ISO is the name of the new folder you want to create.
In that case you should do
mkdir -Path DS:\Datastore1\ISO
Or you can use the New-Item cmdlet
New-Item -Path ds:\Datastore1\ISO -ItemType Directory
thanks for that reply
i've tried both of those commands
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> New-Item
-Path ds:\datastore1\ISO -ItemType Directory
New-Item : Cannot find drive. A drive with the name 'ds' does not exist.
At line:1 char:1
+ New-Item -Path ds:\datastore1\ISO -ItemType Directory
+ CategoryInfo : ObjectNotFound: (ds:String) [New-Item], DriveNot
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.NewI
Did you create the datastore drive (the ds: part).
This is an example of the complete script
$datastore = Get-Datastore -Name Datastore1
New-PSDrive -Location $datastore -Name DS -PSProvider VimDatastore -Root "\"
New-Item -Path DS:\ISO -ItemType Directory
Remove-PSDrive -Name DS -Confirm:$false
It will create a folder ISO on the datastore called Datastore1
Anyone seen this?
New-PSDrive -Location $DatastoreObject -Name DS -PSProvider VimDatastore -Root "\" > $null
A parameter cannot be found that matches parameter name 'Location'.
Happens sporadically. Resolved by launching a new powershell window and retrying.
Hard to trap as hard to distinguish between this a genuine error
The PSDrive implementation has some issues and shortcomings.
Any suggested method to handle the error and reset/retry without bombing the script?
The main problem is repro in order to attempt a workaround.
I wrote a loop that mounted, tested a path and unmounted 5000 times. No error. So hard to catch.
You could do a Try-Catch construct.
Add -ErrorAction Stop on the New-PSDrive cmdlet to make sure it triggers a terminating exception.
So I made a little progress with this. Time will tell if its actually a full resolution.
After researching further and seeing other threads (also with @LucD in them!) I started to see a common trend. Whenever New-PSDrive fails, it seems that's because the underlying PSDrive connections have been lost.
For instance, if you do a "Get-PSDrive | Select Name,Provider" in a fresh shell, you'll see something like this:
Name Provider ---- -------- A Microsoft.PowerShell.Core\FileSystem Alias Microsoft.PowerShell.Core\Alias C Microsoft.PowerShell.Core\FileSystem Cert Microsoft.PowerShell.Security\Certificate D Microsoft.PowerShell.Core\FileSystem E Microsoft.PowerShell.Core\FileSystem Env Microsoft.PowerShell.Core\Environment F Microsoft.PowerShell.Core\FileSystem Function Microsoft.PowerShell.Core\Function G Microsoft.PowerShell.Core\FileSystem HKCU Microsoft.PowerShell.Core\Registry HKLM Microsoft.PowerShell.Core\Registry Variable Microsoft.PowerShell.Core\Variable WSMan Microsoft.WSMan.Management\WSMan
However, if you run the same command immediately after a Connect-VIServer, your output will look something like this
Name Provider ---- -------- A Microsoft.PowerShell.Core\FileSystem Alias Microsoft.PowerShell.Core\Alias C Microsoft.PowerShell.Core\FileSystem Cert Microsoft.PowerShell.Security\Certificate D Microsoft.PowerShell.Core\FileSystem E Microsoft.PowerShell.Core\FileSystem Env Microsoft.PowerShell.Core\Environment F Microsoft.PowerShell.Core\FileSystem Function Microsoft.PowerShell.Core\Function G Microsoft.PowerShell.Core\FileSystem HKCU Microsoft.PowerShell.Core\Registry HKLM Microsoft.PowerShell.Core\Registry Variable Microsoft.PowerShell.Core\Variable vi VMware.VimAutomation.Core\VimInventory vis VMware.VimAutomation.Core\VimInventory vmstore VMware.VimAutomation.Core\VimDatastore vmstores VMware.VimAutomation.Core\VimDatastore WSMan Microsoft.WSMan.Management\WSMan
Note the vmstore/vmstores that are now present.
When I managed to replicate the New-PSDrive error, after some playing around I discovered that in that shell, the two VimInventory and VimDatastore PSDrives were lost. No clue how. Rerunning your script in that shell does not fix the issue, only closing and relaunching the a new shell did, because you were starting over and the Connect-ViServer in the new shell reestablished the drives.
So I tried running a Connect-ViServer again in the 'broken shell' but that did not correct the problem. So I went further back and discovered that running these two lines of code restore the four missing PSDrives.
Remove-Module VMware.VimAutomation.Core -force -confirm:$false Import-Module VMware.VimAutomation.Core
So right now, anytime I am attempt to run New-PSDrive with a provider of vimDatastore in my script, I run the above two lines first. It doesn't seem to break anything else, and there is no need to run Connect-ViServer again.
I haven't encountered the error since but time will tell if that's just luck or if this is a valid workaround. This fix also seems to correct the issues I saw in parallel threads where users were getting errors to the effect of 'vmstores' being missing, or where you are attempting to use the vmstore/vmstores directly rather than using a New-PSDrive. Thats actually an elegant mode of use too, as long as you know (or discover) which vSphere Datacenter the datastore is in.
Anyhow, hope this helps someone else facing the same issue.
You can skip the New-PSDrive step entirely and just use the DatastoreBrowserPath property:
> New-Item -Path $DatastoreObj.DatastoreBrowserPath -Name TestFolder -ItemType Directory
Name Type Id
---- ---- --
> mkdir -Path $DatastoreObj.DatastoreBrowserPath -Name TestFolder2
Name Type Id
---- ---- --
Sweet. Thanks for the tip