SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Remove-Item giving error and not accepting path passed thru variable


Remove-Item giving error and not accepting path passed thru variable

Author
Message
shahnr21
shahnr21
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 6

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.



SQLPirate
SQLPirate
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1171 Visits: 962
shahnr21 - Sunday, December 17, 2017 9:44 PM

Here 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.

Sue_H
Sue_H
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: General Forum Members
Points: 36228 Visits: 10349
shahnr21 - Sunday, December 17, 2017 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.




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



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search