Home Forums Programming Powershell Remove-Item giving error and not accepting path passed thru variable RE: Remove-Item giving error and not accepting path passed thru variable

  • 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