通过批处理文件以管理员 Powershell 身份启动

nka*_*sco 4 powershell batch-file

因此,我正在运行一个基本脚本来将快捷方式复制到公共配置文件桌面,以便任何登录的用户都可以在其桌面上看到该快捷方式。情况是我将不得不绕过执行策略,所以我通过批处理文件来做到这一点。这是我尝试过的,但似乎对我不起作用......

Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList 'ExecutionPolicy Bypass -File DesktopShortcut.ps1' -Verb RunAs}"
Run Code Online (Sandbox Code Playgroud)

PS文件很简单:

Copy-Item -Path "aiStarter.lnk" -Destination "C:\Users\Public\Desktop\" -PassThru
Run Code Online (Sandbox Code Playgroud)

当我运行它时,窗口只是闪烁然后消失。如果我在没有 RunAs 的情况下运行它,我会拒绝访问。我不想问这个,因为我确定以前有人问过这个问题,但我很确定我正确地执行了这个问题。想法?

Pat*_*cke 5

就像其他一些人所说的那样,这不是提供快捷方式的最佳选择。

话虽如此,有两个问题。一个是需要-执行策略。另一种是在创建提升的 powershell 实例后工作路径发生变化。所以你需要添加脚本的完整路径,%~dp0如果它是一个批处理文件,你可以使用它。

Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0DesktopShortcut.ps1' -Verb RunAs}"
Run Code Online (Sandbox Code Playgroud)

之后可能需要对 powershell 脚本进行同样的处理。

Copy-Item -Path "$($MyInvocation.MyCommand.Path)\aiStarter.lnk" -Destination "C:\Users\Public\Desktop\" -PassThru
Run Code Online (Sandbox Code Playgroud)

这假设批处理文件、快捷方式和 powershell 脚本都在同一个文件夹中。