Delete Folders from Multiple Servers

Team,

I’m trying to delete folders from multiple Backup servers (SQL Servers). Folder names are Server Names itself .

When I use below script remotely to delete folders, I get access denied error. Could you please help me why this code is not working

$BackupServers = Get-Content ‘C:\Scripts\BackupServers.txt’ # Backup Servers are the servers where folders exists
$Servers = Get-Content ‘C:\Scripts\Serverlist.txt’ # contains folder names which are nothing but server names

Write-host “Deleting Backup folders from Backup Servers… ” -ForegroundColor cyan -BackgroundColor DarkMagenta

foreach ($BackupServer in $BackupServers)

{

foreach ($Server in $Servers)
{

Write-host “Removing Backups from the server ” -f Yellow -NoNewline; write-host $Server -f Cyan

Remove-Item -Verbose “\\$BackupServer\$Servers” -Recurse -ErrorAction continue
}
}

Error

Remove-Item : Access is denied
At line:20 char:5
+ Remove-Item -Verbose “\\$BackupServer\$Servers_Test” -Recurse -Er …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\\LVSQLBACKUP\Padma_test123:String) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand

Remove-Item : Cannot find path ‘\\LVSQLBACKUP\Padma_test123’ because it does not exist.
At line:20 char:5
+ Remove-Item -Verbose “\\$BackupServer\$Servers_Test” -Recurse -Er …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\LVSQLBACKUP\Padma_test123:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand

Its just because of double hoping issue. When in PowerShell if we try to do any action which requires authentication within a remote session, then for security reasons the authentication is done by default using NT Authority\System account. You can understand more about it here. Here you are trying to access a network share which requires authentication.

Didn’t we answer this question already?

Delete Folders from Multiple Servers

Didn’t noticed the original one. @PowerShell_Techie, This thread will be closed, please continue to reply in the old thread if the problem is not solved.

https://powershell.org/forums/topic/delete-folders-from-multiple-servers-2/#post-290752