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

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

问题描述

我有以下 powershell 代码调用 WSHShell,它将在 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 路径中的空格以及快捷方式属性中工作目录中单引号的删除.(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天全站免登陆