本文介绍了从提升的会话启动非提升的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果从提升的会话运行,我有一些工具不起作用.
I have some tool that doesn't work if it runs from elevated session.
我正在运行提升的会话(在管理安装的上下文中,所以这是强制性的)所以这是不可更改的.
I am running elevated session (in the context of administrative intall so this is mandatory) so that is not changable.
除了创建计划任务并执行它之外,还有其他更快或更友好的方法吗?
Other then creating scheduled task and executing it, is there anything else that is faster or friendlier?
=== 编辑 ===
这是需要unlevated run的命令:vboxmanage list runningvms
This is the command that needs unelevated run: vboxmanage list runningvms
我试过了
runas /trustlevel:0x20000 "powershell.exe -noprofile -noexit -command iex 'vboxmanage list runningvms'"
它什么都不返回
此代码有效:
function Run-NonElevated( [string] $cmd ) {
$task_name = "Run-NonElevated-$(New-Guid)"
schtasks /Create /RU $Env:USERNAME /TN $task_name /SC ONCE /ST 00:00 /F /TR $cmd
schtasks /run /tn $task_name
schtasks /delete /F /tn $task_name
}
Run-NonElevated "powershell -NoProfile -Command 'vboxmanage list runningvms | Out-File $Env:TEMP\re.out'"
cat C:\Users\majkinetor\AppData\Local\Temp\re.out
"test machine" {31409fff-c195-4a83-ab12-96bba020e051}
推荐答案
最后使用这个函数:
function Start-ProcessNonElevated( [string] $Cmd, [switch]$UsePowerShell ) {
$svc = gsv Schedule -ea 0
if ($svc -and $svc.Status -ne 'Running') { throw 'Start-ProcessNonElevated requires running Task Scheduler service' }
$res = @{}
$tmp_base = [System.IO.Path]::GetTempFileName()
$tmp_base = $tmp_base -replace '\.tmp$'
$tmp_name = Split-Path $tmp_base -Leaf
$task_name = "Start-ProcessNonElevated-$tmp_name"
Write-Verbose "Temporary files: $tmp_base"
if ($UsePowershell) {
@(
'$r = "{0}"' -f $tmp_base
". {{`n{0}`n}} >`"`$r.out.log`" 2>`"`$r.err.log`"" -f $Cmd
) -join "`n" | Out-String | Out-File "$tmp_base.ps1"
$cmd = "powershell -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -NoLogo -NonInteractive -File '$tmp_base.ps1'"
}
Write-Verbose "Creating scheduled task for command:`n$cmd"
schtasks.exe /Create /RU $Env:USERNAME /TN $task_name /SC ONCE /ST 00:00 /F /TR $cmd *> "$tmp_base.schtasks.log"
schtasks.exe /run /tn $task_name *>> "$tmp_base.schtasks.log"
Write-Verbose 'Waiting for scheduled task to finish'
do {
$status = schtasks /query /tn $task_name /FO csv | ConvertFrom-Csv | select -expand Status
sleep 1
}
until ($status -eq 'Ready')
schtasks.exe /delete /F /tn $task_name *>> "$tmp_base.schtasks.log"
if ($UsePowershell) {
$res = @{
out = cat "$tmp_base.out.log" -ea 0
err = cat "$tmp_base.err.log" -ea 0
}
}
return $res
}
仍然想知道没有任务调度程序的解决方案.
Would still like to know solution without task scheduler.
这篇关于从提升的会话启动非提升的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!