Here is the code I am trying:
[string] $Server= "ServerName"
[string] $Database = "DBName"
$UserSqlQuery= $("select FileDelete from DBName.dbo.tmp_TableName")
$resultsDataTable = New-Object System.Data.DataTable
$resultsDataTable.Columns.Add((New-Object System.Data.DataColumn 'FileDelete', ([string])))
$resultsDataTable = ExecuteSqlQuery $Server $Database $UserSqlQuery
Write-Host ("The table contains: " + $resultsDatatable.Rows.Count + " rows")
echo $resultsDatatable
$RowDelete = $resultsDatatable
foreach($RowDelete in $resultsDatatable)
{
echo "Inside For Loop"
echo $RowDelete
write-host “Deleting Duplicate PDF File/ Letterâ€;
# $RowDelete | foreach { $_.Delete()}
# $RowDelete | Remove-Item -Recurse -WhatIf -ErrorAction SilentlyContinue
Remove-Item -recurse -force $RowDelete -erroraction silentlycontinue -Whatif
}
DBName.dbo.tmp_TableName has single column with multiple values of path of the file on shared drive.
Example of Actual Path passed thru $RowDelete
is \\ccaintranet.com\Path\abc.pdf
.
The code is working without any error now. But it is not deleting the file from windows folder.
Can someone please help me to diagnose the error?
--===================================
Expected Result -
By running executing following query, $UserSqlQuery= $("select FileDelete from ServerName.DBName.dbo.tmp_TableName")
$resultsDataTable gets multiple rows. These are file names with the path in the format below -
\\ccaintranet.com\Shared Server\Data\FS02-V\ClientName\Workspace\User\TestLetters\63566\1002059003.pdf
And what I am trying to achieve is deleting the above file using Remove-Item in PowerShell script.
Hope this information helps.