简体   繁体   English

对powershell.exe正确转义命令(从CMD.exe启动命令)

[英]Escaping command correctly for powershell.exe (launch command from CMD.exe)

How do I escape properly the below command? 如何正确逃避以下命令? I need to run the powershell command from CMD because it's a stubpath value. 我需要从CMD运行powershell命令,因为它是一个stubpath值。 The registry value for stubpath can't run powershell commands natively. stubpath的注册表值无法本机运行powershell命令。 The command is verified and works correctly. 该命令已经过验证且工作正常。 The problem is only to get it to run via powershell.exe -command "..." 问题只是让它通过powershell.exe -command "..."运行 - powershell.exe -command "..."

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "(Get-WmiObject -Class Win32_Printer | where-object { $_.Name -eq ("IPL" + ($env:COMPUTERNAME).Substring(1, 4)) }).SetDefaultPrinter()"

Right now it's breaking the line at "IPL" because of the double quotes. 由于双引号,它现在正在“IPL”打破界限。 I think I have to escape the pipe has well. 我想我必须逃脱管道很好。

Much appreciated! 非常感激!

you might try this 你可以试试这个

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& { (Get-WmiObject -Class Win32_Printer | where-object { $_.Name -eq (\"IPL\" + ($env:COMPUTERNAME).Substring(1, 4)) }).SetDefaultPrinter() }"

otherwise, what i like to do to avoid escaping anything is encode the command into base64, then use powershell -encodedcommand, like this 否则,我想做什么来避免逃避任何东西是将命令编码到base64,然后使用powershell -encodedcommand,像这样

powershell.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand KABHAGUAdAAtAFcAbQBpAE8AYgBqAGUAYwB0ACAALQBDAGwAYQBzAHMAIABXAGkAbgAzADIAXwBQAHIAaQBuAHQAZQByACAAfAAgAHcAaABlAHIAZQAtAG8AYgBqAGUAYwB0ACAAewAgACQAXwAuAE4AYQBtAGUAIAAtAGUAcQAgACgAIgBJAFAATAAiACAAKwAgACgAJABlAG4AdgA6AEMATwBNAFAAVQBUAEUAUgBOAEEATQBFACkALgBTAHUAYgBzAHQAcgBpAG4AZwAoADEALAAgADQAKQApACAAfQApAC4AUwBlAHQARABlAGYAYQB1AGwAdABQAHIAaQBuAHQAZQByACgAKQA=

here is a link to info on how to encode your commands. 这是一个关于如何编码命令的信息的链接。 this even works for full, multi-line scripts 这甚至适用于完整的多行脚本

https://blogs.msdn.microsoft.com/timid/2014/03/26/powershell-encodedcommand-and-round-trips/ https://blogs.msdn.microsoft.com/timid/2014/03/26/powershell-encodedcommand-and-round-trips/

or you may be able to use this site to encode/decode 或者您可以使用此站点进行编码/解码

https://www.base64decode.org/ https://www.base64decode.org/

Found it, I'll leave this here since it can be useful for other users: 找到它,我会留在这里,因为它对其他用户有用:

Original powershell command : 原始powershell命令:

(Get-WmiObject -Class Win32_Printer | where-object { $_.Name -eq ("IPL" + ($env:COMPUTERNAME).Substring(1, 4)) }).SetDefaultPrinter()

To run from CMD.EXE, you have to escape the double quotes like this \\"IPL\\" instead of "IPL" 要从CMD.EXE运行,您必须转义双引号,如\\"IPL\\"而不是"IPL"

Working command: 工作指令:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "(Get-WmiObject -Class Win32_Printer | where-object { $_.Name -eq (\"IPL\" + ($env:COMPUTERNAME).Substring(1, 4)) }).SetDefaultPrinter()"

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

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