VMware Cloud Community
Leffingwell
Contributor
Contributor

Browsing Datastores

Hi All -- I've been work on getting a kind of search engine for our datastores going (rather than using the browse datastore / search feature in the VIC)

My logic may be imperfect in how I'm designing this thing - I'm just trying to work with methods I know of.  Everything else I've seen uses some insane syntax I've never seen and/or don't know where they're getting it from.

My process:

get the datastores and mount them to a psdrive

based on search terms do an "ls" of those terms through every datastore

write the output of where it found items based off of a users search

Reason:

We've got VM's spanning multiple datastores .. and multiple departments.  We've just asked them to delete a bunch of VM's, and they're doing it through the VIC, but we need to verify there are no folders matching the VM's deleted left in other datastores..

Here's what I've cooked up ..

# What are you looking for?
$searchterms = "*sga*"

# Search results
$searchresults = @()

# Set datastores to a PSDrive for searching
$DSList = get-datastore
$DSList | % {
    $DSInfo = "" | select Name
    $DSInfo.Name = $_.name
    new-psdrive -location $_ -name $DSInfo.Name -psprovider vimdatastore -root "\"
    cd $DSInfo.Name:
    $searchresults += ls $searchterms
    }
   
$searchresults

Starting from the 'cd' line hopefully you can see what I'm trying to accomplish Smiley Happy  Whether or not it's smart is another story..

Looking forward to any help !

5 Replies
LucD
Leadership
Leadership

Your idea is quite good, although it my not be the fastest method.

The other syntax you mention most probably uses some of the SDK method to do the datastore search.

But what is in fact the question, doesn't the script return what you are looking for ?


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

CRad14
Hot Shot
Hot Shot

I would probably do something like...

# What are you looking for?
$searchterms = "*sga*"

# Search results
$searchresults = @()

# Set datastores to a PSDrive for searching
$DSLists = get-datastore

ForEach($datastore in DSLists)

{

remove-psdrive search | out-null

new-psdrive -location $datastore -name Search -psprovider vimdatastore -root "\"

$searchresults += Get-ChildItem -Path Search: -recurse | Where-Object { $_.name -like $searchterms} | % {$_.fullname}

}

$searchresults

I think this way is just a bit cleaner, but yours certainly looks like it would work as well. It really isn't necessary to setup all the dsinfo stuff unless you really want to Smiley Happy . I hope this helps! You could also put a Out-Null at the end of the new-psdrive line, then you wouldn't see it setup each datastore as  a psdrive

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
Leffingwell
Contributor
Contributor

Hey guys glad to see some responses!

@LucD - thanks for the compliment!  That means a lot coming from you !  The script does not in fact return what I desire, the expression it gets hung up on is the

cd $DSInfo.Name:

part.  I've since cleaned up the script and found out a solution to why it wasn't actually changing path with the above command! Behold below, my solution Smiley Happy

# What are you looking for?
$UserInput = Read-Host 'What would you like to search for?'
$SearchTerms = "*$UserInput*"

# Search results
$SearchResults = @()

# Set datastores to a PSDrive for searching
$DSList = Get-Datastore
$DSList | % {
    $DSName = $_.name
    New-PSDrive -location $_ -name $DSName -psprovider VimDatastore -root "\"
    cd  $DSName":"
    $SearchResults += ls $SearchTerms | select PSDrive, PSChildName, ItemType, LastWriteTime
    }
   
$SearchResults | out-gridview

Works like a charm !!!!!!!!!!!!!  I welcome feedback about faster ways to do this etc.. LucD you were correct it the super complicated scripts I had seen were using the API .. I know how to read the API documentation, I just don't know an intuitive way to divine that information on my own to actually create something myself.

Enjoy gentlemen/ladies

Leffingwell
Contributor
Contributor

See my last post Smiley Happy Solution listed.

Reply
0 Kudos
CRad14
Hot Shot
Hot Shot

Just so you know, in case you don't.

"ls" is an alias for "Get-ChildItem", so they are interchangable

Conrad www.vnoob.com | @vNoob | If I or anyone else is helpful to you make sure you mark their posts as such! 🙂
Reply
0 Kudos