且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PowerShell从快捷方式打开程序

更新时间:2023-02-03 20:53:24

如果你想要的答案不是一个解决方法,但使用c代码:



CMD代码

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

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

strong>

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

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

编译并在管理模式下运行。



这取决于你自己的偏好,如果你想拥有/ c或/ k(对于cmd),如果你想要-noexit(for PS)


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?

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)