在Powershell中为带参数的快捷方式创建带引号的路径 [英] creating quoted path for shortcut with arguments in powershell

查看:142
本文介绍了在Powershell中为带参数的快捷方式创建带引号的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下调用WSHShell的powershell代码,它将在Win7/8的开始菜单中创建一个快捷方式,但无法弄清楚如何使powershell在目标中的参数之前传递UNC路径所需的引号线.

I have the following powershell code calling WSHShell which will create a shortcut in the start menu for Win7/8 but am unable to figure out how to get powershell to pass the quotes needed around the UNC path prior to the arguments in the target line.

我想要的东西:"\\ UNCPATH1 \ Directory \ application.exe"参数A ArgumentB

What I want: "\\UNCPATH1\Directory\application.exe" argumentA ArgumentB

我得到的:\\ UNCPATH1 \ Directory \ application.exe参数A ArgumentB

What I get: \\UNCPATH1\Directory\application.exe argumentA ArgumentB

当前使用的代码:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\supercoolprogram\mrincredible.lnk")
$Shortcut.TargetPath = "\\UNCPATH1\Directory\application.exe"
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = "\\UNCPATH1\Directory"
$Shortcut.Save()

使用代码示例进行编辑...感谢TheMadTechnician和Speerian都提供了示例.Windows正在从应用程序UNC路径中没有空格的快捷方式中删除目标字段中带引号的路径.这两个代码示例均适用于带有空格的路径.

Edit with Code examples... thanks to TheMadTechnician and Speerian who both had working examples. Windows is stripping quoted paths in the target field from shortcuts that do not have a space in the application UNC path. Both code examples work on paths with spaces.

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\supercoolprogram\mrincredible.lnk")
$Shortcut.TargetPath = "`"\\UNCPATH1\Directory1\application.exe`""
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = '"\\UNCPATH1\Directory1"'
$Shortcut.Save()

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\supercoolprogram\mrincredible.lnk")
$Shortcut.TargetPath = """\\UNCPATH1\Directory 1\application.exe"""
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = "\\UNCPATH1\Directory 1"
$Shortcut.Save()

在第二个示例中,请注意UNC路径中的空格,并从快捷方式属性中的workdirectory中除去了单引号.(Windows会自动在此处添加)

On the second example note the space in UNC path and the removal of the single quotes from workingdirectory in the shortcut attributes. (windows will automatically add here)

推荐答案

将带引号的字符串放在其他引号中,因此"\\ UNCPATH1 \ Directory \ application.exe" 将成为'"\\ UNCPATH1 \ Directory \ application.exe"'.

Place your quoted string within other quotes, so "\\UNCPATH1\Directory\application.exe" will become '"\\UNCPATH1\Directory\application.exe"'.

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:ProgramData\Microsoft\Windows\Start Menu\Programs\supercoolprogram\mrincredible.lnk")
$Shortcut.TargetPath = '"\\UNCPATH1\Directory\application.exe"'
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = '"\\UNCPATH1\Directory"'
$Shortcut.Save()

...而我错了.这确实适用于 WorkingDirectory 属性,但不适用于 TargetPath 属性.的工作是将字符串用三引号引起来.因此,这导致了这一点:

...and I was wrong. This does work for the WorkingDirectory property but not the TargetPath property. What does work is to triple-quote your string instead. So, that leads us to this:

$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$([environment]::GetFolderPath("Desktop"))\mrincredible.lnk")
$Shortcut.TargetPath = """\\UNCPATH1\Directory 1\application.exe"""
$Shortcut.Arguments = "argumentA ArgumentB"
$Shortcut.WorkingDirectory = '"\\UNCPATH1\Directory"'
$Shortcut.Save()

至少在Windows 8.1上运行良好.

Works fine on Windows 8.1 at the very least.

这篇关于在Powershell中为带参数的快捷方式创建带引号的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆