December 17, 2017 at 9:44 pm
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.
January 4, 2018 at 8:44 am
shahnr21 - Sunday, December 17, 2017 9:44 PMHere is the code I am trying:
...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
}And what I am trying to achieve is deleting the above file using Remove-Item in PowerShell script.
Hope this information helps.
I realize this was posted a while back, but the -Whatif switch tells Powershell to just test and not actually make any permanent changes.
January 4, 2018 at 3:17 pm
shahnr21 - Sunday, December 17, 2017 9:44 PMHere 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.pdfAnd what I am trying to achieve is deleting the above file using Remove-Item in PowerShell script.
Hope this information helps.
It may not be working without any errors as they are just ignored when using -erroraction silentlycontinue. If you remove that, what are the errors? You will still get the errors if you are using -WhatIf
I think this is different than what you originally had posted. You were getting objects back, datarows, from Invoke-Sqlcmd or something like that.
Not sure if you are running into the same thing but you would want to check the type of $RowDelete, Try doing a $RowDelete.GetType() to make sure you are dealing with string values. The name in the output values tells you the type.
Sue
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply