PowerCLI

 View Only
Expand all | Collapse all

finding .vmx file using powercli

jvm2016

jvm2016Aug 16, 2018 10:39 AM

  • 1.  finding .vmx file using powercli

    Posted Aug 16, 2018 09:29 AM

    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.



  • 2.  RE: finding .vmx file using powercli

    Posted Aug 16, 2018 09:38 AM

    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



  • 3.  RE: finding .vmx file using powercli

    Posted Aug 16, 2018 10:39 AM

    thnaks iam checking this.



  • 4.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 04:19 AM

    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 .



  • 5.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 04:44 AM

    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



  • 6.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 07:26 AM

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

    as mentioned below.

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



  • 7.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 07:57 AM

    Try the updated version above.



  • 8.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 08:00 AM

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



  • 9.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 08:02 AM

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



  • 10.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 08:19 AM

    sorry that also did not work.



  • 11.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 08:29 AM

    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



  • 12.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 08:47 AM

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



  • 13.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 08:51 AM

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



  • 14.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 09:24 AM

    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 .



  • 15.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 09:35 AM

    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?



  • 16.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 11:12 AM

    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/



  • 17.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 11:44 AM

    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)"}



  • 18.  RE: finding .vmx file using powercli

    Posted Aug 17, 2018 12:55 PM

    Thanks luc .i will be checking both shortly.



  • 19.  RE: finding .vmx file using powercli

    Posted Aug 20, 2018 03:52 AM

    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.



  • 20.  RE: finding .vmx file using powercli

    Posted Aug 20, 2018 04:44 AM

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

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