如何正确转义以下命令?我需要从CMD运行powershell命令,因为它是存根路径值。 stubpath的注册表值不能在本地运行powershell命令。该命令已验证并且可以正常工作。问题只是让它通过TDisplayMode::dmYear
On(Calendar)ChangeView
由于双引号,现在它在“ IPL”处中断了行。我想我必须逃脱管道的状况。
非常感谢!
答案 0 :(得分:2)
您可以尝试
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& { (Get-WmiObject -Class Win32_Printer | where-object { $_.Name -eq (\"IPL\" + ($env:COMPUTERNAME).Substring(1, 4)) }).SetDefaultPrinter() }"
否则,为了避免转义,我想做的就是将命令编码为base64,然后使用powershell -encodedcommand这样
powershell.exe -NoProfile -ExecutionPolicy Bypass -EncodedCommand KABHAGUAdAAtAFcAbQBpAE8AYgBqAGUAYwB0ACAALQBDAGwAYQBzAHMAIABXAGkAbgAzADIAXwBQAHIAaQBuAHQAZQByACAAfAAgAHcAaABlAHIAZQAtAG8AYgBqAGUAYwB0ACAAewAgACQAXwAuAE4AYQBtAGUAIAAtAGUAcQAgACgAIgBJAFAATAAiACAAKwAgACgAJABlAG4AdgA6AEMATwBNAFAAVQBUAEUAUgBOAEEATQBFACkALgBTAHUAYgBzAHQAcgBpAG4AZwAoADEALAAgADQAKQApACAAfQApAC4AUwBlAHQARABlAGYAYQB1AGwAdABQAHIAaQBuAHQAZQByACgAKQA=
这里是有关如何编码命令的信息的链接。这甚至适用于完整的多行脚本
https://blogs.msdn.microsoft.com/timid/2014/03/26/powershell-encodedcommand-and-round-trips/
或者您可以使用此网站进行编码/解码
答案 1 :(得分:1)
找到它了,我将其留在这里,因为它可能对其他用户有用:
原始powershell命令:
(Get-WmiObject -Class Win32_Printer | where-object { $_.Name -eq ("IPL" + ($env:COMPUTERNAME).Substring(1, 4)) }).SetDefaultPrinter()
要从CMD.EXE运行,必须转义像这样的双引号\"IPL\"
而不是"IPL"
工作命令:
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "(Get-WmiObject -Class Win32_Printer | where-object { $_.Name -eq (\"IPL\" + ($env:COMPUTERNAME).Substring(1, 4)) }).SetDefaultPrinter()"