1

这是我正在尝试做的事情:

@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>
4

2 回答 2

2

我会使用内置命令 Start-Process 而不是创建 shell 对象,例如:

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

对于任何重要的事情,引用都会很烦人。能否将最终脚本放入文件中并使用 PowerShell.exe 上的 -File 参数执行脚本文件?

于 2010-11-22T06:05:51.790 回答
0

我解决了,这是针对我的实际问题的长批次单线,所以人们可以看到一个真实的例子:

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')"

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

于 2010-11-21T22:36:04.947 回答