VMware Cloud Community
sideoutteam
Contributor
Contributor

Help with simple VMware PowerCLI script

In running a script from this forum, we're having difficulties understanding whats being reported.  For almost every VM, its stating that its on multiple Datastores, but when looking at each on, the drivespace is only configured on one given datastore. 

For example this entry:

Name                                                                               Datastore                                                                                                              UsedSpaceGB                    ProvisionedSpaceGB     Folder

VMNameXDatastore1,datastore2,datastore1,datastore2152.1152.1vm

In looking at Vcenter, or the ESXi host, VMNameX is only provisioned on DataStore1 so we're not sure why its reporting multiple and duplicate?

Here's the script:

Get-VM |

Select Name,

@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},

@{N="UsedSpaceGB";E={[math]::Round($_.UsedSpaceGB,1)}},

@{N="ProvisionedSpaceGB";E={[math]::Round($_.ProvisionedSpaceGB,1)}},

@{N="Folder";E={$_.Folder.Name}} |

Export-Csv C:\report.csv -NoTypeInformation -UseCulture

Appreciate any help, we're new to the CLI

Reply
0 Kudos
15 Replies
LucD
Leadership
Leadership

Can you check for a VM which file might be sitting on another datastore?
It might be swap space, snapshots...
Run something like this

$vm = Get-VM -Name VMnameX

$vm.ExtensionData.LayoutEx.File.Name


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

Reply
0 Kudos
sideoutteam
Contributor
Contributor

Sorry, very new to this so I'm not sure how to run just that?

vm = Get-VM -Name 1.24SyslogServerGrayLog

Get-VM : A positional parameter cannot be found that accepts argument '='.

At line:1 char:1

+ vm = Get-VM -Name 1.24SyslogServerGrayLog

Reply
0 Kudos
LucD
Leadership
Leadership

All PowerShell variables start with a $-sign at the start.
You forgot the $-sign


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

Reply
0 Kudos
sideoutteam
Contributor
Contributor

Thanks for that! Smiley Happy

Here's an example showing multiple datastores, and duplicated.  VM only uses one hard drive on Datastore1

1.126cafe30TemplateDatastore2,Datastore1,Datastore2,Datastore1

PS C:\WINDOWS\system32> $vm = Get-VM -Name 1.126cafe30Template

PS C:\WINDOWS\system32> $vm.ExtensionData.LayoutEx.File.Name

[Datastore1] 1.126Template/1.126Template.vmx

[Datastore1] 1.126Template/1.126Template.nvram

[Datastore1] 1.126Template/1.126Template.vmsd

[Datastore1] 1.126Template/1.126Template_3.vmdk

[Datastore1] 1.126Template/1.126Template_3-flat.vmdk

[Datastore1] 1.126Template/1.126Template-99408afe.vswp

[Datastore1] 1.126Template/vmx-1.126Template-2571143934-1.vswp

[Datastore1] 1.126Template/vmware-1.log

[Datastore1] 1.126Template/vmware.log

[Datastore1] 1.126Template/1.126Template-aux.xml

[Datastore1] 1.126Template/1.126Template.vmx

[Datastore1] 1.126Template/1.126Template.nvram

[Datastore1] 1.126Template/1.126Template.vmsd

[Datastore1] 1.126Template/1.126Template_3.vmdk

[Datastore1] 1.126Template/1.126Template_3-flat.vmdk

[Datastore1] 1.126Template/1.126Template-99408afe.vswp

[Datastore1] 1.126Template/vmx-1.126Template-2571143934-1.vswp

[Datastore1] 1.126Template/vmware-1.log

[Datastore1] 1.126Template/vmware.log

[Datastore1] 1.126Template/1.126Template-aux.xml

[Datastore1] 1.126Template/1.126Template.vmx

[Datastore1] 1.126Template/1.126Template.nvram

[Datastore1] 1.126Template/1.126Template.vmsd

[Datastore1] 1.126Template/1.126Template_3.vmdk

[Datastore1] 1.126Template/1.126Template_3-flat.vmdk

[Datastore1] 1.126Template/1.126Template-99408afe.vswp

[Datastore1] 1.126Template/vmx-1.126Template-2571143934-1.vswp

[Datastore1] 1.126Template/vmware-1.log

[Datastore1] 1.126Template/vmware.log

[Datastore1] 1.126Template/1.126Template-aux.xml

Thanks again for the look!

Reply
0 Kudos
LucD
Leadership
Leadership

Is that a typo, or do I see two different names?
1.126cafe30Template and 1.126Template?


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

Reply
0 Kudos
LucD
Leadership
Leadership

Also, can you run this for 1 of those VMs?

Get-VM -Name VMnameX |

Select Name,

@{N = "DatastoreIds"; E = { $_.DatastoreIdList -join '|' } },

@{N = "Datastore"; E = { (Get-Datastore -VM $_).Name -join '|' } },

@{N = "Datastore2"; E = { (Get-Datastore -Id $_.DatastoreIdList).Name -join '|' }}


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

Reply
0 Kudos
sideoutteam
Contributor
Contributor

Odd, that's not a typo, I only changed the Datastore name to Datastore1.. and thats the only 'like' name VM on the solution, nothing else even close.

Here is the output:  At the bother of trying to change Datastore names, I just left them in.. the output is attached.

Reply
0 Kudos
LucD
Leadership
Leadership

That difference in names can be the result of a rename.

As long as there is no svMotion done, the VM's displayname and the foldername will stay different.

The fact that you have 4 entries in Datastore2, might be caused by multiple connections being open.
Check what $global:defaultviservers contains.

The quickest way to proceed would be to use the Web Client, select these datastores (one-by-one) and via the Files tab, check if both datastores have a folder with the same name.
And also what is in those folders.


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

Reply
0 Kudos
sideoutteam
Contributor
Contributor

PS C:\WINDOWS\system32> $global:defaultviservers

Name                           Port  User

----                           ----  ----

10.22.73.11                    443   DEVNET\devadm

10.22.1.16                     443   DEVNET\devadm

10.22.1.16                     443   DEVNET\qqs833625

this is showing just the viservers that I have connected to since we just started playing with this.

I've browsed the Datastores in question from VCenter, and fro its perspective only the correct Datastore has the VM data on it. The others that report it has data do not just from the Browse.  that's whats confusing.. We've looked at about 20 VM's, and its just the CLI that seems to be reporting info that's not accurate, or thats not  being reported in either the html or flash clients.

Reply
0 Kudos
LucD
Leadership
Leadership

Since you are connected to multiple vSphere servers, is it possible that VMs with the same DisplayName exist on multiple vCenters?


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

Reply
0 Kudos
sideoutteam
Contributor
Contributor

We only have the '1' Vcenter, thats at the 73.11

the 1.16 is on of 7 ESXi hosts.

Reply
0 Kudos
LucD
Leadership
Leadership

Did you already run the script with just the connection to the vCenter open?


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

Reply
0 Kudos
sideoutteam
Contributor
Contributor

Hmm I have not, that might be it?  I have meetings the rest of day but I'll rerun with only that connection tomorrow!

thanks again for all the great feedback and patience!

Reply
0 Kudos
sideoutteam
Contributor
Contributor

got out of the meeting as this is somewhat perplexing..

So I disconnected from both viservers, and only connected to the main vcenter.

after running the script it no longer duplicates the datastores as it did before, but it still reports that its on multiple datastores.  So only two entries instead of 4...

Only connecting to one server

1.126cafe30TemplateDELL_MD3800I2,DELL_MD3800i1

before while connected to 2

1.126cafe30TemplateDELL_MD3800I2,DELL_MD3800i1,DELL_MD3800I2,DELL_MD3800i1


Progress!! Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership

That was the easy part :smileygrin:

Can you try to unregister/register one of those VMs?

The following will do that, just change the value for $vmName.


After that is done, run the report again and check if there are still multiple datastores listed.

$vmName = 'VMnameX'

$vm = Get-VM -Name $vmName

$vmx = $vm.ExtensionData.LayoutEx.File | where { $_.Type -eq 'config' }

Remove-VM -VM $vm -DeletePermanently:$false -Confirm:$false

New-VM -VMFilePath $vmx.Name -VMHost $vm.VMHost -Confirm:$false


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

Reply
0 Kudos