PowerShell script exchange
Post Reply
decoy5657
Influencer
Posts: 12
Liked: 3 times
Joined: Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson
Contact:

Remove VM's that no longer exist

Post by decoy5657 »

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?
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Remove VM's that no longer exist

Post by veremin »

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.
decoy5657
Influencer
Posts: 12
Liked: 3 times
Joined: Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson
Contact:

Re: Remove VM's that no longer exist

Post by decoy5657 »

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

Re: Remove VM's that no longer exist

Post by decoy5657 »

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: Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson
Contact:

Re: Remove VM's that no longer exist

Post by decoy5657 » 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
	}
	}
veremin
Product Manager
Posts: 20415
Liked: 2302 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Remove VM's that no longer exist

Post by veremin »

I didn’t have an access to console at that moment to take a look at this, but you solved it greatly. Thanks.
decoy5657
Influencer
Posts: 12
Liked: 3 times
Joined: Oct 09, 2013 2:45 pm
Full Name: Aaron Anderson
Contact:

Re: Remove VM's that no longer exist

Post by decoy5657 » 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
	}
	}














Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests