hi all,
I have a few vCentres (5.5 and 5.1) which I want to setup a schedule task to collect some information and email me a csv every 1st of the month.
To start with I am testing on 2x vcentres just collecting the esxi hosts (the info I need is different, but once I get the script working I can amend for what I need).
I setup something like:
$user = "domain\username"
$pass = "user password"
$vcentres = "IP1, IP2"
ForEach ($vcentre in $vcentres)
{
$filedate = get-date -uformat '%d-%m-%Y-%H%M%S'
$filename = "c:\tmp\reports\" + $vcentre + "_" + $filedate + ".csv"
Write-Host 'Connecting to' $vcentre
Connect-viserver -server $vcentre -User $user -Password $pass
get-vmhost | select Name | Export-CSV $filename -NoType -UseCulture
}
Disconnect-viserver -Server * -confirm:$false
I was expecting to receive 2x csv files, one for each vcentre IP, i.e., c:\tmp\IP1_date_time.csv and c:\tmp\IP2_date_time.csv
Script runs file but only generates 1x csv with all esxi hosts in it.
I think I need to setup another variable to store the csv when on the first loop pass, then on the second and finally save both to the location specified.
Could someone advice what I am doing wrong?
Thanks in anticiaption.
Try doing the Disconnect-VIServer within the foreach loop.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try doing the Disconnect-VIServer within the foreach loop.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc's right, you connect within the foreach loop, if you don't disconnect, the second connection will be added.
$user = "domain\username"
$pass = "user password"
$vcentres = "IP1, IP2"
ForEach ($vcentre in $vcentres)
{
$filedate = get-date -uformat '%d-%m-%Y-%H%M%S'
$filename = "c:\tmp\reports\" + $vcentre + "_" + $filedate + ".csv"
Write-Host 'Connecting to' $vcentre
Connect-viserver -server $vcentre -User $user -Password $pass
get-vmhost | select Name | Export-CSV $filename -NoType -UseCulture
Disconnect-viserver -Server * -confirm:$false
}
LucD,
Thank you and that is really embarrassing on my side....
Would you guys know which permissions I should give to a "service account" to connect and extract this info from the vcentres?
I suspect the "Read-Only" role has all the privileges you require to report.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference