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 (215k 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

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 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)
0 votes
1 answer

I am working on a custom deprovision task and have the need to delete a directory on a file server (not a profile directory) and cannot find a function in ... like the following path: \\servername\datafiles\hou\UserName\MyDocuments\* Thanks in advance.

asked Feb 9, 2015 by stuarts24 (50 points)
0 votes
1 answer

Hi, I need to delete a Business Unit. Is it correct that deleting a Business Unit will not delete anything from Active Directory?

asked Sep 6, 2011 by abdutal (110 points)
0 votes
1 answer

Hi, I'm probably over thinking this, so I'm hoping to get some clarity. But we've had an issue for a while and I can't get my head around it. When we create a ... exchange isn't needed for most of our environment now that we're Windows 10/11. Thanks, Gary

asked Sep 16 by gazoco (430 points)
2,807 questions
2,541 answers
6,615 comments
65,368 users