简体   繁体   English

在命令参数中转义双引号

[英]Escape double quotes in command parameter

I need to run a PS command on SQL Server.我需要在 SQL 服务器上运行 PS 命令。 Running a simple command without double-quotes works without a problem.运行一个没有双引号的简单命令没有问题。

How to run below command?如何在命令下运行?

DECLARE @sql nvarchar(4000);
 SET @sql = 
 '
 powershell.exe -command "
 $messageBody = "ds"
 Write-Host $messageBody
 "';
EXEC xp_cmdshell @sql;

I tried this ` and backslash but nothing seems to be working.我尝试了这个 ` 和反斜杠,但似乎没有任何效果。

Running it from ps1 file is not an option.从 ps1 文件运行它不是一个选项。

Instead of "ds" , use ''ds'' to use single quotes withing the ps script to enclose the literal.代替"ds" ,使用''ds''在 ps 脚本中使用单引号将文字括起来。 Furthermore, use the call operator (&) withing the command parameter to call the script block:此外,使用带有命令参数的调用运算符 (&) 来调用脚本块:

SET @sql = 'powershell.exe -command "& { $messageBody = ''ds''; Write-Host $messageBody }';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM