VMware Cloud Community
mmangiante
Contributor
Contributor

Get-Datastore and how it works in PowerCLI 4.1

Hello,

I have installed on a server VMWare ESXi 4.1.0. On  this installation, I have two datastores (datastore1 and datastore2) and  on each datastore I have a virtual machine; now, I found a simple  script that help me to backup a virtual machine: in this script I  retrieve the datastore with this line of code:

# We get the datastore of the machine
$ds = Get-Datastore -Server $vh -VM $vm

were $vh and $vm are the connection to the server and the (one) virtual machine on that server.

What  I expect is the datatstore related to that $vm but instead the  Get-Datastore retrieves both the datastores on the server: is this the  behaviour of this function?

In what mode I can retrieve the datastore related to $vm ?

--

Regards,

Marco Mangiante

P

Reply
0 Kudos
9 Replies
RvdNieuwendijk
Leadership
Leadership

Hi Marco,

you should remove the -Server parameter. So it will be:

# We get the datastore of the machine
$ds = Get-Datastore -VM $vm
Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
Sreejesh_D
Virtuoso
Virtuoso

this will list the name of datastores associated to the VM.

Connect-VIServer <server>

$HardDisk = Get-VM -Name <VM name>| Get-HardDisk

$HardDisk.FileName.Split("]")[0].TrimStart("[")

Reply
0 Kudos
mmangiante
Contributor
Contributor

Hello Robert,

Robert van den Nieuwendijk ha scritto:

Hi Marco,

you should remove the -Server parameter. So it will be:

# We get the datastore of the machine
$ds = Get-Datastore -VM $vm

I tried it but even this retrieves the 2 datastores. For simplicity, this is the code that I'm using:

# Connet to ESX
$vh = Connect-VIServer -Server $server -Port 443 -User $login -Password $password

# Get the machine we are interested in
$vm = Get-VM -Server $vh -Name $machine


#Get the datastore of the machine
$ds = Get-Datastore -VM $vm

where $server, $login, $password and $machine are parameters passed from command line.

--

Regards,

Marco Mangiante

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Are you sure that all of the disks of the VM are on the same datastore? If you split the disks of the vm over multiple datastores then the Get-Datastore cmdlet will return all these datastores.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
IgorAfonin
Contributor
Contributor

Hi Marco,

Maybe any iso connected to vm ?

Get-VM $vm |  Get-CDDrive | FT Parent, IsoPath
#and please, show output
Get-HardDisk -VM $vm

Also check where is vmx file is located.

Have you any snapshots on VM ?

Reply
0 Kudos
mmangiante
Contributor
Contributor

Hello Robert,

Robert van den Nieuwendijk ha scritto:

Are you sure that all of the disks of the VM are on the same datastore? If you split the disks of the vm over multiple datastores then the Get-Datastore cmdlet will return all these datastores.

I have 2 physical hdd: on datastore1 (first hard disk) there is the ESXi and the virtual machine that name I pass to the script at command line, on the second datastore (on the second hard disk), there is the virtual machine where I excute the script.

Maybe is this the problem?

--

Regards,

Marco Mangiante

Reply
0 Kudos
Sreejesh_D
Virtuoso
Virtuoso

then the output you are getting now is expected, both datastores will be listed. Because the VM is associated to both datastore1 and datastore2. 

Reply
0 Kudos
mmangiante
Contributor
Contributor

Hello,

yezdi ha scritto:

then the output you are getting now is expected, both datastores will be listed. Because the VM is associated to both datastore1 and datastore2. 

so, to summarize:

datastore1 -> vm-test (that I pass to the script to backup)

datastore2 -> svr-bck (where I execute the script)

So, I execute the script on svr-bck to backup vm-test and so vm-test is associated to both datastores?

--

Regards,

Marco Mangiante

Reply
0 Kudos
LucD
Leadership
Leadership

No, that doesn't make sense.

Can you do

Get-VM -Name vm-test | Get-HardDisk | Select Name,FileName 

to see where the harddisk(s) are located.

And this

$vm = Get-VM -Name vm-test 
$vm
.ExtensionData.LayoutEx.File | Select Name

should let us see if there are any other (non-VMDK) files on the 2nd datastore.


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

Reply
0 Kudos