VMware Cloud Community
Pilu1978
Enthusiast
Enthusiast

Searching files in Datastore

HI,

I want to create a script but and I am new to powercli so need your help to achieve the below:

Generally when we browse any datastore the path is showing as mentioned below:

[datastorename]/VMfoldername1

[datastorename]/VMfoldername2

[datastorename]/VMfoldername3

.

.

.

Now I need a script which will go to each "VMfoldername" folder of a datastore and search for vmdk, flat-vmdk and vmx files. If any of these three files are not present it will return the name of VMfoldername.

I search a lot in internet but all I am getting is orphaned vmdk script where files are searched on per datastore basis and detect the orphaned vmdk.

But my requirement is little different.

Plesae help me to achieve the above mentioned task through powercli.

Thanks in advance.

0 Kudos
8 Replies
LucD
Leadership
Leadership

You could use the datastore provider.

Something like this

$ds = Get-Datastore -Name MyDS 
New-PSDrive
-Location $ds -Name ds -PSProvider VimDatastore -Root '\'
Get-ChildItem
-Path ds:\ -Recurse

In the objects that are returned you can now use the -match operator on the Name property to test your files


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Hi LucD,

Thanks a lot for the information.

I would request if you can send me the entire script. As I am new to powercli so it will help me understand it completely and gather knowledge as well.

0 Kudos
LucD
Leadership
Leadership

Sure, try something like this.

The script uses a recursive function to traverse through the folder structure.

function Test-Folder{
    param(
    [string]$FolderPath
    )         $found = Get-ChildItem -Path $FolderPath | where {$_.Name -match ".vmdk" -or $_.Name -match ".vmx"}     if(!$found){         $FolderPath
    }         Get-ChildItem -Path $FolderPath | where {$_.ItemType -eq "Folder"} | %{         Test-Folder -FolderPath ($FolderPath + "\" + $_.Name)     } } $ds = Get-Datastore -Name MyDS
New-PSDrive -Location $ds -Name ds -PSProvider VimDatastore -Root '\' | Out-Null
$startPath
= "ds:\"
Test-Folder
-FolderPath $startPath
Remove-PSDrive
-Name ds


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Hi Lucd.

Thanks a lot. But I am getting the the error mentioned in the attahced txt file.

Please help.

0 Kudos
LucD
Leadership
Leadership

How did you run the script ?

From an editor ?

Or from the PowerCLI prompt ?


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

In a powercli prompt.

Also If any VMfoldername is found where no vmdk and vmx files are present then it will show the VCName, Clustername, Datastorename and VMFolderName in a html report and it will do for multiple VCs. Is it possible?

0 Kudos
LucD
Leadership
Leadership

Try to store the script in a .ps1 file.

Then from the PowerCLI prompt do

./MyScript.ps1

That should produce a run without errors.

To have the output in HTML you can do something like this

function Test-Folder{
    param(
    [string]$FolderPath    )
    
    $found = Get-ChildItem -Path $FolderPath | where {$_.Name -match ".vmdk" -or $_.Name -match ".vmx"}
    if(!$found){
        $FolderPath
    }        
Get-ChildItem -Path $FolderPath | where {$_.ItemType -eq "Folder"} | %{         Test-Folder -FolderPath ($FolderPath + "\" + $_.Name)     } } $dsName = "MyDS"
$filePath
= "C:\folderpath.html"

$ds = Get-Datastore -Name $dsName New-PSDrive -Location $ds -Name ds -PSProvider VimDatastore -Root '\' | Out-Null
$startPath
= "ds:\" Test-Folder -FolderPath $startPath | Select @{N="Datastore";e={$dsName}},@{N="Path";E={$_}} | ConvertTo-Html | Out-File $filePath
Remove-PSDrive
-Name ds Invoke-Item $filePath


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

0 Kudos
Pilu1978
Enthusiast
Enthusiast

Hi Lucd,

Thanks the script is running now but output is not showing the vmfoldername which does not have any .vmdk or .vmx file. I have one vmfoldername in my datastore which does not have the .vmdk and .vmx file. The script should show the vmfoldername along with the datastore and cluster name. But it is only showing the datastore name.

Actually what I wanted to search if there is any  Orphan VM  exists (registered or unregistered) in the VC.

The only logic in my mind is to search all the folders in all the datastores in a VC and if there is any folder which doesnot contain any .vmdk or .vmx file the folder is treated as junk and should be reported. So I wanted a script which can do this for me. I am not sure if my logic is correct.

Please help.

0 Kudos