【发布时间】:2020-02-26 21:20:31
【问题描述】:
如果我在控制台窗口或 ISE 中运行以下 sn-p,它会按预期工作,列出本地计算机上的活动用户会话:
(Invoke-Expression "$env:windir\system32\quser.exe") -replace '\s{2,}', ',' | ConvertFrom-Csv
很遗憾,这不是控制台应用程序,而是作为服务安装的 PowerShell 脚本。该服务作为 LocalSystem(不是 LocalService)运行。当服务尝试运行此代码时,它会输出以下错误:
The term 'C:\Windows\system32\quser.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
我联系了 Sapien 支持并被告知 服务在没有配置文件的情况下运行,并且没有对系统文件夹的执行访问权限。您需要授予服务帐户对 exe 及其支持 DLL 的执行权限,以及使用 EXE 的完整路径。
我已经证明(我认为)安全主体可以访问 quser。我使用 PSExen 打开了一个作为 LocalSystem 运行的 PowerShell 控制台,并成功运行了 quser 应用程序:
问题一定是我作为服务运行。有谁知道我如何在服务中访问/使用 QUser?
我想真正的问题是,作为 LocalSystem 运行的服务如何在系统文件夹中执行应用程序?
【问题讨论】:
-
cmdlet“New-ServiceEvent”从何而来?我怀疑它没有运行 quser 命令,或者在服务凭据下找不到它的路径。我会在函数中添加一个成绩单(Start-Transcript/Stop Transcript),这样你就可以看到哪里出错了。
-
New-ServiceEvent是脚本中其他地方的日志功能,它可以完美运行并记录错误Cannot bind argument to parameter 'Message' because it is an empty string.。问题是$QUserToStringOutput是空的。这意味着这不起作用:$QUserToStringOutput = cmd.exe /C $Env:SystemRoot\System32\quser.exe 2>$null -
如果你添加一个脚本,它会告诉你在尝试 "$QUserToStringOutput=" 行时发生了什么,就像你在控制台中运行它一样。你也试过“$QUserToStringOutput = & quser.exe”吗?
-
对不起,我应该说,这是一个很好的建议 :) 我现在正在这样做
-
我试过
& quser.exe,它说找不到。然后我尝试了$QUserToStringOutput = & $ENV:SystemRoot\System32\quser.exe 2>$null,它返回了The term 'C:\Windows\System32\quser.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.。我有点迷茫,为什么这在服务中不起作用,但在 ISE/控制台中起作用?
标签: powershell cmd windows-services windows-console