Windows 在运行混合批处理/powershell脚本时,转义双引号问题

Windows 在运行混合批处理/powershell脚本时,转义双引号问题,windows,powershell,batch-file,escaping,double-quotes,Windows,Powershell,Batch File,Escaping,Double Quotes,以下是我想做的: @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-

以下是我想做的:

@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>
错误的数值常量:4D。
第1行字符:57

+$path=HKLM:\SYSTEM\CurrentControlSet\Control\Class\{4D我解决了这个问题,下面是我真正的问题的一个很长的批处理一行,因此人们可以看到一个真实的例子:

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}''; Get-Childitem $path -ErrorAction SilentlyContinue | Where { (Get-ItemProperty $_.PSPath DriverDesc) -Match ''VMnet'' } | Foreach { New-ItemProperty -ErrorAction SilentlyContinue $_.PSPath -Name ''*NdisDeviceType'' -Value ''1'' -PropertyType DWord }; netsh interface set interface name=''VMware Network Adapter VMnet1'' admin=DISABLED; netsh interface set interface name=''VMware Network Adapter VMnet1'' admin=ENABLED; netsh interface set interface name=''VMware Network Adapter VMnet8'' admin=DISABLED; netsh interface set interface name=''VMware Network Adapter VMnet8'' admin=ENABLED""', '', 'runas')"

注意:如果有人想知道它是干什么用的……我每次安装/更新VMware Workstation时都会运行它,以隐藏虚拟网络适配器,使其不会出现在Windows Vista/7的网络和共享中心上。

我会使用内置的命令启动过程,而不是创建shell对象,例如:

CALL powershell -ExecutionPolicy RemoteSigned -NoProfile -Command "& {Start-Process PowerShell -Verb runas -Arg '-NoExit -Command & {$path=''foo'';$path}'}"

对于任何有意义的事情,引用都会很烦人。你能把最后一个脚本放在一个文件中,并使用PowerShell.exe上的-file参数执行脚本文件吗?

使用启动过程或创建shell对象有什么区别?我更喜欢只执行一个文件…使用内置功能需要更少的工作n必须转义到COM对象,我认为键入的次数更少。:-)