AdhocAdam / smletsexchangeconnector

SMLets PowerShell based Exchange Connector for controlling Microsoft System Center Service Manager 2016+
https://adhocadam.github.io/smletsexchangeconnector/
GNU General Public License v3.0
29 stars 19 forks source link

Check for existing user in New-CMDBUser #470

Open AdhocAdam opened 1 year ago

AdhocAdam commented 1 year ago

Describe the change
New-CMDBUser is called when an e-mail address cannot be found in SCSM, but if the username/domain combo already exists in SCSM but without the mail address the user creation will fail without an error message. This has happened to me a couple of times now, and is kind of hard to detect.

Added a simple check to see if the user object already exists before creation, and if the creation should fail for some reason, it will be logged as an error.

AdhocAdam commented 1 year ago

I've been doing some extensive testing of this proposed change @alexaxb. I had a question/scenario that I'm not sure needs to be handled, but seems worth bringing up if we're here working on this. For the sake of example, let's imagine a run of the connector processing a single email.

  1. Get-SCSMUserByEmailAddress engages. user@lab.lcl cannot be matched to a related Domain User object
  2. New-CMDBUser engages. The user is found, but they don't have a related Notification Channel object. The Notification Channel is created and the email (target) address is set
  3. Connector carries on and ultimately finishes up

If the connector runs again looking for the same address, it should correctly be able to identify the user via Get-SCSMUserByEmailAddress and thus, never engage New-CMDBUser. I point this out as in my testing, if I try to create the user again using the proposed changes in this PR it throws an error that isn't handled because the Notification Channel object already exists e.g. "New-SCSMObjectProjection : A discovery data item was rejected because the item has already been inserted by another user or process." This is probably okay, because it doesn't seem like a path that could ever be logically executed in the connector.

But this led me to a question, have you ever experienced a scenario wherein the User object exists, the Notification Channel object exists/is related, but the Notification Channel does not have the Email/Target address set? Because if so, it means the above scenario could be executed and thus, should be handled.

alexaxb commented 1 year ago

I would say no. Can't remember that I've seen a user with an Notification Channel with empty address. If the user is missing an address, the usual would be that the Notification Channel object would also be missing.

pwahlmueller commented 1 year ago

We experience the same issue. We can reproduce it:

  1. Send E-Mail with new User to Service Manager
  2. SMLets Exchange Connector creates the user without the notification
  3. Sending an E-Mail again results in the error.
AdhocAdam commented 1 year ago

Can you reproduce on demand @pwahlmueller? If so, it'd be great to get some PowerShell debugging events by running the connector manually (ISE or VS Code) and see what (if any) kind of errors are thrown.

pwahlmueller commented 1 year ago

It reproduces every time, but don' exactly know why. When I copy the "New-CMDBUser" code to a script only generating user + email, it works as it should. I'll try the debugging this afternoon.

We use the connector in System Center Orchestrator.

pwahlmueller commented 1 year ago

So this is the error message. I think I have the solution. Will provide it.

New-SCSMObjectProjection : A discovery data item was rejected because the item is already bound to another Membership relationship.
At D:\_SCOWorkingDir\smlets connector ursprung.ps1:2795 char:32
+ ... ification = New-SCSMObjectProjection -Type "$($userHasPrefProjection. ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Enter...bjectProjection:EnterpriseManagementObjectProjection) [New-SCSMObjectProjection], DiscoveryDataLifetimeDependencyException
    + FullyQualifiedErrorId : projection commit failure,SMLets.NewSCSMObjectProjectionCommand
pwahlmueller commented 1 year ago

So think I'd to add a new PR for my changes, see https://github.com/AdhocAdam/smletsexchangeconnector/pull/477

pwahlmueller commented 1 year ago

If you want to do

It is recommended to use the verb Set for the function instead of new.