Closed SAERXCIT closed 3 years ago
That's awesome! You are finding all the bugs in my code! 🙂
This one was easy to fix though. Here is what I did:
$ResultItem = $CurrentTask.PsObject.Copy()
$ResultItem | Add-Member -MemberType "NoteProperty" -Name "ModifiablePath" -Value $_.ModifiablePath
$ResultItem | Add-Member -MemberType "NoteProperty" -Name "IdentityReference" -Value $_.IdentityReference
$ResultItem | Add-Member -MemberType "NoteProperty" -Name "Permissions" -Value $_.Permissions
$ResultItem
Rather than modifying the source object, I create a copy of it and I add the relevant fields to the object's copy. I made sure that it is still PSv2 compatible because I haven't used this trick before. In the other checks, I created a new object for each new result. I don't know why I didn't do that in these two checks.
Anyway, thanks a lot! 🙂
And, yeah the -Force
would have been a quick fix but it could have some side effects that are difficult to foresee. So, I prefer to play safe here and simply make a copy of the object. Perhaps it's actually what -Force
does under the hood... 🙄 I don't know.
Great, thanks for fixing!
I saw Copy()
but did not want to implement it because its documentation page mentions it only applies to PSv5, but since you actually tried and it works, it means the MS doc is not complete (or I misunderstood something :upside_down_face:).
Closing the issue, cheers!
Yup, we read the same doc! 😉
I think that PSv5.1 is now considered as the minimum PS version in the entire documentation.
But a Copy()
seemed like a very basic operation that you would expect even in older versions.
So, I tested it by copying a random object in PSv2 and it worked! 🙂
I double checked on Windows Server 2008 R2 as well.
Hi !
The scheduled tasks checks (
Invoke-ScheduledTasksImagePermissionsCheck
,Invoke-ScheduledTasksUnquotedPathCheck
) do not show correct results when multiple results exist for the same task: the firstAdd-Member
s work as expected, but furtherAdd-Member
s for subsequent results of the sameCurrentTask
do not work, as the functionAdd-Member
does not allow overwriting by default. This results in theModifiablePath
,IdentityReference
, andPermissions
elements not being updated (and errors in the console), which for the user means the same result is outputted multiple times.Example on an up-to-date 20H2, PSv5 (same
ModifiablePath
,IdentityReference
, andPermissions
for both items; check was executed as a privileged user for demonstration purposes, so technically not a false positive :slightly_smiling_face:):Proposed solution: adding
-Force
to eachAdd-Member
of both these checks. Example output (the attributes are updated, and no more error outputted):Cheers! :slightly_smiling_face: