VMware Cloud Community
vmwarelicense12
Enthusiast
Enthusiast
Jump to solution

Remove vm objects from array

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
vmwarelicense12
Enthusiast
Enthusiast
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos