Tuesday, October 23, 2012

Create Windows Shortcuts for Remote Development

A couple months ago I decided to posted about manually creating shortcuts for handling remote development. Below is a function that will create a shortcut and will attach an icon to the shortcut.
function Create-Shortcut([string] $shortcutName, [string] $target, [string] $username, [string] $iconTarget, [string] $workingDirectory="") {
 $WshShell = New-Object -comObject WScript.Shell;
 $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\$shortcutName`.lnk");
 Write-Host "C:\Windows\System32\runas.exe /user:$username /netonly `"$target`"";
 $Shortcut.TargetPath = "C:\Windows\System32\runas.exe"
 $Shortcut.Arguments = "/user:$username /netonly `"$target`"";
 if ([string]::IsNullOrEmpty($iconTarget)) {
  $Shortcut.IconLocation = "$target, 0";
 } else {
  $Shortcut.IconLocation = $iconTarget;
 }
 $Shortcut.WorkingDirectory = $workingDirectory;
 $Shortcut.Save();
}
Here is the full script that will do the heavy lifting. The only limitation is that you still need to set the shortcuts to "Run as administrator" in the advanced settings. I have not found a successful way to programatically set the flag, so for now, it will have to be a manual process.
param(
 [string] $ProjectPrefix = "TEST", # $(Read-Host "Project Prefix"),
 [string] $userName = "domain\user" # $(Read-Host "Username")
)

function Main
{
 Create-Shortcut "$ProjectPrefix Visual Studio 2010" "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" $userName;
 Create-Shortcut "$ProjectPrefix SQL Server Management Studio" "C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\Ssms.exe" $userName;
 Create-Shortcut "$ProjectPrefix Visual Studio Command Line" "%comspec% /k `"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat`" x86" $userName -iconTarget "%comspec%, 0";
 Create-Shortcut "$ProjectPrefix LINQPad 4" "C:\Program Files (x86)\LINQPad\LINQPad.exe" $userName;
 Create-Shortcut "$ProjectPrefix Internet Explorer" "C:\Program Files (x86)\Internet Explorer\iexplore.exe" $userName;
}

function Create-Shortcut([string] $shortcutName, [string] $target, [string] $username, [string] $iconTarget, [string] $workingDirectory="") {
 $WshShell = New-Object -comObject WScript.Shell;
 $Shortcut = $WshShell.CreateShortcut("$Home\Desktop\$shortcutName`.lnk");
 Write-Host "C:\Windows\System32\runas.exe /user:$username /netonly `"$target`"";
 $Shortcut.TargetPath = "C:\Windows\System32\runas.exe"
 $Shortcut.Arguments = "/user:$username /netonly `"$target`"";
 if ([string]::IsNullOrEmpty($iconTarget)) {
  $Shortcut.IconLocation = "$target, 0";
 } else {
  $Shortcut.IconLocation = $iconTarget;
 }
 $Shortcut.WorkingDirectory = $workingDirectory;
 $Shortcut.Save();
}

& Main;

No comments: