混合批处理/powershell脚本运行中转义双引号问题
这就是我想要做的:
@ECHO OFF
CALL powershell -ExecutionPolicy RemoteSigned -Command "$sh = new-object -com 'Shell.Application'; $sh.ShellExecute('powershell', '-NoExit -Command "$path = """HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}""";echo $path"', '', 'runas')"
PAUSE
基本上,我想要一个可以双击的批处理文件,它将运行一个 powershell 脚本,该脚本调用另一个 powershell 脚本,但要求管理员权限并以管理员身份运行该命令。
不过,我遇到了问题,我认为双引号...我已经尝试了很多方法,但似乎无法修复它,这是 powershell 错误消息:
Bad numeric constant: 4D.
At line:1 char:57
+ $path = HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D <<<< 36E972-E325-11C
E-BFC1-08002BE10318};echo $path
+ CategoryInfo : ParserError: (4D:String) [], ParentContainsError
RecordException
+ FullyQualifiedErrorId : BadNumericConstant
PS C:\Windows\system32>
Here's what I'm trying to do:
@ECHO OFF
CALL powershell -ExecutionPolicy RemoteSigned -Command "$sh = new-object -com 'Shell.Application'; $sh.ShellExecute('powershell', '-NoExit -Command "$path = """HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}""";echo $path"', '', 'runas')"
PAUSE
Basically, I want to have a batch file that I can double-click, which will run a powershell script that calls another powershell script but asks for admin privileges and runs that command as admin.
I'm having problems though, with the double-quotes I think... I've tried many things but can't seem to fix it, here's the powershell error message:
Bad numeric constant: 4D.
At line:1 char:57
+ $path = HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D <<<< 36E972-E325-11C
E-BFC1-08002BE10318};echo $path
+ CategoryInfo : ParserError: (4D:String) [], ParentContainsError
RecordException
+ FullyQualifiedErrorId : BadNumericConstant
PS C:\Windows\system32>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用内置命令 Start-Process 而不是创建 shell 对象,例如:
对于任何重要的事情,引用都会很烦人。您可以将最终脚本放入文件中并使用 PowerShell.exe 上的 -File 参数执行脚本文件吗?
I would use the built-in command Start-Process rather than creating a shell object e.g.:
For anything of significance the quoting is going to be annoying. Can you put the final script in a file and execute the script file using the -File parameter on PowerShell.exe?
我解决了这个问题,这是我真正问题的长批单行代码,这样人们就可以看到一个真实的例子:
PS:如果有人想知道它的用途......我每次安装/更新 VMware Workstation 时都会运行它以隐藏虚拟网络适配器不会出现在 Windows Vista/7 的网络和共享中心上。
I solved it, here's the long batch one-liner for my real problem, so people can see a real example:
P.S: In case anyone's wondering what it's for... I run this every time I install/update VMware Workstation to hide the virtual network adapters from appearing on the Network and Sharing Center in Windows Vista/7.