0

I have two PS scripts, one reads from a list of computers on Sharepoint and outputs the results and the other outputs a list of computers from Active Directory.

What I need to do is somehow compare both of these lists to see: a) If list A has computers not listed on list B and b) If list B has computers not listed on list A

I'm assuming this may be possible using the Compare-Object cmdlet, and using arrays, but I'm not overly familiar with PS and not sure where to go from here.

To get the list of computers from AD I use:

Import-Module ActiveDirectory

$DCServer = "DC1.global"
$Searchbase = "OU=World,DC=global"

$list = Get-ADComputer -Server $DCServer -searchbase $Searchbase -Filter * -Property *

foreach($item in $list) {
    write-host $item["Name"]
}

$Count = (Get-ADComputer -Server $DCServer -searchbase $Searchbase -Filter * -Property *).count
write-host "Total computers ="$count

To get the list of computers from Sharepoint I use:

Import-Module -DisableNameChecking "C:\Program Files (x86)\SharePointPnPPowerShellOnline\Modules\SharePointPnPPowerShellOnline"

$cred = Get_credential

connect-pnponline "https://domain.sharepoint.com/sites/Team" -credential $cred

$list = get-pnplistitem -list "HIVE_Devices"

foreach($item in $list)
{
  write-host $item["Title"]
}
notw86
  • 13
  • 2

2 Answers2

1

You search something like that?

$ListA = "PC-1","PC-2","PC-3","PC-4","PC-5","PC-6","PC-7","PC-8","PC-9","PC-10","PC-11"
$ListB = "PC-1","PC-2","PC-3","PC-40","PC-55","PC-6","PC-70","PC-8","PC-9","PC-10","PC-11"

$ListDiffrent = @(Compare-Object -ReferenceObject $ListA -DifferenceObject $ListB).InputObject

ListDiffrent Output:

PC-40
PC-55
PC-70
PC-4
PC-5
PC-7

You have to edit your script. Instead of writing the clients to the host, create an array

Replace: write-host $item["Name"] with $ArrayList.Add($item["Name") | out-null (Note: you have to define the array list out of the foreach loop)

Now create a new script. In this script you can add your scripts as a function with the arraylist as returnvalue(Return $ArrayList) or you can invoke your script.

After that call your function/script and store the returnvalue in a variable and then use the compare cmdlet

guiwhatsthat
  • 2,349
  • 1
  • 12
  • 24
  • That's pretty much what I'm after, except I don't want to input all the computers manually into the array, I need them to come from the results of the two different PS scripts - one for the Sharepoint list and one from Active Directory... Does that make sense? I'm not sure how to get those results into the array. – notw86 Jun 23 '17 at 12:09
0

Both of your variables are $list, and they are in different scripts. For a start you'll need to use different variables.

PowerShell is good at dealing with objects without you having to explicitly create arrays/lists.

Try:

$adList = Get-ADComputer -Server $DCServer -searchbase $Searchbase -Filter * -Property *
$spList = get-pnplistitem -list "HIVE_Devices"

$diff = Compare-Object $adList.Name $spList.Title

I would not recommend dot indexing into .InputObject straight away if you would like to tell which list the properties are in.

G42
  • 9,791
  • 2
  • 19
  • 34