Invoke-Sqlcmd定义新的变量错误

时间:2017-09-07 07:46:34

标签: powershell invoke-sqlcmd

我有以下功能:

Function Backup-SQLDatabase { #MP-RestoreDatabase
    Param(
        [Parameter(Mandatory)]
        [String]$Server,
        [Parameter(Mandatory)]
        [String]$Database,
        [Parameter(Mandatory)]
        [String]$BackupFile,
        [Parameter(DontShow)]
        [String]$SQLScriptPath = "$($PSScriptRoot)\SQLFiles\"
    )  

    Import-Module SQLPS

    Invoke-Sqlcmd -ErrorLevel 1 -InputFile "$($SQLScriptPath)BackupDatabase.sql" `
        -Variable DBName=$Name, BackupLocation=$BackupFile `
        -ServerInstance $server `
        -ErrorAction Stop
}

这是在调用这个SQL脚本:

use master
go

Declare @DBName varchar(max)
Declare  @BackupLocation varchar(max)
Declare @sql nvarchar(max)

set @DBName = '$(DBName)'
set @BackupLocation = '$(BackupLocation)'

SET @sql = N'BACKUP DATABASE ['+ @DBName + '] TO  DISK = N''' + @BackupLocation + ''' WITH  COPY_ONLY, NOFORMAT, NOINIT,  NAME = N''' + @DBName + '-Full Database Backup'', SKIP, NOREWIND, NOUNLOAD,  STATS = 10;'

EXEC sp_executesql @sql

但是当我打电话给它时,我收到错误

  

用于为Invoke-Sqlcmd cmdlet定义新变量的格式无效。

我传入的值是:

  • 数据库名称:uat.test.co.uk
  • 服务器实例:MSGUXXXXX01
  • 备份路径:\\ mstXXXXX01.XXXXX.local \ SQL_Backups_Temp \ ScriptBackups \ 20170907083425-uat.test.co.uk.bak

我觉得我在这里找不到一些非常明显的东西,但看了一个小时之后,我感到难过。

0 个答案:

没有答案