Hey!
So I am trying to get a .bat file created and run using powershell. Pretty much a single line.
.\Backup1.ps1 -serverName "localhost" -backupDirectory "D:\SQL Backups\" -daystoStore 7
The above command works directly when run from powershell.
I created a .bat file with the contents
powershell

\Backup1.ps1 -serverName "localhost" -backupDirectory "D:\SQL Backups\" -daystoStore 7
And when I run the .bat I get the following error
The string is missing the terminator:".
+ Category Info : ParseError: (:) [], Parent ContainsErrorRecordException
+ Fully QualifiedErrorID : TerminatorExpectedAtEndofString
I have tried Single quotes and stuff like that but dont seem to understand what the problem is.
Just in case. The contents of backup1.ps1 are
param(
$serverName,
$backupDirectory,
$daysToStoreBackups
)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
$server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $serverName
$dbs = $server.Databases
foreach ($database in $dbs | where { $_.IsSystemObject -eq $False })
{
$dbName = $database.Name
$timestamp = Get-Date -format yyyy-MM-dd-HHmmss
$backupFileName = $dbName + "_UAT_" + $timestamp + ".bak"
$backupFileNames = $dbName + "_UAT_" + $timestamp + ".log"
$targetPath = $backupDirectory + "\" + $backupFileName
$targetPaths = $backupDirectory + "\" + $backupFileNames
$smoBackup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup")
$smoBackup.Action = "Database"
$smoBackup.BackupSetDescription = "Full Backup of " + $dbName
$smoBackup.BackupSetName = $dbName + " Backup"
$smoBackup.Database = $dbName
$smoBackup.MediaDescription = "Disk"
$smoBackup.Devices.AddDevice($targetPath, "File")
$smoBackup.SqlBackup($server)
Write-S3Object -BucketName sqlbackup -File $targetPath -Key $backupFileName -Region ap-southeast-2
Backup-SqlDatabase -ServerInstance localhost -Database $dbName -BackupAction Log -BackupFile $backupFileNames
Write-S3Object -BucketName sqlbackup -File $targetPaths -Key $backupFileNames -Region ap-southeast-2
"backed & uploaded $dbName ($serverName) to $targetPath"
}
Get-ChildItem "$backupDirectory\*.bak" |? { $_.lastwritetime -le (Get-Date).AddDays(-$daysToStoreBackups)} |% {Remove-Item $_ -force }
"removed all previous backups older than $daysToStoreBackups days"