VMware Cloud Community
AliSarreshteh
Enthusiast
Enthusiast
Jump to solution

Browse datastores using PowerCLI

We have a large VMware 5.0 installation across many data centers, clusters and hosts.  If we remove a VM from inventory without noticing the datastore it was residing on and need to add it back to inventory, we have to go through every datastore and browse each one.

Is there an easier way to search for a VM using PowerCLI or another method?

1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, AliSarreshteh-

Yes, that is close.  As written, your bit of code will look for all files with "DES084" at the end of the file name.  Since you want to find all files with "DES084" anywhere in the name, I assume (not just at the end of the file name), you need to add another wildcard character to the value for the -Include parameter in the "dir" statement.  Like:

...
## search for everything that has "DES084" in the name
dir -Recurse -Path vmstores:\ -Include *DES084* | select Name,DatastoreFullPath,LastWriteTime | Export-Csv -NoTypeInformation -UseCulture -Path D:\MyVMDKInfo.csv

That do better for you?

View solution in original post

7 Replies
mattboren
Expert
Expert
Jump to solution

Hello, AliSarreshteh-

One way to do so is to use the PSDrive "vmstores" that is accessible when you are connected to one or more VIServers (hosts or vCenters).  You can use the standard Get-ChildItem cmdlet (aliased as "dir" by default) to find files that match given criteria, like VM names, for instance.  See the example at http://communities.vmware.com/message/2080522#2080522.

That help?

0 Kudos
AliSarreshteh
Enthusiast
Enthusiast
Jump to solution

Sorry for the delayed response...

So something like the following should work to connect to the vCenter "vcenter-server.test.com" and find all the files/folders with DES084 in the name?  After running this, the CSV output file was blank:

#Load PowerCLI module in PowerShell
Add-PSSnapin VMware.VimAutomation.Core
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Confirm:$False

#Connect to the VCenter server

Connect-Viserver "vcenter-server.test.com" -user "userid-entered-here" -password "password-entered-here"
dir -Recurse -Path vmstores:\ -Include *DES084 | select Name,DatastoreFullPath,LastWriteTime | Export-Csv -NoTypeInformation -UseCulture -Path D:\MyVMDKInfo.csv

mattboren
Expert
Expert
Jump to solution

Hello, AliSarreshteh-

Yes, that is close.  As written, your bit of code will look for all files with "DES084" at the end of the file name.  Since you want to find all files with "DES084" anywhere in the name, I assume (not just at the end of the file name), you need to add another wildcard character to the value for the -Include parameter in the "dir" statement.  Like:

...
## search for everything that has "DES084" in the name
dir -Recurse -Path vmstores:\ -Include *DES084* | select Name,DatastoreFullPath,LastWriteTime | Export-Csv -NoTypeInformation -UseCulture -Path D:\MyVMDKInfo.csv

That do better for you?

AliSarreshteh
Enthusiast
Enthusiast
Jump to solution

Yo Matt, you da man! Thanks much...I couldn't find any documentation on constructing the search string.

Thank you...

0 Kudos
AliSarreshteh
Enthusiast
Enthusiast
Jump to solution

One last question: if we are looking for a particular Cluster with all the hosts and all the datastores they contain...would this work or do I need to modify the Get-ChildItem?

#Connect to the VCenter server
Connect-Viserver "vcenter-server.test.com" -user "userid-entered-here" -password "password-entered-here"

$cluster = "Test"
Write-Host $cluster
$VMHosts = Get-Cluster $Cluster | Get-VMhost | sort
foreach ($VMHost in ($VMHosts)) {
   Write-Host $VMHost

   $datastores = Get-Datastore -VMHost $VMHost| sort
   foreach ($datastore in ($datastores)) {
      write-host $datastore
      Get-ChildItem -Recurse -Path vmstores:\ -Include *ILTEST084* | select Name,DatastoreFullPath,LastWriteTime | Export-Csv -NoTypeInformation -UseCulture -Path D:\MyVMDKInfo99.csv

}
}

mattboren
Expert
Expert
Jump to solution

For the benefit of those who encounter this thread, the last question asked here is continued at the "Part Two" thread at http://communities.vmware.com/message/2210296.

0 Kudos
Jared1978
Contributor
Contributor
Jump to solution

This also works if you're looking for files from a specific data store as well.

Note replace *datastorename, with the name of the data store you wish to browse ( " * " can be used as a wildcard).

The recurse parameter expands all the folders listed and shows their contents. 


dir -recurse (get-datastore *datastorename).datastorebrowserpath

0 Kudos