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,
This may or may not help you. I had the same error when trying to run my script in a SQL Agent job. It would work fine for my account and the service account the agent was running under but refused to work as a job until I prefixed the UNC file path it was reading with
Microsoft.PowerShell.Core\FileSystem::
I don't recommend file shares in automation solutions... but had no choice. Hope it helps.