We have a rule implemented where if any user gets deprovisioned this rule deletes the home directory but in some cases where Directory is large it throws error. Please help to fix this
HOME_Directory_Delete.txt (2.3 KB)
We can now determine what todo with these attributes
if ($resultObject.Errors.count -eq 0) {
Write-Output "Calculated the new DN as: " $newNativeIdentity | out-file .\ConnectorAfterDeleteResult.xml -append;
Write-Output "Existing DN is: " $nativeIdentity | out-file .\ConnectorAfterDeleteResult.xml -append;
$user = Get-ADUSer $nativeIdentity -properties homedirectory;
$newUser = Get-ADUser $newNativeIdentity -properties homedirectory;
if ($user) {
Write-Output "Found user with old DN: " $user | out-file .\ConnectorAfterDeleteResult.xml -append;
$currentHomeDirectory = $user.homeDirectory;
$sAMAccountName = $user.sAMAccountName;
#Write-Output "sAMAccountName user" $sAMAccountName | out-file .\EnableMailboxPS.log -append;
}
if ($newUser) {
$user = $newUser;
Write-Output "Found user with new DN: " $newUser | out-file .\ConnectorAfterDeleteResult.xml -append;
$currentHomeDirectory = $newUser.homeDirectory;
$sAMAccountName = $newUser.sAMAccountName;
#Write-Output "sAMAccountName newuser" $sAMAccountName | out-file .\EnableMailboxPS.log -append;
}
$date = get-date -Format "MM_dd_yyyy";
$path = ""
$fileName = $sAMAccountName+"_AfterDeletePS.log";
If(!(test-path $path))
{
New-Item -ItemType File -Force -Path $path -Name $fileName
}
$filePath = $path + "\"+$fileName
}
$homeDirectoryPath = \\sailpointtest\;
param (
[string]$Username
)
# Validate the input
if (-not $Username) {
Write-Host "Usage: .\Delete-HomeDirectory.ps1 -Username <username>"
exit;
}
$HomeDir = "\\sailpointtest\Test"
# Check if the directory exists
if (Test-Path $HomeDir) {
Write-Output "Deleting home directory: $HomeDir" | out-file .\ConnectorAfterDeleteResult.xml -append;
# Start a background job to delete the directory
Start-Job -ScriptBlock {
param ($path)
Remove-Item -Path $path -Recurse -Force
} -ArgumentList $HomeDir
}
Write-Output "Deletion of $HomeDir initiated as a background job." | out-file .\ConnectorAfterDeleteResult.xml -append;
} else {
Write-Output "Error: Home directory $HomeDir does not exist." | out-file .\ConnectorAfterDeleteResult.xml -append;
exit;
can you share the error?
Ps next time open the topic in Powershell Space
The error being that the task cannot be completed or Connection Reset
@Himanshu_singh03 -
The error you’re encountering—“The task cannot be completed” or “Connection Reset”—is common when using Remove-Item over a network path to delete large directories. This happens because Remove-Item is not optimized for network operations involving large amounts of data. Network timeouts, insufficient permissions, or resource limitations can cause the deletion process to fail.
Solution Overview:
To resolve this issue, you can use a more efficient and robust method for deleting large directories over a network:
Use robocopy to Mirror an Empty Directory:
robocopy is designed for efficient file operations and can handle large directories more reliably.
Step 1: Create an Empty Directory
$EmptyFolder = "C:\EmptyFolder"
if (-not (Test-Path $EmptyFolder)) {
New-Item -ItemType Directory -Path $EmptyFolder
}
Step 2: Use robocopy to Mirror the Empty Directory
$HomeDir = "\\sailpointtest\Test"
Write-Output "Deleting home directory: $HomeDir using robocopy" | Out-File .\ConnectorAfterDeleteResult.log -Append
$cmd = "robocopy `"$EmptyFolder`" `"$HomeDir`" /MIR /R:1 /W:1"
Invoke-Expression $cmd
Write-Output "Deletion of $HomeDir completed using robocopy." | Out-File .\ConnectorAfterDeleteResult.log -Append
Explanation:
robocopy mirrors the empty directory to the target directory, effectively deleting all files and subdirectories.
The /MIR switch mirrors the directory tree.
The /R:1 and /W:1 switches minimize retries and wait time.
Example: Modified Script Using robocopy
# Parameters and initial checks
param (
[string]$Username
)
if (-not $Username) {
Write-Host "Usage: .\Delete-HomeDirectory.ps1 -Username <username>"
exit
}
# Define paths
$HomeDir = "\\sailpointtest\Test\$Username"
$EmptyFolder = "C:\EmptyFolder"
# Create EmptyFolder if it doesn't exist
if (-not (Test-Path $EmptyFolder)) {
New-Item -ItemType Directory -Path $EmptyFolder
}
# Check if the home directory exists
if (Test-Path $HomeDir) {
Write-Output "Deleting home directory: $HomeDir using robocopy" | Out-File .\ConnectorAfterDeleteResult.log -Append
# Use robocopy to mirror the empty directory to the home directory
$cmd = "robocopy `"$EmptyFolder`" `"$HomeDir`" /MIR /R:1 /W:1"
Invoke-Expression $cmd
Write-Output "Deletion of $HomeDir completed using robocopy." | Out-File .\ConnectorAfterDeleteResult.log -Append
} else {
Write-Output "Error: Home directory $HomeDir does not exist." | Out-File .\ConnectorAfterDeleteResult.log -Append
exit
}
Note: Replace \\sailpointtest\Test\$Username with the actual path to the user’s home directory.
Hope this helps!!
Although this is PowerShell and I appreciate the Spaces plug, I’m okay with this being here since it’s still identityiq related