获取当前登录的用户名 [英] Get Currently Logged in username

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

问题描述

我正在尝试使用VBS获取用户的登录用户名。

I am trying to get the logged in username of the user by using VBS.

我尝试了一些直接运行脚本时有效的代码(双击该代码) )

I tried some codes which works when I run the script directly (Double Clicking it)

Set wshNetwork = CreateObject("WScript.Network")
strUser = wshNetwork.Username
WScript.Echo "Current User: " & strUser

但是,我需要做的是使用CMD通过AT命令运行计划任务。
运行时,用户名将是计算机的名称,而不是登录的用户。

However, what I need to do is to use CMD to run a scheduled task using the AT command. When it ran, the username would be the computer's name instead of the logged in user.

当我以管理员身份运行CMD并使用wscript时,也会出现此问题运行脚本。

This problem also occurs when I run CMD as administrator and use wscript to run the script.

有什么方法可以绕过此上下文并获得登录用户,而不是运行脚本的用户吗?

Is there any way to bypass this context and get the logged in user instead of the one that runs the script?

推荐答案

命令

query session console

应提供所需的内容

为了更容易解析

quser console

已编辑-包含的示例vbs代码

EDITED - Included sample vbs code

Dim strCmd
    strCmd = "cmd /q /c ""quser console | find /i ""console"" "" "

Dim buffer 
    buffer = WScript.CreateObject("WScript.Shell").Exec(strCmd).StdOut.ReadAll()

Dim c, consoleUserName
    c = InStr(buffer,"console")
    If c > 2 Then
        consoleUserName = Trim(Mid(buffer,2,c-2))
    Else
        consoleUserName = ""
    End If

    WScript.Echo consoleUserName

这篇关于获取当前登录的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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