小编Joh*_*mer的帖子

在批处理文件中调用powershell脚本时,如何使用嵌套引号?

我有一个DOS批处理文件,其中有一行执行powershell脚本.首先,我在批处理文件中尝试了一个非常简单的脚本:

powershell -command "get-date" < nul
Run Code Online (Sandbox Code Playgroud)

这很有效.但该脚本嵌套了双引号字符,有时可以使用反引号(`)字符进行转义.那么我试过这个:

powershell -command "Write-Host `"hello world`"" < nul
Run Code Online (Sandbox Code Playgroud)

这也很有效.但是,我需要运行的脚本非常复杂,并且具有多个级别的嵌套双引号字符.我已经采用了复杂的脚本并将其简化为具有相同原理的示例:

[string]$Source =  "    `"hello world`" ";
Write-Host $Source;
Run Code Online (Sandbox Code Playgroud)

如果我将此脚本保存在PS脚本文件中并运行它,它工作正常,打印出"hello world",包括双引号,但我需要将它嵌入批处理文件中的行中.所以我把脚本放在一行上,并尝试将其插入批处理文件行,但它不起作用.我试图逃避双引号,但它仍然不起作用,像这样:

powershell -command "[string]$Source =  `"    `"hello world`" `";Write-Host $Source;" < nul
Run Code Online (Sandbox Code Playgroud)

有办法做我想要的吗?你可能会问为什么我这样做,但这是一个很长的故事,所以我不会详细介绍.谢谢

powershell batch-file

7
推荐指数
1
解决办法
9452
查看次数

标签 统计

batch-file ×1

powershell ×1