I am trying to write some code that will update a contact list using EWS API.
\nI grab the list of users using ADSI searcher, then I pull the current contacts from a mailbox using EWS API.<\/p>\n
Next I use LINQ to compare the differences. I see if there are users that need to be imported as well as any users that need to be deleted because they no longer exist.<\/p>\n
Here is my code (see Below)<\/p>\n
Everything works above, but the trouble I am running in to no is the balues that are stored in $NeedsToBeAdded and $NeedsToBeDeleted I need to do stuff with. Th issue is, that these values only hold the user email address, and I need to process an import using the contact ID.<\/p>\n
So then I try do a foreach-object {} loop and something like this: (See Below)<\/p>\n
(Keep in mind I use Get as the method just for output), once it works, I would change the method to Delete to process the delete.
\nNow the code above I use should be able to get me that contacts ID info based on the output and value(s) stored in $NeedsToBeDeleted, however for some reason it prints out several users that don’t even match that filter.<\/p>\n
For example if I want to filter out with using a user ID like billy bob. I changed a couple of values, but this is the layout of the contact Output when I output everything without any filtering.<\/p>\n
I hope this makes sense and hopefully someone can assist me please. Thank you.<\/p>\n
id : someLongID\ncreatedDateTime : 2021-06-02T02:54:47Z\nlastModifiedDateTime : 2021-06-02T02:55:11Z\nchangeKey : changeKEY\ncategories : {}\nparentFolderId : parentfolderID\nbirthday : \nfileAs : Bob, Billy\ndisplayName : Billy Bob\ngivenName : Billy\ninitials : B.B.\nmiddleName : \nnickName : \nsurname : Bob\ntitle : \nyomiGivenName : \nyomiSurname : \nyomiCompanyName : \ngeneration : \nimAddresses : {}\njobTitle : \ncompanyName : \ndepartment : \nofficeLocation : \nprofession : \nbusinessHomePage : \nassistantName : \nmanager : \nhomePhones : {}\nmobilePhone : \nbusinessPhones : {}\nspouseName : \npersonalNotes : \nchildren : {}\nemailAddresses : {@{name=Billy Bob ([email protected]); [email protected]}}\nhomeAddress : \nbusinessAddress : \notherAddress : \n<\/code><\/pre>\n#Get Users\n\n$objSearcher=[adsisearcher]\"\"\n$objSearcher.Sort.PropertyName = \"sn\"\n$objSearcher.Filter = \"(&(extensionAttribute2=DynamicDistro)(|(mobile=*)(telephonenumber=*)))\"\n$colProplist = \"extensionattribute2\",\"initials\",\"mobile\",\"telephonenumber\",\"GivenName\",\"sn\",\"DisplayName\",\"Company\",\"title\",\"mail\",\"department\",\"photo\",\"samaccountname\"\nforeach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i) | out-null }\n$Users = $objSearcher.FindAll()\n\n#Get Contacts\n\n$Contacts = Invoke-RestMethod -Uri \"https://graph.microsoft.com/v1.0/users/$UPN/contactFolders/$folderId/contacts?`$top=999\" -Headers $headers -Method Get\n\n#Create 2 variables\n\n#Store User Emails\n$A = $($Users.properties.mail)\n#Store Contact Emails\n$B = $($Contacts.value.emailAddresses::address)\n\n<#\nCheck differences Between the 2\nSee if there are any \"new\" users that need to be added\nSee if there are any users that need to be deleted because they no longer exist in Add\n#>\n\n#Displays what accounts we need to import\n$NeedsToBeAdded = [string[]]([Linq.Enumerable]::Except([object[]]$a, [object[]]$b))\n\n#Displays what acocunts we need to delete because they no longer exist\n$NeedsToBeDeleted = [string[]]([Linq.Enumerable]::Except([object[]]$b, [object[]]$a))\n<\/code><\/pre>","upvoteCount":6,"answerCount":4,"datePublished":"2021-06-02T13:29:03.000Z","author":{"@type":"Person","name":"spiceuser-ih3sy","url":"https://community.spiceworks.com/u/spiceuser-ih3sy"},"suggestedAnswer":[{"@type":"Answer","text":"I am trying to write some code that will update a contact list using EWS API.
\nI grab the list of users using ADSI searcher, then I pull the current contacts from a mailbox using EWS API.<\/p>\n
Next I use LINQ to compare the differences. I see if there are users that need to be imported as well as any users that need to be deleted because they no longer exist.<\/p>\n
Here is my code (see Below)<\/p>\n
Everything works above, but the trouble I am running in to no is the balues that are stored in $NeedsToBeAdded and $NeedsToBeDeleted I need to do stuff with. Th issue is, that these values only hold the user email address, and I need to process an import using the contact ID.<\/p>\n
So then I try do a foreach-object {} loop and something like this: (See Below)<\/p>\n
(Keep in mind I use Get as the method just for output), once it works, I would change the method to Delete to process the delete.
\nNow the code above I use should be able to get me that contacts ID info based on the output and value(s) stored in $NeedsToBeDeleted, however for some reason it prints out several users that don’t even match that filter.<\/p>\n
For example if I want to filter out with using a user ID like billy bob. I changed a couple of values, but this is the layout of the contact Output when I output everything without any filtering.<\/p>\n
I hope this makes sense and hopefully someone can assist me please. Thank you.<\/p>\n
id : someLongID\ncreatedDateTime : 2021-06-02T02:54:47Z\nlastModifiedDateTime : 2021-06-02T02:55:11Z\nchangeKey : changeKEY\ncategories : {}\nparentFolderId : parentfolderID\nbirthday : \nfileAs : Bob, Billy\ndisplayName : Billy Bob\ngivenName : Billy\ninitials : B.B.\nmiddleName : \nnickName : \nsurname : Bob\ntitle : \nyomiGivenName : \nyomiSurname : \nyomiCompanyName : \ngeneration : \nimAddresses : {}\njobTitle : \ncompanyName : \ndepartment : \nofficeLocation : \nprofession : \nbusinessHomePage : \nassistantName : \nmanager : \nhomePhones : {}\nmobilePhone : \nbusinessPhones : {}\nspouseName : \npersonalNotes : \nchildren : {}\nemailAddresses : {@{name=Billy Bob ([email protected]); [email protected]}}\nhomeAddress : \nbusinessAddress : \notherAddress : \n<\/code><\/pre>\n#Get Users\n\n$objSearcher=[adsisearcher]\"\"\n$objSearcher.Sort.PropertyName = \"sn\"\n$objSearcher.Filter = \"(&(extensionAttribute2=DynamicDistro)(|(mobile=*)(telephonenumber=*)))\"\n$colProplist = \"extensionattribute2\",\"initials\",\"mobile\",\"telephonenumber\",\"GivenName\",\"sn\",\"DisplayName\",\"Company\",\"title\",\"mail\",\"department\",\"photo\",\"samaccountname\"\nforeach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i) | out-null }\n$Users = $objSearcher.FindAll()\n\n#Get Contacts\n\n$Contacts = Invoke-RestMethod -Uri \"https://graph.microsoft.com/v1.0/users/$UPN/contactFolders/$folderId/contacts?`$top=999\" -Headers $headers -Method Get\n\n#Create 2 variables\n\n#Store User Emails\n$A = $($Users.properties.mail)\n#Store Contact Emails\n$B = $($Contacts.value.emailAddresses::address)\n\n<#\nCheck differences Between the 2\nSee if there are any \"new\" users that need to be added\nSee if there are any users that need to be deleted because they no longer exist in Add\n#>\n\n#Displays what accounts we need to import\n$NeedsToBeAdded = [string[]]([Linq.Enumerable]::Except([object[]]$a, [object[]]$b))\n\n#Displays what acocunts we need to delete because they no longer exist\n$NeedsToBeDeleted = [string[]]([Linq.Enumerable]::Except([object[]]$b, [object[]]$a))\n<\/code><\/pre>","upvoteCount":6,"datePublished":"2021-06-02T13:29:04.000Z","url":"https://community.spiceworks.com/t/powershell-syntax-issue-using-linq/801581/1","author":{"@type":"Person","name":"spiceuser-ih3sy","url":"https://community.spiceworks.com/u/spiceuser-ih3sy"}},{"@type":"Answer","text":"If you post code, please use the ‘Insert Code’ button. Please and thank you!<\/p>\n