Hi all,
I'm running the below script to connect to a list of vcenters one by one and get all the VMs that have "test" in their names and export them to a CSV file, and all the VMs that does NOT have "test" in their names to another CSV file.
The problem is that it is not disconnecting from $vcenter when it's done, so each round it's appending the new entries from the next $vcenter and adding the oldones ones again from the previous $vcenter, so for example instead of getting at the end 140 entries I'm getting +1K entries .
Im not getting the "Disconnecting from $vcenter" output at the end of each round.
$vcenters = get-content vcenters.txt $credential = Get-Credential -Message "Enter vCenter Credentials." $outputfile_1 = "C:\users\user.name\Desktop\report1.csv" $outputfile_2 = "C:\users\user.name\Desktop\report2.csv" foreach ($vcenter in $vcenters){ Write-Output "connecting to vcenter" $vcenter Connect-VIServer $vcenter -Credential $credentials get-vm | where-object {$_.name -Like "*test*"} | Export-Csv -append $outputfile_1 -NoTypeInformation -UseCulture get-vm | where-object {$_.name -notlike "*test*" } | Export-Csv -append $outputfile_2 -NoTypeInformation -UseCulture Write-Output "Disconnecting from " $vcenter Disconnect-VIServer $vcenter -Confirm:$false }