In this article, I will go over the basics of managing the VMware datastore with PowerCLI cmdlets.

In a VMware vSphere environment, a datastore is the basic building block for storage. You can use a few different types of datastores (such as NFS, iSCSI, or Fibre Channel), but they all do the same thing: they provide a means to store virtual machine data as well as other data.

What is a datastore

What is a datastore

For one-off tasks such as creating or removing a datastore in vSphere, a GUI is a fine choice, as it can easily guide the user. Although when it comes to bulk changes—for instance, adding 20 NFS datastores to each ESXi host—PowerCLI can save sysadmins hours of time and is a much better tool if you are working in a large environment.

For the sake of simplicities, I will be using NFS as my datastore type.

Creating a datastore ^

In PowerCLI, I must specify the VMhost, the name for the datastore, the path, and the NFS host when creating a new datastore:

As I alluded to earlier, typically you are going to want to add a datastore to all ESXi hosts at once. To create this datastore on all VMhosts in the cluster, I could use this command:

Viewing datastore information ^

To view information on any datastore in vSphere, you can run the Get-Datastore cmdlet. Piping the output to Select-Object * shows us some additional information such as authentication method, path, type of datastore, current state, and capacity.

To filter through data, we can pipe output to Where-Object as well, such as this example below, which only shows NFS datastores:

Removing datastores ^

There are only a few parameters for Remove-Datastore, -Datastore, -VMHost, and -Server.

These are quite self-explanatory. For instance, if you are decommissioning storage and want to remove a datastore from all hosts, you can run:

Moving items between datastores ^

One of the great functionalities of vSphere is the ability to seamlessly move not only virtual machines, but also other files between datastores. This comes in handy especially during maintenance windows and storage migrations among other tasks.

To move virtual machines between datastores (vMotion), the Move-VM cmdlet is useful:

We all know vMotion has been around forever, but what if I want to move an ISO (or any type of file) between datastores?

When you connect to vCenter with Connect-ViServer, it creates a PSDrive in your PowerShell session that gives you filesystem-like access to your datastores with the VimDatastore provider. To access them, we just use the path.

In this example, I have a file "test.iso" I want to upload to my "NewDataStore" datastore.

Using a GUI to manage vSphere is perhaps an easier way to use the system, but if you really want to excel in automating large-scale tasks, there is no better tool than PowerCLI combined with some good old PowerShell.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!

2+

Users who have LIKED this post:

  • avatar
Share
2 Comments
  1. sadheesh 2 years ago

    Hi Dan,

    Copy Item getting below error, tried with multiple PowerCLI versions but no luck. Any help will be appreciated.

    script:1

    PS C:\> Copy-DatastoreItem C:\temp\local2Ds.txt $ds.DatastoreBrowserPath -Recurse
    Copy-DatastoreItem : Upload of file 'C:\temp\local2Ds.txt' failed. Error message: The remote server returned an error: (400) Bad Request.
    At line:1 char:1
    + Copy-DatastoreItem C:\temp\local2Ds.txt $ds.DatastoreBrowserPath -Rec ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], VimException
    + FullyQualifiedErrorId : Client20_StorageServiceImpl_UploadFile_UploadFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreItem

    Script:2

    PS C:\> get-childitem –recurse (get-datastore “USPNRDHPR002”).datastorebrowserpath |where-Object{$_.name -like 'server*.txt'}|Copy-datastoreItem -Destination c:\temp -PassThru
    Copy-datastoreItem : 6/22/2018 7:29:46 AM VimDatastore Download of file
    'https://uspnvccvc001.devp.dbn.hpe.com/folder%2fservers.txt?dcPath=USPNCDC01&dsName=USPNRDHPR002' failed. Error message: The remote server
    returned an error: (404) Not Found.
    At line:1 char:119
    + ... like 'server*.txt'}|Copy-datastoreItem -Destination c:\temp -PassThru
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], VimException
    + FullyQualifiedErrorId : Client20_StorageServiceImpl_DownloadFile_DownloadFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreIt
    em

    Copy-datastoreItem : Object reference not set to an instance of an object.
    At line:1 char:119
    + ... like 'server*.txt'}|Copy-datastoreItem -Destination c:\temp -PassThru
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreItem

    Thanks.

    Sadheesh S

    0

  2. sadheesh 2 years ago

    Faced similar Issue and below solution helped to fix.
    1. C:\ProgramData\VMware\VMware VirtualCenter
    2. edit vpxa.cfg
    3. Edit below line False to True
    false

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2020

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account