invoke-sqlcmd ends with error after completion the operaition

Invoke-SQLCMD ends with below error after completing SQL server backup through a script delivered

+ invoke-sqlcmd -InputFile $DBSCRIPTFILE -ServerInstance $INSTSERVER
-Username ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerS
hellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShe
ll.GetScriptCommand

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Below is the SQL script

DECLARE @name VARCHAR(50) -- database name
DECLARE @rec_model tinyint
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @LogName VARCHAR(256) -- filename for Log backup
DECLARE @fileDate VARCHAR(20) -- used for file name

SET @path = 'F:\MSSQL\Backup'

SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)

DECLARE db_cursor CURSOR READ_ONLY FOR
SELECT name , recovery_model
FROM sys.databases where state <> 6

OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name , @rec_model

WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path +'\'+ @name + '\'+'FULL'+'\'+@name+'_' + @fileDate + '.BAK'
SET @LogName = @path +'\'+ @name + '\'+'FULL'+'\'+@name+'_' + @fileDate + '.LBK'
BACKUP DATABASE @name TO DISK = @fileName WITH RETAINDAYS = 1

IF ( @rec_model = 1 )
BEGIN
BACKUP LOG @name TO DISK = @LogName
END
FETCH NEXT FROM db_cursor INTO @name , @rec_model
END

/** CLOSE db_cursor **/
DEALLOCATE db_cursor

The error you share seems to be incomplete, please share the complete error without trimming it.

invoke-sqlcmd : Backup and restore operations are not allowed on database
tempdb.
BACKUP DATABASE is terminating abnormally.
Cannot open backup device ‘F:\MSSQL\Backup\Guard1Plus_Arc_2013-07-08\FULL\Guard
1Plus_Arc_2013-07-08_20190424.BAK’. Operating system error 3(The system cannot
find the path specified.).
BACKUP DATABASE is terminating abnormally.
Cannot open backup device
‘F:\MSSQL\Backup\Guard1PlusConfigTest\FULL\Guard1PlusConfigTest_20190424.BAK’.
Operating system error 3(The system cannot find the path specified.).
BACKUP DATABASE is terminating abnormally.
Cannot open backup device
‘F:\MSSQL\Backup\Guard1PlusTest\FULL\Guard1PlusTest_20190424.BAK’. Operating
system error 3(The system cannot find the path specified.).
BACKUP DATABASE is terminating abnormally.
Processed 320 pages for database ‘model’, file ‘modeldev’ on file 3.
Processed 2 pages for database ‘model’, file ‘modellog’ on file 3.
BACKUP DATABASE successfully processed 322 pages in 0.120 seconds (20.902
MB/sec).
Processed 2392 pages for database ‘msdb’, file ‘MSDBData’ on file 3.
Processed 4 pages for database ‘msdb’, file ‘MSDBLog’ on file 3.
BACKUP DATABASE successfully processed 2396 pages in 0.863 seconds (21.689
MB/sec).
Processed 1640 pages for database ‘ReportServer’, file ‘ReportServer’ on file
3.
Processed 2 pages for database ‘ReportServer’, file ‘ReportServer_log’ on file
3.
BACKUP DATABASE successfully processed 1642 pages in 0.714 seconds (17.956
MB/sec).
Processed 592 pages for database ‘ReportServerTempDB’, file
‘ReportServerTempDB’ on file 3.
Processed 2 pages for database ‘ReportServerTempDB’, file
‘ReportServerTempDB_log’ on file 3.
BACKUP DATABASE successfully processed 594 pages in 0.223 seconds (20.777
MB/sec).
Processed 17104 pages for database ‘Guard1Plus’, file ‘Guard1Plus’ on file 3.
Processed 2 pages for database ‘Guard1Plus’, file ‘Guard1Plus_log’ on file 3.
BACKUP DATABASE successfully processed 17106 pages in 3.054 seconds (43.756
MB/sec).
At F:\dba\bin\MS_SQLServer_Backup.ps1:11 char:1

  • invoke-sqlcmd -InputFile $DBSCRIPTFILE -ServerInstance $INSTSERVER
    -Username …
+ CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlPowerS
hellSqlExecutionException
+ FullyQualifiedErrorId : SqlError,Microsoft.SqlServer.Management.PowerShe
ll.GetScriptCommand