在powershell中使用参数为快捷方式创建引用路径

r9f1avp5  于 2023-03-30  发布在  Shell
关注(0)|答案(2)|浏览(130)

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

所需内容:“\UNCPATH1\Directory\application.exe”参数A参数B
我得到的:\UNCPATH1\Directory\application.exe参数A参数B
目前使用的代码:

$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路径中没有空格的快捷方式中删除目标字段中的引用路径。这两个代码示例都可以在有空格的路径上工作。

$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路径中的空格,以及在快捷方式属性中从workingdirectory中删除的单引号。(windows将自动添加到这里)

vaj7vani

vaj7vani1#

将你的引号字符串放在另一个引号中,这样"\\UNCPATH1\Directory\application.exe"就变成了'"\\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()

Edit:......我错了。这对WorkingDirectory属性有效,但对TargetPath属性无效。**的作用是用三重引号括起字符串。因此,这导致我们得出以下结论:

$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上运行良好。

fjaof16o

fjaof16o2#

您可以使用`对引号进行转义。它是“~”键上的另一个符号。

$Shortcut.TargetPath = "`"\\UNCPATH1\Directory\application.exe`""

相关问题