从提升的会话

时间:2016-11-29 10:27:44

标签: powershell

如果从高架会话运行,我有一些工具不起作用。

我正在运行提升会话(在管理intall的上下文中,所以这是必需的)所以这是不可更改的。

除了创建计划任务并执行它之外,还有其他更快或更友好的东西吗?

===编辑===

这是需要无法运行的命令: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}

2 个答案:

答案 0 :(得分:0)

Function Run-NonElevated([string]$Cmd) {
    $Cmd | Out-File -Encoding AscII -FilePath "$Env:Temp\Run-NonElevated.cmd"
    Explorer "$Env:Temp\Run-NonElevated.cmd"
}

Run-NonElevated "vboxmanage list runningvms"

答案 1 :(得分:0)

最后使用此功能:

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
}

仍然想知道没有任务调度程序的解决方案。