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

Remove "Resolved By User" Relationship when WorkItem is reactivated #467

Closed SimonZein closed 1 year ago

SimonZein commented 1 year ago

When the Undo-WorkItemResolution is called, the Resolved By User Relationship remains. Therefor additional code is added to the function

AdhocAdam commented 1 year ago

Been doing some testing of this and it certainly works as expected. I had no doubt 😄.

And I was considering simplifying the catch statement down to a single line by removing

$logMessage = $_.Exception.Message

and then just writing Exception into the event directly. But then it got me thinking, "What are the conditions the CATCH would even engage?" So for the sake of argument I proceded to:

And to my surprise, the CATCH will never engage in any of these scenarios. Nor will it produce any output. In fact, the only time the CATCH will engage is if the Work Item doesn't exist/cant be found. Which all things considered, should be impossible to get here with a Work Item that doesn't exist because the function Update-WorkItem is only way in with a total of 5 references within that function via Incidents/Problems. So that said, while the try/catch certainly would have been my default line of thinking as well, it appears it doesn't need to be used at all! Which means your suggestion can actually be executed in a single line:

Get-SCSMRelationshipObject -BySource $workItem -Filter "RelationshipId -eq '$($workResolvedByUserRelClass.Id)'" @scsmMGMTParams | Remove-SCSMRelationshipObject

It's also worth adding, I think the reason I never thought to test this scenario until now is because Remove-SCSMRelationshipObject is not used anywhere in the connector.

But before any changes are made to your branch/this pull request, I figured I'd put this here first just to bounce it off you in the event there is something I'm overlooking or if your experience is different.

SimonZein commented 1 year ago

Hello Adam, nice to hear this one works as expected. Yes the thing with the relationships...In my scripts, and I really don't know why I didn't add it to my function here, I always pipeline the relationships via

| ? {$_.IsDeleted -eq $false}

Maybe this is something worth adding, so even if the Get... returns more than one relationship, it would delete the actual one :)

AdhocAdam commented 1 year ago

I was also surprised by that, because I too have used

| ? {$_.IsDeleted -eq $false}

when filtering/looking through relationships. But in the case of passing it to a Remove-SCSMRelationshipObject, it looks to be unnecessary. It just wants a relationship object/objects and then attempts to remove it. In which case, it looks like it/the SCSM SDK might handle a lot of this.