Opening a program with PowerShell from a shortcut

纵饮孤独 提交于 2020-01-02 08:31:09

问题


I have a problem with creating a shortcut that opens a certain program in Powershell. It is part of a small game server. Therefore the server folder needs to be able to be copied, to create another server.

The program needs to be run in administrator mode. It needs to work for both cmd an powershell (I don't mind if they are 2 different shortcuts due to syntax). What I have so far:

CMD:

%SystemRoot%\System32\cmd.exe /c D: & cd "D:\Path\to\server\folder\" & Server.exe

PowerShell:

%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command "cd 'D:\Path\to\server\folder\';.\Server.exe"

The problem is that these paths aren't relative, so if I move the server folder, I would need to change the shortcut's target by replacing the path in the cd command.

It would be easier if I could use relative paths (the shortcut is located in the same folder as server.exe), but both the cmd as the PowerShell shortcut start in system32, because it is run in admin mode.

What can I do to make it work by creating at most 1 file for PS and 1 file for cmd (lnk, bat, ps1, I don't care)

EDIT: I also tried the following, but it didn't work:

C:\Windows\System32\cmd.exe /k cd & runas /user:<machine name>\<username> server.exe

the problem is that first of all it asks me the password of the account (it is the same account), which is annoying. Secondly it opens the server in a separate window: one where I can't scroll or rightclick. And lastly, the folder it operates in is wrong, because it can not find files that are in the folder it should operate in.

Does anyone have a better idea?


回答1:


After a lot of trying and a lot of blood, sweat and tears I finally found a workaround to make it happen. This is the only solution I came up with and the cmd version I am not completely happy with. Furthermore it includes a few temporary files.

Result for cmd:

startServerCMD.bat

SET currdir=%CD%
SET currdrive=%CD:~0,2%
echo %currdir%
echo %currdrive%
echo %currdrive% > temp.bat
attrib +H temp.bat
echo cd "%currdir%" >> temp.bat
echo server.exe >> temp.bat
shelexec\Release\ShelExec /verb:runas "temp.bat"
del /A:H temp.bat

I then wrote a program in c with icon that executes startServerCMD.bat

Shelexec.exe is a program that I found on the web that lets me run programs from the command line. I didn't find a way to get it to work with the runas command without lots of trouble and filling in a password, so this is a good alternative.

The only problem is that this opens a second cmd window. That is not a problem, as the first will close after it is done with the last line of the bat, but for some reason the first cmd window hangs for a couple of seconds, before it quits.

Lastly I can't get the icon to work for the new cmd window, since it runs the bat file, which has no icon.

Result for PowerShell:

This was a bit more difficult, but I have found a way. It even works more fluently and has a working icon.

startServerPS.ps1

$currdir=$(get-location)
$target = ".\startServer.lnk"
echo "Set oWS = WScript.CreateObject(`"WScript.Shell`")" > temp.vbs
echo $("sLinkFile = `"" + $target + "`"") >> temp.vbs
echo "Set oLink = oWS.CreateShortcut(sLinkFile)" >> temp.vbs
echo "oLink.TargetPath = `"%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe`"" >> temp.vbs
echo $("oLink.Arguments = `"-Command `"`"cd '" + $currdir + "';.\server.exe`"`"`"") >> temp.vbs
echo $("oLink.IconLocation = `"" + $currdir + "\server.ico, 0`"") >> temp.vbs
echo "oLink.Save" >> temp.vbs
cscript temp.vbs
del temp.vbs
attrib +H ".\startServer.lnk"
shelexec\Release\ShelExec /verb:runas "startServer.lnk"

So... This is a PowerShell script that creates a VB script that creates a shortcut which opens the server and then the PowerShell script opens that shortcut with shelexec like before to run it in administration mode. It isn't called a workaround for nothing :D

For easy running (without rightclicking, a shortcut is created for startServerPS.ps1, but not directly. The target is C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& '.\startServerPS.ps1'"




回答2:


If you want the answer that isn't a workaround but uses c-code:

code for CMD:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    system("C:\\Windows\\System32\\cmd.exe /k server.exe");
    return 0;
}

code for PowerShell:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    system("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -noexit -Command \"& '.\\server.exe'\"");
    return 0;
}

compile and run in admin mode. Now there is weirdly enough no problem with the current folder.

It depends on your own preference if you want to have /c or /k (for cmd) and if you want -noexit (for PS)




回答3:


Fortunately there is an easy answer that will work for both options.

  1. Create the shortcut to launch the program or script (no changing directory stuff needed)
  2. Open up the properties of the shortcut.
  3. Clear the Start In: box so that it is empty. Windows will then default to starting the program in the current directory.


来源:https://stackoverflow.com/questions/20807603/opening-a-program-with-powershell-from-a-shortcut

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