VMware Cloud Community
jvm2016
Hot Shot
Hot Shot

finding .vmx file using powercli

HI Luc,

Good morning

could you suggest the way to find all configurations files .vmx from  datastore .

we are doing this to find whats vms needs to be added to inventory in case they were removed earlier .

i tried to find in following way

$datastores = ("SAN01","SAN_02","SAN_03","SAN_D4","TRDL_VSAN1","VSAN2")

$report=foreach ($d in $datastores)

{

get-vm -datastore $d|select name

}

$report|out-file vmnames.txt

however above will give information depending on the display name of vcenter .what needs to be modified to get .vmx files in datastore.

0 Kudos
19 Replies
LucD
Leadership
Leadership

You can search for all unregistered VMX file with something like this

$Datastores = 'DS1','DS2'

foreach($Datastore in Get-Datastore $Datastores) {

    # Collect .vmx paths of registered VMs on the datastore

    $registered = @{}

    Get-VM -Datastore $Datastore | %{$registered.Add($_.ExtensionData.Summary.Config.VmPathName.Split('/')[-1],$true)}

    # Set up Search for .VMX Files in Datastore

    New-PSDrive -Name TgtDS -Location $Datastore -PSProvider VimDatastore -Root '\' | Out-Null

    $unregistered = @(Get-ChildItem -Path TgtDS: -Filter *.vmx -Recurse |

        where {$_.FolderPath -notmatch ".snapshot" -and !$registered.ContainsKey($_.Name)})

    Remove-PSDrive -Name TgtDS

}

$unregistered | Select Name,FolderPath


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

thnaks iam checking this.

0 Kudos
jvm2016
Hot Shot
Hot Shot

could you explain the above code specially new-psdrive part .

it seems it does not give the desired output .$unregistered giving me vms which are registered .

0 Kudos
LucD
Leadership
Leadership

For the PSDrive part, see about_vimdatastore

Are you sure that the VMs that you say are registered, are also registered on the datastore that is reported?

There can be vmx files on other datastores than the one on which the VM is registered


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

its giving blank output now .for sure there is one unregistered vm in one of the datastores . its name is"test"

as mentioned below.

pastedImage_0.png

also to avoid confusion i have included all datastores in $datastore array.

0 Kudos
LucD
Leadership
Leadership

Try the updated version above.


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

ok but i dont see any updated version from your side .

0 Kudos
LucD
Leadership
Leadership

I updated the script I gave earlier in place.
See above


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

sorry that also did not work.

0 Kudos
LucD
Leadership
Leadership

Strange, works for me.

Can we do some debugging.

What does this return for that datastore?

$dsName = 'Your-DS-Name'

$ds = Get-Datastore -Name $dsName

Get-VM -Datastore $ds | select Name,@{N='VMX';E={$_.ExtensionData.Summary.Config.VmPathName.Split('/')[-1]}}

New-PSDrive -Name TgtDS -Location $ds -PSProvider VimDatastore -Root '\' | Out-Null

Get-ChildItem -Path TgtDS: -Filter *.vmx -Recurse

Remove-PSDrive -Name TgtDS


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

pastedImage_0.png

so Get-ChildItem -Path TgtDS: -Filter *.vmx -Recurse     gave test.vmx also which is not given by the  $unregistered in updated script.

0 Kudos
LucD
Leadership
Leadership

Did you do the previous run with more than 1 datastore?
Is there another test VM on another datastore?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

yes previous run with 4 datastores and we used foreach loop.

No,there is only one unregistered test vm in first datastore which was displayed earlier .

0 Kudos
LucD
Leadership
Leadership

Then I can't explain why it doesn't work for you.

The debug run earlier showed all the correct values as I expected them.

Did another test in my environment, and it works.

Unless something went wrong in your copy/paste.

Can you attach the script as you are running it?


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

iam going to check this again .

also can yu suggest if there is a way to find duplicate session of powercli running under my credential.

as many times the same script(it happens with this script also and other previous script)gives me different output which is weird but true .

and i have been forced to think that a duplication session from somewhere else is running under my credential .

could you suggest any thing on above/

0 Kudos
LucD
Leadership
Leadership

Try like this (but this will only work if you logon with an AD account, not with a SSO account.

Get-View SessionManager |

select -ExpandProperty SessionList |

where{$_.UserName -match "$($env:USERNAME)"}


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

0 Kudos
jvm2016
Hot Shot
Hot Shot

Thanks luc .i will be checking both shortly.

0 Kudos
jvm2016
Hot Shot
Hot Shot

i got following .and iam running this session from 141.11.101.89    also i see same output again from 127.0.0.1 .is this normal output.???

also iam not sure what is callcount.

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership

That seems to be the Web Client you have open..

I see the same, no idea why this shows the localhost.


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

0 Kudos