function RunLocalSQLQuery($query)
IF ((Get-Command "sqlcmd" -ErrorAction SilentlyContinue))
sqlcmd -U sa -P <INSERT PWD> -d <INSERT DB NAME> -w 4096 -y 29 -Y 29 -Q $query
return 1
ELSEIF ((Get-Command "osql" -ErrorAction SilentlyContinue))
osql -U sa -P <INSERT PWD> -d <INSERT DB NAME> -w 4096 -y 29 -Y 29 -Q $query
return 1
ELSE
return 0
$query = @"
Declare @MTDB2 NVARCHAR(255)
Select @MTDB2 = DatabaseName
from #TempMaintenanceLog
where Command = 'CheckDBName'
BACKUP DATABASE @MTDB2
TO DISK = N'C:\Octopus\<DBNAME>.bak' WITH RETAINDAYS = 5,
FORMAT,
INIT,
MEDIANAME = N'<DBNAME>.bak',
NAME = N'<DBNAME>-Full Database Backup',
SKIP,
NOREWIND,
NOUNLOAD,
STATS = 10
GO
Declare @backupSetId as int
Select @backupSetId = position
From msdb..backupset
Where database_name=N'<DBNAME>'
and backup_set_id=(select max(backup_set_id)
From msdb..backupset
Where database_name=N'<DBNAME>' )
If @backupSetId is null
Begin
raiserror(N'Verify failed. Backup information for database ''<DBNAME>'' not found.', 16, 1)
SET NOEXEC ON
End
RESTORE VERIFYONLY FROM DISK = N'C:\Octopus\<DBNAME>.bak'
WITH FILE = @backupSetId,
NOUNLOAD,
NOREWIND
GO
"@
RunLocalSQLQuery($query)