VMware Cloud Community
sunder3480
Contributor
Contributor
Jump to solution

Finding PowerCli script to move newly created VMs into specific DRsClusterGroup

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..

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

6 Replies
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
sunder3480
Contributor
Contributor
Jump to solution

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. 

sunder3480_0-1686132815441.png

 

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

sunder3480
Contributor
Contributor
Jump to solution

Again getting error for the "Name" and attached below screenshot for your reference.

 

sunder3480_0-1686135195775.png

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There was a typo, it is corrected.


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

0 Kudos
sunder3480
Contributor
Contributor
Jump to solution

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"

0 Kudos