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