CMD:如何在cmd中转义此powershell命令?

时间:2016-03-23 17:32:50

标签: powershell cmd

我有以下powershell命令:

(gc c:\foo.xml -Raw) -replace '(?s)(.*)<bar baz="foo" />', '$1LOL' | Out-File c:\foo.xml

我知道当我在PowerShell中输入它时,验证了这个powershell命令。

现在我想从cmd提示符运行它,所以我知道我需要在cmd中执行:

powershell -Command [the-above-powershell-command]

然而,powershell命令有很多特殊字符,无论我使用什么逃脱,我都无法做到。

有人可以为我逃脱powershell命令,所以我可以把它放在cmd中吗?

2 个答案:

答案 0 :(得分:5)

自己找到答案:

router.route('/deleteGuestFromJam/:id').delete(function(req, res){

Jam.find({_id: req.params.id}, function(err, jam){

    jam.guests = _.without(jam.guests, _.findWhere(jam.guests, {id: req.user.id}));

    jam.save(function(err, jam) {
        if (err){
            return res.status(500).send(err)
        }
        console.log(jam.guests)
        return res.status(200).send(jam);
    });

})

诀窍是逃避&#34;与\,而不是&#34;。

webdriver specification帮助我解决了问题。

答案 1 :(得分:5)

另一种方法是对命令进行Base64编码并使用-EncodedCommand开关:

$CmdLine = @'
(gc c:\foo.xml -Raw) -replace '(?s)(.*)<bar baz="foo" />', '$1LOL' | Out-File c:\foo.xml
'@
$CmdLineBytes = [System.Text.Encoding]::Unicode.GetBytes($CmdLine)
$EncodedCommand = [System.Convert]::ToBase64String($CmdLineBytes)

在您的情况下,这将产生字符串:

KABnAGMAIABjADoAXABmAG8AbwAuAHgAbQBsACAALQBSAGEAdwApACAALQByAGUAcABsAGEAYwBlACAAJwAoAD8AcwApACgALgAqACkAPABiAGEAcgAgAGIAYQB6AD0AIgBmAG8AbwAiACAALwA+ACcALAAgACcAJAAxAEwATwBMACcAIAB8ACAATwB1AHQALQBGAGkAbABlACAAYwA6AFwAZgBvAG8ALgB4AG0AbAA=

cmd

调用它
powershell.exe -EncodedCommand KABnAGMAIABjADoAXABmAG8AbwAuAHgAbQBsACAALQBSAGEAdwApACAALQByAGUAcABsAGEAYwBlACAAJwAoAD8AcwApACgALgAqACkAPABiAGEAcgAgAGIAYQB6AD0AIgBmAG8AbwAiACAALwA+ACcALAAgACcAJAAxAEwATwBMACcAIAB8ACAATwB1AHQALQBGAGkAbABlACAAYwA6AFwAZgBvAG8ALgB4AG0AbAA=