Add mstsc to shortcut target powershell

谁都会走 提交于 2019-12-12 06:48:24

问题


A little background about what I'm trying to do and why. We are slowly migrating from using a local copy of office on our end users Win7 machines to publishing office through RDS 2012. With 2012 you can have the end users machine subscribe to a webfeed which puts the shortcuts to the actual RDP files in Appdata. In order to have our image pretty much mirror before RDS, I need to pin the shortcuts to the taskbar. If you pin the shortcut as it comes from the RDS Gateway server the icon on the taskbar is that of the .rdp file. If you edit the target for the shortcut and put mstsc.exe before the path to the .rdp file you can then pin the shortcut to the taskbar using those icons of the shortcut.

I have found posts on how to change the target field of shortcuts but nothing on how to add something to what is currently there. An environment variable is needed since the path to the shorts will be different for each user. Below is I have tried thus far

      $Word = $env:userprofile + "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\RemoteApp and Desktop Connections\Microsoft Word 2010.lnk"

      $sh = New-Object -COM WScript.Shell
      $targetPath = $sh.CreateShortcut($Word).TargetPath
      $sh.TargetPath = "mstsc.exe" + $targetPath ## Make changes
      $sh.Save()  ## Save$shell = New-Object -COM WScript.Shell

One of the errors i'm getting is : Property 'Arguments' cannot be found on this object; make sure it exists and is settable.

Any help will be greatly appreciated.


回答1:


Instead of using Shell COM object, how about using a .Net wrapper class? There is a great sample.

To use the VBAccelerator's wrapper in Powershell, extract the source code and compile a DLL like so,

C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:library /out:ShellLink.dll /r:System.Drawing.dll .\ShellLink.cs .\FileIcon.cs

This should create ShellLink.dll, which you can add as Powershell type like so,

Add-Type -path .\ShellLink.dll

And use the class by creating a new object like so,

$lnk = new-object vbAccelerator.Components.Shell.ShellLink
$lnk.Target = "C:\Windows\System32\mstsc.exe"
$lnk.Arguments = "some arguments"
$lnk.Description = "My awesome shortcut"
$lnk.Save("c:\temp\test.lnk")



回答2:


Your code will prepend "mstsc.exe" to the current target path if you just add this line before your ## Make changes line:

$sh = $sh.CreateShortcut($word)

Sounds like you also want to add a space, so that your lnk file is the "connection file" argument of mstsc.exe. You can set the Arguments property like so:

function Set-RDSshortcut {
    param( [parameter(mandatory=$true)]$Shortcut )
    $sh = New-Object -COM WScript.Shell
    $targetPath = $sh.CreateShortcut($Shortcut).TargetPath
    $targetPath = "mstsc.exe"
    $sh = $sh.CreateShortcut($Shortcut)
    $sh.TargetPath = $targetPath
    $sh.Arguments = $Shortcut
    $sh.Save() 
} #end function


来源:https://stackoverflow.com/questions/20789591/add-mstsc-to-shortcut-target-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!