Remove VM's that no longer exist

PowerShell script exchange

Remove VM's that no longer exist

Veeam Logoby decoy5657 » Thu Oct 17, 2013 1:13 pm

I run a powershell script every day to automatically add new machines to my backup jobs... but if a machine is deleted from vCenter, it creates errors in Veeam... Is there a way to remove these machines automatically with powershell or some other method?
decoy5657
Influencer
 
Posts: 12
Liked: 3 times
Joined: Wed Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson

Re: Remove VM's that no longer exist

Veeam Logoby v.Eremin » Thu Oct 17, 2013 1:23 pm

I believe can either use Veeam snap-in o Veeam snap-in in conjunction with PowerCLI. As to the algorithm, it should be as the following:

1) Get VMs that are present in a given job (Veeam PSSnapin).
2) Check whether these VMs are present in vCenter, as well. (VeeamPSSnapin or PowerCLI)
3) Delete VMs that aren’t. (Veeam PSSnapin)

Method including PowerCLI seems to be more reliable, since I’m not sure whether the changes made to Virtual Infrastructure, such as adding/deleting VMs, are immediately reflected in Veeam PSSnapin.

Thanks.
v.Eremin
Veeam Software
 
Posts: 13255
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Remove VM's that no longer exist

Veeam Logoby decoy5657 » Thu Oct 17, 2013 5:08 pm

any tips on doing the comparison? (Step 2 in your list)
decoy5657
Influencer
 
Posts: 12
Liked: 3 times
Joined: Wed Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson

Re: Remove VM's that no longer exist

Veeam Logoby decoy5657 » Thu Oct 17, 2013 5:29 pm

Code: Select all
$OldObjects = Compare-Object -PassThru $(Get-VBRJobObject -Job "Production") $(Get-Cluster "Production" | Get-VM) -Property Name | Where-Object {$_.SideIndicator -like "<="}
if ($OldObjects -eq $null) {Exit}

Foreach ($OldObject in $OldObjects)
{
Get-VBRJob -name "Production" | Get-VBRJobObject -Name $OldObject.Name | Remove-VBRJobObject -Completely
}
decoy5657
Influencer
 
Posts: 12
Liked: 3 times
Joined: Wed Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson

Re: Remove VM's that no longer exist

Veeam Logoby decoy5657 » Thu Oct 17, 2013 6:02 pm 1 person likes this post

Here's one with a better exit loop in case you want to run more code

Code: Select all
$OldProdObjects = Compare-Object -PassThru $(Get-VBRJobObject -Job "Production") $(Get-Cluster "Production" | Get-VM) -Property Name | Where-Object {$_.SideIndicator -like "<="}
if ($OldProdObjects -eq $null) {Exit}
else {
   Foreach ($OldProdObject in $OldProdObjects)
   {
   Get-VBRJob -name "Production" | Get-VBRJobObject -Name $OldProdObject.Name | Remove-VBRJobObject -Completely
   }
   }
decoy5657
Influencer
 
Posts: 12
Liked: 3 times
Joined: Wed Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson

Re: Remove VM's that no longer exist

Veeam Logoby v.Eremin » Fri Oct 18, 2013 9:22 am

I didn’t have an access to console at that moment to take a look at this, but you solved it greatly. Thanks.
v.Eremin
Veeam Software
 
Posts: 13255
Liked: 968 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin

Re: Remove VM's that no longer exist

Veeam Logoby decoy5657 » Fri Oct 18, 2013 1:44 pm 1 person likes this post

Here's the entire script I came up with.

Code: Select all
Add-PSSnapin VMware.VimAutomation.Core
Add-PSSnapin VeeamPSSnapin
Connect-VIServer LouPrMgt011.domain.lan -User 'user' -Password 'password'

### ------------------------------ ADD OBJECTS TO JOB ------------------------------ ###

### Dev cluster
$DevClusterVMsToAdd = Get-Cluster "Development" | Get-VM | Where-Object {$_.Name -notlike "*b" -and $_.Name -notlike "*dvsql*"}
Add-VBRJobObject -Job "Development" -Server LouPrMgt011.domain.lan -Objects $DevClusterVMsToAdd

### Production cluster
$ProductionClusterVMsToAdd = Get-Cluster "Production" | Get-VM | Where-Object {$_.Name -notlike "*b"}
Add-VBRJobObject -Job "Production" -Server LouPrMgt011.domain.lan -Objects $ProductionClusterVMsToAdd

### SQL
$SQLVMsToAdd = Get-Cluster "Development" | Get-VM | Where-Object {$_.Name -like "*dvsql*"}
Add-VBRJobObject -Job "SQL" -Server LouPrMgt011.domain.lan -Objects $SQLVMsToAdd


### ----------------------- REMOVE UNWANTED OBJECTS FROM JOB ----------------------- ###

### Manual exclusions for Production job
$ProdExclusions = @(
"LouPrAudit001",
"LouPrAudit002",
"LouPrGMS001"
)
Foreach ($ProdExclusion in $ProdExclusions)
{
Get-VBRJob -name "Production" | Get-VBRJobObject -Name $ProdExclusion | Remove-VBRJobObject -Completely
}
### Exclude SQL from Development job
$DevSqlBoxes = Get-VBRJob -name "Development" | Get-VBRJobObject  | Where-Object {$_.Name -like "*dvsql*"}

if ($DevSqlBoxes -eq $null) {}
else {
   Foreach ($DevSqlBox in $DevSqlBoxes)
      {
      $DevSqlBox | Remove-VBRJobObject -completely
      }
   }


### Manual exclusions for Development job
$DevExclusions = @(
"Analytics VM",
"UI VM"
)
Foreach ($DevExclusion in $DevExclusions)
{
Get-VBRJob -name "Development" | Get-VBRJobObject -Name $DevExclusion | Remove-VBRJobObject -completely
}


### ----------------------- REMOVE STALE OBJECTS FROM JOBS ----------------------- ###

### Production

$OldProdObjects = Compare-Object -PassThru $(Get-VBRJobObject -Job "Production") $(Get-Cluster "Production" | Get-VM) -Property Name | Where-Object {$_.SideIndicator -like "<="}
if ($OldProdObjects -eq $null) {}
else {
   Foreach ($OldProdObject in $OldProdObjects)
   {
   Get-VBRJob -name "Production" | Get-VBRJobObject -Name $OldProdObject.Name | Remove-VBRJobObject -Completely
   }
   }

### Dev
$OldDevObjects = Compare-Object -PassThru $(Get-VBRJobObject -Job "Development") $(Get-Cluster "Development" | Get-VM) -Property Name | Where-Object {$_.SideIndicator -like "<="}
if ($OldDevObjects -eq $null) {}
else {
   Foreach ($OldDevObject in $OldDevObjects)
   {
   Get-VBRJob -name "Development" | Get-VBRJobObject -Name $OldDevObject.Name | Remove-VBRJobObject -Completely
   }
   }














decoy5657
Influencer
 
Posts: 12
Liked: 3 times
Joined: Wed Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson


Return to PowerShell



Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests