Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过命令提示符转义powershell.exe中的引号_Powershell_Cmd - Fatal编程技术网

通过命令提示符转义powershell.exe中的引号

通过命令提示符转义powershell.exe中的引号,powershell,cmd,Powershell,Cmd,查看后,我尝试转义引号,但使用反斜杠不会转义如下的html引号:“从命令提示符运行此命令(实际上是在PowerShell中): 生成: hello '123' world 这能满足您的需要吗?使用反勾`来转义您的特殊双引号,因此: powershell -noexit -command $str = \"hello '123'” world\"; write-host $str 变成这样: powershell -noexit -command $str = \"hello '123'`”

查看后,我尝试转义引号,但使用反斜杠不会转义如下的html引号:
从命令提示符运行此命令(实际上是在PowerShell中):

生成:

hello '123' world
这能满足您的需要吗?

使用反勾`来转义您的特殊双引号,因此:

powershell -noexit -command $str = \"hello '123'” world\"; write-host $str
变成这样:

powershell -noexit -command $str = \"hello '123'`” world\"; write-host $str
编辑:

powershell -noexit -command $str = \"hello '123' world\"; write-host $str
powershell -noexit -command $str = \"hello '123'” world\"; write-host $str
备选案文1。如果您喜欢使用here字符串,可以将上面的内容更改为
powershell-file
并运行powershell脚本文件。在此处尽可能多地使用字符串

备选案文2。如果您不想在调用/处理应用程序/脚本中处理特殊的引号字符,可以改为使用单引号。在这种情况下,您只需通过将单引号加倍来转义单引号,如下所示:

powershell -noexit -command $str = 'hello ''123''” world'; write-host $str
注意,这里我没有对你的双引号字符做任何修改,而且效果很好

因此,字符串替换代码可能会变得更易于阅读和维护,特别是在编辑器未正确显示此字符的情况下(就像Powershell控制台一样,它会显示一个常规的双引号)

  • 启动-命令切换为“”
  • 字符串是通过在文本两侧加4倍引号来识别的:“文本”

    例如:

  • 对于子表达式中的引号,双引号表示8倍引号

    例如:


  • 我也遇到过同样的问题,并发现使用Powershell 6.x(Core)可以做到这一点:

    pwsh -command write-host "`u{22}quoted`u{22}" 
    
    输出:

    "quoted"
    

    在命令提示符下,您的“工作正常”行对我不起作用。我得到“字符串缺少终结者:”。“错误。如果我从run对话框执行它,它会正常处理。这并没有解决OPs问题中的智能引号字符。是的,在一些屏幕上很难区分这个角色和普通的双引号。在我的Surface 2上非常清晰,但在我的桌面上看,很难看到问题中使用的比例字体。。。啊,我现在看得很清楚了。我相信Neolisk给出的答案如下:)工作起来很有魅力!谢谢,但这不需要我知道正确的双引号在哪里吗?问题是我的$str是一个可变文本。它用于只能通过命令提示符启动powershell的Service Manager(SCSM):(.$str是一个可能包含上述特殊字符的描述字段,因此如果我可以转义所有字符,那就太好了。@user2969412:您应该知道您的特殊字符,以便可以转义它们。是的,您自己负责转义它们,Powershell中没有标准函数。这是一个试用版ror过程,直到你发现所有这些特殊字符。你可能会发现有用的。我确实知道我的特殊字符-只有这个正确的双引号才是问题。但我不知道它会发生在文本中的什么地方。在powershell中,你可以通过这个@''来转义所有内容,所以从@'到@的所有内容都将转义。只有我无法获得是通过命令提示符工作。所以我认为cmd有很多类似的东西。

    here字符串也需要在单独的一行上才能工作:$str=@'hello world*'“'123'@@user2969412:谁/什么运行此命令?它是C#应用程序吗?如何将
    $str
    文本传递到它?在执行命令之前,您应该能够将
    替换为
    `
    ”。我们越来越热:)哇!8个双引号,即16个单引号。命令提示符和Powershell是完美的匹配;)
    Output Stream:> Recognized as string
    
    powershell.exe -command """""""""How to quote in subexpression"""""""""
    
    Output Stream:> "How to quote in subexpression"
    
    pwsh -command write-host "`u{22}quoted`u{22}" 
    
    "quoted"