billyjoel
Enthusiast
Enthusiast

can't append output properly

Jump to solution

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 

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Seems to be working for me.
Try forcing all connections to close before the loop

$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"  


Disconnect-VIServer -Server * -Force -Confirm:$false


foreach ($vcenter in $vcenters){  

    Write-Output "connecting to vcenter $vcenter"

    Connect-VIServer $vcenter -Credential $credential 

    if (($vcenter -like "*amer*") -or ($vcenter -like "*eu*") -or ($vcenter -like "*asia*")){

        Get-VM | where-object {$_.name -Like "*test*"}   |

        Export-Csv -append $outputfile_1 -NoTypeInformation -UseCulture  

    } else{

        Get-VM | where-object {$_.name -notlike "*test*" }   |

        Export-Csv -append $outputfile_2 -NoTypeInformation -UseCulture

    }

  

    Write-Output "Disconnecting from $vcenter"

    Disconnect-VIServer $vcenter -Confirm:$false

} 


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

View solution in original post

0 Kudos
5 Replies
billyjoel
Enthusiast
Enthusiast

I came out with this "better" approach:

$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 $credential

    if (($vcenter -like "*amer*") -or ($vcenter -like "*eu*") -or ($vcenter -like "*asia*")){

    get-vm | where-object {$_.name -Like "*test*"}   |

    Export-Csv -append $outputfile_1 -NoTypeInformation -UseCulture 

    } else{

 

            get-vm | where-object {$_.name -notlike "*test*" }   |

            Export-Csv -append $outputfile_2 -NoTypeInformation -UseCulture

            }

 

Write-Output "Disconnecting from " $vcenter 

Disconnect-VIServer $vcenter -Confirm:$false

}

Now Im getting the disconnect message at the end of each round but Im still getting appended the new entries from the next $vcenter and adding the old ones again from the previous $vcenter.

I mean for example if I have 10 VMs with "test" in their names in the firts vcenter and 10 VMs with "test" in their names in the second vcenter I should get report1.csv with a total of 20 VM entries but instead im getting more that 100, each round for each $vcenter it re-appends everything even if the previous vcenter is disconnected, its growing exponentialy I believe.

0 Kudos
LucD
Leadership
Leadership

I suspect the name of the variable on the Connect-VIServer line is not correct.

It should be (without the s at the end)

Connect-VIServer $vcenter -Credential $credential


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

0 Kudos
billyjoel
Enthusiast
Enthusiast

Yep LucD​ I already did the correction there, please check my previous answer Im still getting a lot of entries when they should be not that much.

0 Kudos
LucD
Leadership
Leadership

Seems to be working for me.
Try forcing all connections to close before the loop

$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"  


Disconnect-VIServer -Server * -Force -Confirm:$false


foreach ($vcenter in $vcenters){  

    Write-Output "connecting to vcenter $vcenter"

    Connect-VIServer $vcenter -Credential $credential 

    if (($vcenter -like "*amer*") -or ($vcenter -like "*eu*") -or ($vcenter -like "*asia*")){

        Get-VM | where-object {$_.name -Like "*test*"}   |

        Export-Csv -append $outputfile_1 -NoTypeInformation -UseCulture  

    } else{

        Get-VM | where-object {$_.name -notlike "*test*" }   |

        Export-Csv -append $outputfile_2 -NoTypeInformation -UseCulture

    }

  

    Write-Output "Disconnecting from $vcenter"

    Disconnect-VIServer $vcenter -Confirm:$false

} 


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

View solution in original post

0 Kudos
billyjoel
Enthusiast
Enthusiast

forcing the connections to close did it, thanks Smiley Happy

0 Kudos