0 votes

I need to delete several user shares when I delete AD accounts (more than just a simple home drive deletion, we also need to delete terminal services profiles, and other things for custom apps). I have created some custom commands that as simple powershell scripts that run the following code

Remove-Item \\server\share\%username% -Force -Confirm:$False

As a part of the AD Account deletion scheduled task, I have set these custom commands to run as a part of the task.

Everything in the account deletion process works, except for these custom commands. I think the problem may have to do with the fact that the account is deleted before these commands are run, therefore the commands don't work because they don't recognize the user account (since it is no longer present).

The adaxes activity history shows a failed status for the cusom command, and the execution log shows "Could not find a part of the path \\server\share\'.".

It's seems like it's leaving the %username% field empty. These custom commands work just fine when I test them by themselves with sample accounts.

Thoughts?

by (100 points)

1 Answer

0 votes
by (216k points)
selected by
Best answer

Hello,

When a Scheduled Task is run on a user, value references contained in all the actions of the Scheduled Task are resolved before performing any actions. However, if a Scheduled Task launches any Custom Commands, value references are resolved for each Custom Command separately before executing the Custom Command. Since Custom Commands are executed after deleting a user, the account does not exist already, and the %username% value reference cannot be resolved.

To remedy the issue, you can execute the PowerShell scripts directly from the Scheduled Task by adding to the Task the Run a program or PowerShell script actions launching the necessary scripts. In this case, the %username% value reference in your scripts will be resolved before the Scheduled Task performs any actions. To insert the necessary Run a program or PowerShell script actions to your Scheduled Task, you may just copy and paste them from your Custom Commands.

0

Perfect, I'll give that a try and report back! Thanks!

EDIT: Perfect, worked like a charm! Thanks!

Related questions

0 votes
1 answer

Hi, is it possible to export automatically the exchange online mailbox of a user to a .PST file on our archive server before the user is deleted? Kind regards, Fabian

asked Oct 26, 2023 by fabian.p (150 points)
0 votes
1 answer

Hello, Before deleting a user, how can I create a PDF with their details and a sentence confirming that the user has been deleted? And then save this document on a server. is this solvable via code? thanks

asked Jul 13, 2023 by DRiVSSi (240 points)
0 votes
1 answer

Currently we have to create lots of new NTFS shared folders for projects and manage the addition and removal of access for users on an ongoing basis. We would like to delegate ... interested to know if it will meet our needs before I install a demo to test.

asked Dec 21, 2021 by darrenbrown (20 points)
0 votes
1 answer

I created a group Business Rule that triggers "After adding or removing a member from a group". On its Activity Scope I added a test group, and set it for "The group ... does not trigger. What should I do to make the BR detect this (admittedly rare) case?

asked Mar 16, 2023 by alex.vanderwoude (60 points)
0 votes
1 answer

I have a PowerShell Script (being run in a Custom Command) that creates a Scheduled Task that runs another Custom Command but I want the resulting Scheduled ... Exclude = $False $scopeItem.SetInfo() $task.ActivityScopeItems.Add($scopeItem) $task.SetInfo() }

asked Apr 1, 2021 by Staj (350 points)
3,326 questions
3,026 answers
7,727 comments
544,678 users