简体   繁体   English

批处理文件发出的 powershell 命令中的 Escaping 引用

[英]Escaping quotes in powershell command issued by batch file

I've been trying to get a PowerShell command to create a shortcut working in a batch file but can't get it to actually put the quotes I need in the shortcut.我一直在尝试获取 PowerShell 命令来创建在批处理文件中工作的快捷方式,但无法将我需要的引号实际放入快捷方式中。 I've used this command in other batch files just fine, but the quotes are messing it up.我在其他批处理文件中使用过这个命令就好了,但是引号把它搞砸了。 I've tried "", """, """", ", ", \ ", `", and pretty much every variation I can think of and nothing has worked. I need the shortcut to be this:我已经尝试过“”、“””、“””、“、 ", \ “、`”,几乎所有我能想到的变体都没有奏效。我需要这样的快捷方式:

"C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect "mullvad.ovpn" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect "mullvad.ovpn"

And here's what I've been trying, of course with the numerous variations:这就是我一直在尝试的,当然还有很多变化:

powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\OpenVPN GUI.lnk');$s.TargetPath='"C:\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect "mullvad.ovpn"';$s.Save()" powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\OpenVPN GUI.lnk');$s.TargetPath='"C :\Program Files\OpenVPN\bin\openvpn-gui.exe" --connect "mullvad.ovpn"';$s.Save()"

But it either just creates a shortcut to this:但它要么只是为此创建了一个快捷方式:

"C:\Program Files\OpenVPN\bin\openvpn-gui.exe --connect mullvad_us_chi.ovpn" "C:\Program Files\OpenVPN\bin\openvpn-gui.exe --connect mullvad_us_chi.ovpn"

or, more often, it errors because the syntax is wrong with my various attempts.或者,更常见的是,它出错是因为我的各种尝试的语法错误。 So how do I get PowerShell to do what I want here?那么如何让 PowerShell 在这里做我想做的事呢?

Your main issue is that you aren't correctly creating a shortcut.您的主要问题是您没有正确创建快捷方式。

In order to do so, you should first of all use an additional property.为此,您应该首先使用附加属性。 Your TargetPath should be the executable, and the rest, its Arguments :您的TargetPath应该是可执行文件,并且 rest 及其Arguments

Then your command should look a little more like this:然后你的命令应该看起来更像这样:

"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -Command "$s = (New-Object -COM WScript.Shell).CreateShortcut(\"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\OpenVPN GUI.lnk\");$s.TargetPath=\"C:\Program Files\OpenVPN\bin\openvpn-gui.exe\";$s.Arguments=\"--connect `\"mullvad.ovpn`\"\";$s.Save()"

I rarely use single quotes, but if you prefer them then perhaps this would also work for you:我很少使用单引号,但如果您更喜欢它们,那么也许这也适合您:

"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -Command "$s = (New-Object -COM WScript.Shell).CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\OpenVPN GUI.lnk');$s.TargetPath='C:\Program Files\OpenVPN\bin\openvpn-gui.exe';$s.Arguments='--connect \"mullvad.ovpn\"';$s.Save()"

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

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