Automation

 View Only
  • 1.  Script creates duplicates when exporting VMs to csv

    Posted Jul 24, 2019 07:24 AM

    I am trying to build a script that will extract list of VMs from several vsphere servers. At this point it does what I want, but unfortunately it also creates duplicate values in results .csv file.

    Just for note serverlist.txt and vicredentials.xml contains the same unique servers.

    Import-Module VMware.VimAutomation.Core
    Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false 

    if
    (Test-Path 'E:\vSphereScript\vCenterVMList.csv'){Remove-Item 'E:\vSphereScript\vCenterVMList.csv'} 
    $serverList
    = Get-Content -Path "E:\vSphereScript\serverlist.txt" 
    foreach
    ($server in $serverList) {
    $creds 
    = Get-VICredentialStoreItem -file "E:\vSphereScript\vicredentials.xml" -Host $server
    Connect-VIServer -server $creds.host -user $creds.user -password $creds.password
    Get-VM | Select-Object Name, Guest, VMhost, ResourcePool | Export-Csv -Path "E:\ProgramData\vCenterVMList.csv" -NoTypeInformation -Append
    Disconnect-VIServer $server -Confirm:$false  }

    Also does anyone have idea why this is so slow? If I connect-vidserver directly from powershell and use Get-VM it's almost instantaneous



  • 2.  RE: Script creates duplicates when exporting VMs to csv
    Best Answer

    Posted Jul 24, 2019 07:50 AM

    Could it be that you have multiple connections open?

    What is in $global:defaultviservers?

    What do you have in serverlist.txt?
    Are those vCenters?

    It looks as if you are overwriting your output file with each loop.

    Shouldn't that be

    if (Test-Path 'E:\vSphereScript\vCenterVMList.csv'){

       Remove-Item 'E:\vSphereScript\vCenterVMList.csv'

    }

    $serverList = Get-Content -Path "E:\vSphereScript\serverlist.txt"

    $report = @()

    foreach ($server in $serverList) {

       $creds  = Get-VICredentialStoreItem -file "E:\vSphereScript\vicredentials.xml" -Host $server

       Connect-VIServer -server $creds.host -user $creds.user -password $creds.password

       $report += Get-VM | Select-Object Name, Guest, VMhost, ResourcePool

       Disconnect-VIServer $server -Confirm:$false

    }


    $report |  | Export-Csv -Path "E:\ProgramData\vCenterVMList.csv" -NoTypeInformation -Append

    Note that behind Guest, VMhost and ResourcePool are complex objects.

    What do you actually want in your output file?

    ---------------------------------------------------------------------------------------------------------

    Was it helpful? Let us know by completing this short survey here.



  • 3.  RE: Script creates duplicates when exporting VMs to csv

    Posted Jul 24, 2019 09:00 AM

    Correct - serverlist.txt contains list of vCenters

    Let me test doing the export outside of the loop.

    Basically what I wanted to achieve is to have a list of VMs, together with their hosts in neat list.

    I don't care much about Guest and ResourcePool actually.



  • 4.  RE: Script creates duplicates when exporting VMs to csv

    Posted Jul 24, 2019 10:08 AM

    I modified the script so the export is outside of the loop and also removed Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -Confirm:$false

    As it wasn't required and could've been part of the problem.

    Additionally I removed Guest and ResourcePool from selected objects.

    Overall it's now working as expected and it's much faster.

    Thank you!