We are having multiple cluster in the vCenter, i am trying to find a script to attached in the "NEW VM Deployment" Alarm with the action of 'Run a script' to move that new VM into specific DRsClusterGroup. I found the below command only to move the existing VMs manually into the group. But someone please assist to have the script and it will move the newly created VMs into the DRsCLuster Group.
"Set-DrsClusterGroup -DrsClusterGroup "Stage VMS" -VM "test1" -Add
I found another option to get list of VMs are not part of any existing DRsCluster group using the below script suggested by Mr.LucD, but unable to export output & send mail the the of list vms.
-----------------------
$cluster = Get-Cluster -Name "Cluster1"
$fileName = [IO path]::\ GetTempFileName(1) | Rename-Item -NewName {$_ -replace 'tmp$', 'csv' } –PassThru
$vmsInGroup = $cluster.ExtensionData.ConfigurationEx.Group | Where {$_ -is [VMware.Vim.ClusterVmGroup]} | %{
Get-View -Id $_.VM | Select -ExpandProperty Name
}
Get-VM -Location $cluster | Where {$vmsInGroup -notcontains $_.Name} |
Select -ExpandProperty Name |
Export-Csv -Path $fileName -NoTypeInformation -UseCulture
Send-MailMessage -Attachments $fileName -From "vCenter@mas.com" -To "alagu@mas.com"
Subject "Report" -SmtpServer "test@mas.com"
----------------------------------------------------
Thanks..
Try like this
$cluster = Get-Cluster -Name MyCluster
$fileName = [IO.Path]::GetTempFileName() | Rename-Item -NewName {$_ -replace 'tmp$', 'csv' } –PassThru
$vmsInGroup = $cluster.ExtensionData.ConfigurationEx.Group | Where {$_ -is [VMware.Vim.ClusterVmGroup]} | %{
Get-View -Id $_.VM | Select -ExpandProperty Name
}
Get-VM -Location $cluster | Where {$vmsInGroup -notcontains $_.Name} |
Select @{N='VM';E={$_.Name}} |
Export-Csv -Path $fileName -NoTypeInformation -UseCulture
Send-MailMessage -Attachments $fileName -From "lucd@lucd.info" -To "lucd@lucd.info" `
-Subject "Report" -SmtpServer "mail.lucd.info"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I assume you are referring to Solved: Re: PowerCli scipt to find VM outside DRS Groups i... - VMware Technology Network VMTN
If you check that code, you'll notice that there are some errors in your copy.
The $fileName line is incorrect, it should be
$fileName = [IO.Path]::GetTempFileName() | Rename-Item -NewName {$_ -replace 'tmp$', 'csv' } –PassThru
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Dear Luc, Thanks. You are correct. After changed the filename , I received the mail with the report . but the VM names are not showing instead of showing as attachment.
Can you please suggest on the same.
Try like this
$cluster = Get-Cluster -Name MyCluster
$fileName = [IO.Path]::GetTempFileName() | Rename-Item -NewName {$_ -replace 'tmp$', 'csv' } –PassThru
$vmsInGroup = $cluster.ExtensionData.ConfigurationEx.Group | Where {$_ -is [VMware.Vim.ClusterVmGroup]} | %{
Get-View -Id $_.VM | Select -ExpandProperty Name
}
Get-VM -Location $cluster | Where {$vmsInGroup -notcontains $_.Name} |
Select @{N='VM';E={$_.Name}} |
Export-Csv -Path $fileName -NoTypeInformation -UseCulture
Send-MailMessage -Attachments $fileName -From "lucd@lucd.info" -To "lucd@lucd.info" `
-Subject "Report" -SmtpServer "mail.lucd.info"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Again getting error for the "Name" and attached below screenshot for your reference.
There was a typo, it is corrected.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Excellent, It worked. But your previous solution itself worked for me . I removed the {-ExpandProperty Name} , then it started working well.
$cluster = Get-Cluster -Name "Cluster1"
$fileName = [IO.Path]::GetTempFileName() | Rename-Item -NewName {$_ -replace 'tmp$', 'csv' } –PassThru
$vmsInGroup = $cluster.ExtensionData.ConfigurationEx.Group | Where {$_ -is [VMware.Vim.ClusterVmGroup]} | %{
Get-View -Id $_.VM | Select -ExpandProperty Name
}
Get-VM -Location $cluster | Where {$vmsInGroup -notcontains $_.Name} |
Select Name |
Export-Csv -Path $fileName -NoTypeInformation -UseCulture
Send-MailMessage -Attachments $fileName -SmtpServer "smtp@mas.com" Subject "Report" -From "vCenter@mas.com" -To "alagu@mas.com"