我有以下功能:
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定义新变量的格式无效。
我传入的值是:
我觉得我在这里找不到一些非常明显的东西,但看了一个小时之后,我感到难过。