简体   繁体   English

Powershell在gpedit脚本中获取用户名

[英]Powershell get Username in gpedit Script

I'm using the gpedit.msc Scripts for Login/Logout/Shutdown/Start events. 我正在使用gpedit.msc脚本进行登录/注销/关机/启动事件。 The following PowerShell script runs on those events: 以下PowerShell脚本在这些事件上运行:

$web = New-Object System.Net.WebClient
[String] ${stUserDomain},[String]  ${stUserAccount} = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name.split("\")
$str = $web.DownloadString("https://srv4923/event.php?username=" + ${stUserAccount} + "&workstation=" + $env:ComputerName + "&event=" + $args[0])
$str

The PHP script then generated an entry in a MySQL DB. 然后,PHP脚本在MySQL数据库中生成一个条目。 The script works perfectly fine for login and logout events but for shutdown/start events the username is not "myusername" anymore but it's "SYSTEM". 该脚本对于登录和注销事件非常有效,但是对于关机/启动事件,用户名不再是“ myusername”,而是“ SYSTEM”。 Can that prevented somehow by getting the username in a different way? 可以通过其他方式获取用户名来防止这种情况吗? I understand that when the boot event is triggered the user may not be logged in and therefore there is no username available. 我了解触发启动事件时,用户可能无法登录,因此没有可用的用户名。 But when the user shuts the machine down or reboots it, we should still know who did? 但是当用户关闭机器或重新启动机器时,我们仍然应该知道是谁做的?

As for this... 至于...

"...shutdown/start events ... myusername" anymore but it's "SYSTEM"... “ ...关闭/启动事件... myusername”不再是,但是它是“ SYSTEM” ...

This is not a PowerShell code issue, but an OS by design requirement process. 这不是PowerShell代码问题,而是按设计要求的操作系统。

The simple answer is nope, as there will never be a user at startup. 简单的答案是不,因为启动时永远不会有用户。 It is always SYSTEM and has to be. 它始终是SYSTEM,必须存在。 The Windows MSGINA.dll does not kick in until the system is in a ready state, then it presents the MSGINA logon for the user, whether you decided to use a auto logon or not. 在系统处于就绪状态之前,Windows MSGINA.dll不会启动,然后无论用户是否决定使用自动登录,它都会为用户提供MSGINA登录。

You have to correlate the logoff and shutdown events to get who was last logged on before the shut down. 您必须关联注销和关闭事件,以获取关闭之前最后登录的用户。

Yet remember and admin can remote shutdown the system as can a scheduled task or even a power outage or electrical system surge. 但是请记住,管理员可以像计划任务甚至断电或电气系统电涌一样远程关闭系统。 So, even with that correlation, you have to keep that in mind. 因此,即使存在这种关联,也必须牢记这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 powershell 中的脚本以打开并更改 gpedit 中的值(组策略编辑器) - script in powershell to open and change a value in gpedit (group policy editor) PowerShell:Get-NetTCPConnection脚本,还显示用户名和进程名 - PowerShell: Get-NetTCPConnection script that also shows Username & Process Name GPEdit Powershell命令屏蔽tcp端口 - GPEdit Powershell command to block tcp ports SCCM Powershell脚本中的$ ENV:USERNAME - $ENV:USERNAME in SCCM Powershell script 将用户名和密码添加到Powershell脚本 - Add username and password to Powershell script 使用用户名并检查安全组的Powershell脚本 - Powershell Script that takes username and checks security groups Powershell上的CURL脚本从HTTPS URL获得响应,并且还希望提供用户名/密码 - CURL script on Powershell to get a Response from HTTPS URL and also want to feed Username/Password Powershell 脚本 - 根据用户名或电子邮件地址从 AD 获取可分辨名称 - Powershell script - get Distinguised Name from AD based on either username OR e-mail address Powershell脚本需要调用域用户名和密码 - Powershell script required domain username and password to be invoked Powershell GET-PSSession 输出用户名作为列表框 - Powershell GET-PSSession output Username as Listbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM