如何在 SQL 中格式化我的字符串,以便在 Powershell 中正确解释它?
How can I format my string in SQL so that it is interpreted properly in Powershell?
我目前正在开发一个简单的 SQL 脚本,我想从 SSMS 运行。该脚本应该做的是获取一个数据库,对其进行备份,然后为该备份创建一个 .zip 文件。
我 运行 遇到的问题是在尝试压缩备份时遇到的问题。我在文件的开头声明了所有变量,我相信当我尝试执行 @sqlcmd 时,Powershell 没有正确读取该字符串。
SET @sqlcmd = ('powershell.exe [IO.Compression.ZipFile]::CreateFromDirectory("' + @bkpath + '", "C:\'+ @fileName + '.zip")')
PRINT @sqlcmd
-- this statement returns (powershell.exe [IO.Compression.ZipFile]::CreateFromDirectory("C:\Backup\", "C:\ScriptingTestDB_20170607.zip"))
EXEC xp_cmdshell @sqlcmd
运行这段代码returns我的结果输出如下window:
At line:1 char:47
+ [IO.Compression.ZipFile]::CreateFromDirectory(C:\Backup", C:\Scriptin ...
+ ~
Missing ')' in method call.
At line:1 char:56
+ ... le]::CreateFromDirectory(C:\Backup", C:\ScriptingTestDB_20170607.zip)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At line:1 char:47
+ ... le]::CreateFromDirectory(C:\Backup", C:\ScriptingTestDB_20170607.zip)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'C:\Backup", C:\ScriptingTestDB_20170607.zip)' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
NULL
经过一些测试,我想我发现问题在于 Powershell 如何解释 SQL 字符串。由于有很多引号,即使 SQL 正确地打印出字符串,当 Powershell 获取它时,格式也会变得混乱,因此需要在那里进行一些更改,但仍然不确定是什么。有谁知道我可以用来找出如何编写 SQL 代码以便 Powershell 读取以下内容的任何准则:
[IO.Compression.ZipFile]::CreateFromDirectory("C:\Backup\", "C:\ScriptingTestDB_20170607.zip")
您需要使用 \
转义 "
个字符。我的工作脚本:
declare @sqlcmd varchar(1000), @bkpath nvarchar(max) = 'C:\i1\tmp', @filename nvarchar(max) = 'file'
SET @sqlcmd = 'powershell.exe Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::CreateFromDirectory(\"' + @bkpath + '\", \"C:\i1\'+ @fileName + '.zip\")'
EXEC xp_cmdshell @sqlcmd
我目前正在开发一个简单的 SQL 脚本,我想从 SSMS 运行。该脚本应该做的是获取一个数据库,对其进行备份,然后为该备份创建一个 .zip 文件。
我 运行 遇到的问题是在尝试压缩备份时遇到的问题。我在文件的开头声明了所有变量,我相信当我尝试执行 @sqlcmd 时,Powershell 没有正确读取该字符串。
SET @sqlcmd = ('powershell.exe [IO.Compression.ZipFile]::CreateFromDirectory("' + @bkpath + '", "C:\'+ @fileName + '.zip")')
PRINT @sqlcmd
-- this statement returns (powershell.exe [IO.Compression.ZipFile]::CreateFromDirectory("C:\Backup\", "C:\ScriptingTestDB_20170607.zip"))
EXEC xp_cmdshell @sqlcmd
运行这段代码returns我的结果输出如下window:
At line:1 char:47
+ [IO.Compression.ZipFile]::CreateFromDirectory(C:\Backup", C:\Scriptin ...
+ ~
Missing ')' in method call.
At line:1 char:56
+ ... le]::CreateFromDirectory(C:\Backup", C:\ScriptingTestDB_20170607.zip)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: ".
At line:1 char:47
+ ... le]::CreateFromDirectory(C:\Backup", C:\ScriptingTestDB_20170607.zip)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'C:\Backup", C:\ScriptingTestDB_20170607.zip)' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
NULL
经过一些测试,我想我发现问题在于 Powershell 如何解释 SQL 字符串。由于有很多引号,即使 SQL 正确地打印出字符串,当 Powershell 获取它时,格式也会变得混乱,因此需要在那里进行一些更改,但仍然不确定是什么。有谁知道我可以用来找出如何编写 SQL 代码以便 Powershell 读取以下内容的任何准则:
[IO.Compression.ZipFile]::CreateFromDirectory("C:\Backup\", "C:\ScriptingTestDB_20170607.zip")
您需要使用 \
转义 "
个字符。我的工作脚本:
declare @sqlcmd varchar(1000), @bkpath nvarchar(max) = 'C:\i1\tmp', @filename nvarchar(max) = 'file'
SET @sqlcmd = 'powershell.exe Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::CreateFromDirectory(\"' + @bkpath + '\", \"C:\i1\'+ @fileName + '.zip\")'
EXEC xp_cmdshell @sqlcmd