vmwarelicense12
Enthusiast
Enthusiast

Remove vm objects from array

Jump to solution

How to I remove all $excluededVms from $vms??

[System.Collections.ArrayList]$excluededVms = get-content E:\scripts\delSnapshots\ExceptionsList.txt

[System.Collections.ArrayList]$vms = get-vm | Select-Object Name

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

In that case, can't you just do?

$excludedVMs = Get-Content -Path 'E:\scripts\delSnapshots\ExceptionsList.txt'

$vms = Get-VM | where{$excludedVMs -notcontains $_.Name} | Select -ExpandProperty Name


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

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership

You could do something like this

# Txt files contains VM names, 1 per line

# Example

# vm1

# vm2

# vm3


$excludedVMs = New-Object System.Collections.ArrayList($null)

Get-Content -Path .\vmnames.txt | ForEach-Object -Process {

    $excludedVMs.Add($_) | Out-Null

}

$excludedVMs

$excludedVMs.Remove('vm2')

$excludedVMs


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

0 Kudos
vmwarelicense12
Enthusiast
Enthusiast

Hi LucD,

As far as I can see, you remove from $excludedVMs list, but they must be removed from $vms list.

This works for me: $excludedVMs.Remove("xxxx")

This do not work for me: $vms.Remove("xxx")

I want vm´s from $excludedVMs do be removed from $vms.

0 Kudos
LucD
Leadership
Leadership

In that case, can't you just do?

$excludedVMs = Get-Content -Path 'E:\scripts\delSnapshots\ExceptionsList.txt'

$vms = Get-VM | where{$excludedVMs -notcontains $_.Name} | Select -ExpandProperty Name


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

View solution in original post

0 Kudos