以当前登录的用户身份运行 powershell 命令 [英] Run powershell command as currently logged in user

查看:43
本文介绍了以当前登录的用户身份运行 powershell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个作为本地系统 (nt authority\system) 运行的 powershell 脚本.
有没有办法以当前登录的用户身份执行命令(当然不指定用户密码)?
到目前为止,我所试验的是采用 -User 参数的 Register-ScheduledTask cmdlet.任务已计划并成功运行,但仅在用户登录时才有效.
有没有更好的方法?

Given a powershell script that runs as Local System (nt authority\system).
Is there a way to execute a command as the currently logged in user (without specifying the user password of course) ?
From what I've experimented so far is the Register-ScheduledTask cmdlet which takes an -User param. The task was scheduled and run successfully, but only works when the user is logged in.
Is there a better way to do it ?

推荐答案

我使用 schtasks.exe.我不确定这是否可以在纯 PS 中完成.

I use schtasks.exe. I am not sure if this can be done in pure PS.

$user = Get-WmiObject -Class win32_computersystem | % Username

$computer = $env:COMPUTERNAME

$time = (Get-Date).AddMinutes(1).ToShortTimeString()

schtasks /create /s $computer /tn "RunCMD" /sc once /tr "cmd.exe" /st $time /ru $user

这篇关于以当前登录的用户身份运行 powershell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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