0

How do you set the target and NOT the targetPath of a .lnk file ? I can't find any .dll I can use in powershell or c#. Most people might try and tell to use wshshell to set the .targetpath of the .lnk variable. That's not the question that I am asking.

A bit of help from this question as a resource

It would be nice to set the target with environmental variables using powershell... But I can't find anything...Most people keep talking about wshshell, but if you try and use that you'll notice that it doesn't really use environmental variables as a path setting target. In fact forcing it just causes errors saying targetpath is out of range.

Some more resources I found useful and added

Dana M
  • 11

1 Answers1

1

Ok after having gone over some of the docs and procedures for working the wshshell object I have actually come up with a solution that works and that I'll probably employ... at work. It took me a while to realize that I can not actually set the targetpath for an executable using /runas with the Target Location in Sys32s Powershell Folder... Instead... I'll just set the working directory to System32, and the TargetPath to runAs.exe... From the arguments you provide to runAs.exe it is that you provide your extended powershell .ps1 launch arguments.

Here is a sample which launches my gradient app after prompting for your microsoft password....--->

    $myAppName = 'spectrumFormLauncher.ps1'
    $myLinkName = 'myLink.lnk'
    $myAppFolder = 'Spex'
    $myIconName = 'spex.ico'
$cactName =  $env:userprofile +"\documents\"+$myAppFolder
$cactName2 = $env:userprofile + "\desktop"

$SourceExeArg = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$SourceExe = "C:\Windows\System32\runAs.exe"
$myDom = $env:USERDOMAIN
$myU = $env:USERNAME
$Arg = '/profile /user:'+$myDom+"\"+$myU       
$myAppZoned = '"'+$cactName+'\modules\'+$myAppName+'"'
$ArgumentsToParameters = ' "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -WindowStyle hidden -File ' + $myAppZoned
$ArgumentsToSourceExe = $Arg + $ArgumentsToParameters
$DestinationPath = $cactName2+"\" +$myLinkName
$testShortCut = Test-Path $DestinationPath
if($testShortCut -eq $true)
{
    Remove-Item -Path $DestinationPath -Confirm:$false -ErrorAction SilentlyContinue -Recurse
}        
$SourceExe2 ="C:\Windows\System32\"      
$iconSpot = $cactName + "\media\"+$myIconName        
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($DestinationPath)
$Shortcut.IconLocation = $iconSpot
$Shortcut.WorkingDirectory = $SourceExe2
$Shortcut.TargetPath = $SourceExe
$Shortcut.Arguments = $ArgumentsToSourceExe
$Shortcut.Save()

Spex the Gradient App

Where I'm at in the c++ procedure

// TODO: Place code here.
//HRESULT CreateLink(LPCWSTR lpszPathObj, LPCSTR lpszPathLink, LPCWSTR lpszDesc)
// HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPWSTR lpszPath, int iPathBufferSize)
    const wchar_t *lpszPathObj = L"C:\\users\\zero8\\desktop\\go.lnk";
    const char *lpszpathLink = "powershell.exe"; 
    const wchar_t *lpszDesc = L"whatever";
    CreateLink(lpszPathObj, lpszpathLink, lpszDesc); 

enter image description here

enter image description here

Dana M
  • 11