Hi LucD, Prakash,
Thank you very much for your quick reply.
Here is what I made, after getting your replies, and I want to share it.
# Check VMs presence Report
# Variable definitions
$Timer = (Get-Date -Format yyyyMMdd-hhmm)
$OutputFile = "C:\data\report"+$Timer+".csv"
$vCenterServer = "vCenter1.local"
$vCenterUser = "Administrator@vsphere.local"
$vCenterUserPwd = "xxxxxxxxxxxxxxxxxxxxxxx"
$MailFrom = "User01 <user01@example.com>"
$MailTo = "User02 <user02@example.com>"
$MailSubject = "Alert:VM on the same host"
$MailBody = "Alert:VM on the same host. Perform manual migration."
$SmtpServer = "smtp.server.com"
# Add the PowerCLI snapin
Add-PSSnapin VMware* -ErrorAction SilentlyContinue
# Connect to the vCenter server
Connect-VIServer -Server $vCenterServer -User $vCenterUser -Password $vCenterUserPwd
# Get all the VM information and append this to the output file
$tgtVM = 'test1.local','test2.local'
Get-VM -Name $tgtVM | Group-Object -Property VMHost | %{
if($_.Count -gt 1){
$vm = $_.Group
$esx = $vm.VMHost
$tgtEsx = Get-Cluster -VM $vm | Get-VMHost | where{$_.Name -ne $esx.Name}
Get-VM $vm |`
Select-Object Name, @{N="ESX Host";E={Get-VMHost -VM $_}}, @{N="vCenter";E={$_.ExtensionData.CLient.ServiceUrl.Split('/')[2]}} | Out-File -FilePath $OutPutFile
}
}
# Send the output file via e-mail
Send-MailMessage -from $MailFrom -to $MailTo -subject $MailSubject `
-body $MailBody -Attachment $OutputFile -smtpServer $SmtpServer
Disconnect-VIServer $vCenterServer -Confirm:$false
I do not want to move automatically the VMs just yet, not before I do an audit of the internal services, so the vMotion will be done manually(for now).
Next stage:
I have several pairs of VMs(2 email_servers, 2DNS_servers and so on...) on the vCenter1 and I want to have the same notification (from above). How can I do that?
I have similar requirements, for multiple vCenters, so I need to run these script against on multiple vCenters. How can I do that?
For security reason, how can I hash the password and present the hash to vCenter to allow connection?
Thank you.