I have a strange error occurring when using the invoke-sqlcmd to insert rows into a table. The entire script works perfectly if executed one time, but if I run it a second time, it fails with the error message: Get-ChildItem : Cannot call method. The provider does not support the use of filters. I tested the code and commented out the invoke-sqlcmd line, and can run it multiple times without any errors. I put the invoke-sqlcmd in a function and it still errors, and after the first successful run it will fail at the PS command line too.
Clear-Host
$ErrorActionPreference = 'Stop'
function Update-SQL
{
invoke-sqlcmd -query $strSQLInsert -server SXLSV-LEAPDBD1 -database DTCS_DV
}
$files = Get-ChildItem '\\pcslog1011\dtcs\ContractEmailNotes\' -Filter '*.txt' | Where-Object {$_.LastWriteTime -ge '08/22/2016 00:00:00' -and $_.LastWriteTime -le '08/22/2016 23:59:59'} | sort-object -Descending
for ($i=0; $i -lt $files.Count; $i++)
{
$SNAC_CNTR_ID = $files[$i].BaseName.Substring(0,6)
$SNAC_MODIFIED = '{0:yyyy-MM-dd HH:mm:ss}' -f ($files[$i].LastWriteTime)
$reader = [System.IO.File]::OpenText($files[$i].FullName)
$lineCnt = 0
$SNAC_ACTION = ''
for()
{
$lineCnt += 1
$line = $reader.ReadLine()
if ($line -eq $null) {break}
if ($lineCnt -eq 4)
{
if ($line.Contains('AMENDED') -eq $True)
{
$SNAC_ACTION = 'AMENDED'
}
elseif ($line.Contains('DEAL CHANGED') -eq $True)
{
$SNAC_ACTION = 'CHANGED'
}
else
{
$SNAC_ACTION = 'NEW'
}
}
}
$reader.Close()
write-host $SNAC_CNTR_ID $SNAC_MODIFIED $SNAC_ACTION
$strSQLInsert = "INSERT INTO CQT_CONTRACT_SOX_NAC_LIST (SNAC_CNTR_ID, SNAC_MODIFIED, SNAC_ACTION, SNAC_UPDATED_BY, SNAC_UPDATED_ON) VALUES ('" + $SNAC_CNTR_ID + "', '" + $SNAC_MODIFIED + "', '" + $SNAC_ACTION + "', Default, Default)"
Update-SQL $strSQLInsert
}
exit