通过批处理文件作为Admin Powershell启动 [英] Launch as Admin Powershell via Batch File

查看:64
本文介绍了通过批处理文件作为Admin Powershell启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am running a basic script to copy a shortcut to the public profile desktop so that any user who logs in will have that on their desktop. The circumstances are that I will have to bypass execution policies, so I am doing this via batch file. This is what I have tried but it doesnt seem to work for me...

Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList 'ExecutionPolicy Bypass -File DesktopShortcut.ps1' -Verb RunAs}"

而PS文件就是:

Copy-Item -Path "aiStarter.lnk" -Destination "C:\Users\Public\Desktop\" -PassThru

当我运行它时,窗口会闪烁然后消失.如果我在没有RunAs的情况下运行它,则访问将被拒绝.我讨厌问这个问题,因为我确定以前已经有人问过这个问题,但是我很确定自己执行正确.有想法吗?

When I run it the window just flashes then disappears. If I run it without RunAs I get access denied. I hate to ask this because I'm sure it has been asked before but I am pretty sure I am executing this correctly. Thoughts?

推荐答案

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

Like a few others have said, this isn't the best choice for delivering shortcuts.

话虽如此,有两个问题.一种是在执行策略上需要-.另一个是在创建提升的Powershell实例后,工作路径会更改.因此,您需要向脚本添加完整路径,如果它是批处理文件,则可以使用%〜dp0 进行操作.

That being said, there are two issues. One is needing a - at execution policy. The other is after the elevated powershell instance is created the working path changes. So you need to add the full path to the script, which you can do with %~dp0 if it's a batch file.

Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0DesktopShortcut.ps1' -Verb RunAs}"

此后,可能需要使用powershell脚本进行相同的操作.

Same likely needs to be done with the powershell script afterwards.

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

这假定批处理文件,快捷方式和powershell脚本都在同一文件夹中.

This assumes the batch file, the short cut, and the powershell script are all in the same folder.

这篇关于通过批处理文件作为Admin Powershell启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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