使用PowerShell从快捷方式打开程序 [英] Opening a program with PowerShell from a shortcut

查看:2331
本文介绍了使用PowerShell从快捷方式打开程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建快捷方式时遇到问题,在Powershell中打开某个程序。它是一个小游戏服务器的一部分。因此,服务器文件夹需要能够被复制,以创建另一个服务器。



程序需要以管理员模式运行。它需要工作的cmd一个powershell(我不介意,如果他们是2个不同的快捷方式,由于语法)。我到目前为止:



CMD:

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

PowerShell:

 %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -CommandcdD:\Path\to\server\folder\'; .\Server.exe

问题是这些路径不是相对的,移动服务器文件夹,我需要通过替换cd命令中的路径来更改快捷方式的目标。



如果我可以使用相对路径(快捷方式位于与server.exe相同的文件夹)会更容易,但cmd作为PowerShell快捷方式启动system32,因为它是在管理模式下运行。



我可以通过创建最多1个文件为PS和1个文件为cmd(lnk,bat,ps1,我不在乎) / p>

编辑:我也尝试了以下,但它不工作:

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

问题是,首先它要求我的帐户的密码同一个帐户),这是烦人。其次,它在一个单独的窗口中打开服务器:其中我不能滚动或右键单击。最后,它操作的文件夹是错误的,因为它不能找到文件夹中应该操作的文件。



有没有人有更好的主意? / p>

解决方案

如果你想要的答案不是一个解决方法,但使用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)

这篇关于使用PowerShell从快捷方式打开程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆