-1

Hey guys I have a little problem. I'm doing a Script where I get all the Servers in our Activ Directory Server and then compare them with all the servers that are in our ESX Servers. I want to find out which Servers are in the Active Directory that are not in the ESX Array. Both Arays have the Option "name" with what I'm trying to compair the two Arrays.

I tried doing it with foreach but couldn't get it to work. Can somebody help?

Forestbird
  • 33
  • 7
  • `@(Compare-Object $AdServers $EsxServers).Count -eq 0` If this returns `$true` both arrays have the same elements – Theo Jun 22 '22 at 11:32
  • Please take some time to read [how to ask](https://stackoverflow.com/help/how-to-ask) and check for similar questions like: [powershell compare arrays](https://stackoverflow.com/search?q=%5Bpowershell%5D+compare+arrays) – iRon Jun 22 '22 at 11:36
  • @Theo Ah Okay thanks for the Comment. But I would like to have the Servers that are not in both Arrays as output in the Commandline – Forestbird Jun 22 '22 at 11:37
  • Then do `Compare-Object $AdServers $EsxServers -PassThru` – Theo Jun 22 '22 at 11:42
  • See also: [Compare A and B list and create a C list with B values not in A list in Powershell](https://stackoverflow.com/q/66887066/1701026) – iRon Jun 22 '22 at 12:02

1 Answers1

1
$a = 1..5
$b = 4..8

Compare-Object -ReferenceObject $a -DifferenceObject $b | Where-Object{$_ -match "<="}
Narayana Lvsl
  • 315
  • 2
  • 10
  • This only returns elements that are in array `$a` but not in array `$b`, not vice-versa – Theo Jun 22 '22 at 11:43
  • ```$a = 1..5 $b = 4..8 Compare-Object -ReferenceObject $a -DifferenceObject $b ``` – Narayana Lvsl Jun 22 '22 at 11:45
  • 1
    Thank you very much that works perfectly for me. @Theo I don't need it the otherway arround aswell, cause we also have Linux Servers which are not in the AD. – Forestbird Jun 22 '22 at 11:46
  • 1
    No, you still need switch `-PassThru` to obtain an array of just the server names – Theo Jun 22 '22 at 11:46