findstr

问题描述 投票:0回答:1

我在一个批处理脚本中使用net user命令来查找一个用户的最后登录时间。

net user administrator | findstr /B /C:"Last logon"

结果是这样的。

Last logon                   04/23/2020 9:02 AM

我想只显示日期和时间,并删除了以下内容 Last logon  

我怎么才能实现这个目标呢?

谢谢。

windows batch-file command-line findstr net-use
1个回答
0
投票

你可以选择使用 WMI 来完成这个任务,它应该会提供一个普遍可解析的日期和时间字符串。

如果你想要一个特定的用户。

@Set "UsersName=Administrator"
@For /F Tokens^=2^,4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
 Where "Name='%UsersName%'" Assoc /AssocClass:Win32_NetworkLoginProfile 2^>NUL'
)Do @For /F %%I In ('%__AppDir__%wbem\WMIC.exe Path Win32_NetworkLoginProfile^
 Where "Name='%%G\\%%H' And LastLogon Is Not Null" Get LastLogon 2^>NUL^
 ^|%__AppDir__%findstr.exe "[0123456789]"')Do @Echo %%H last logon was %%~nI
@Pause

或者如果你想要一个用户列表..:

@For /F Tokens^=2^,4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
 Assoc /AssocClass:Win32_NetworkLoginProfile 2^>NUL')Do @For /F %%I In (
    '%__AppDir__%wbem\WMIC.exe Path Win32_NetworkLoginProfile Where^
     "Name='%%G\\%%H' And LastLogon Is Not Null" Get LastLogon 2^>NUL^
     ^|%__AppDir__%findstr.exe "[0123456789]"')Do @Echo %%H last logon was %%~nI
@Pause

[编辑?] 基于你在另一个答案的评论中提出的问题,并且因为我已经指出我上面的解决方案提供了一个普遍可解析的日期和时间格式,你可以进一步调整它以只输出小时。

例如,你可以将它进一步调整为只输出小时。

@For /F Tokens^=2^,4Delims^=^" %%G In ('%__AppDir__%wbem\WMIC.exe UserAccount^
 Assoc /AssocClass:Win32_NetworkLoginProfile 2^>NUL')Do @For /F %%I In (
    '%__AppDir__%wbem\WMIC.exe Path Win32_NetworkLoginProfile Where^
     "Name='%%G\\%%H' And LastLogon Is Not Null" Get LastLogon 2^>NUL^
     ^|%__AppDir__%findstr.exe "[0123456789]"')Do @(Set "YYYYmmDDHHMMSS=%%~nI"
     Call Echo(%%H last logged in during the hour of %%YYYYmmDDHHMMSS:~-6,2%%:00)
@Pause

当然,这并不能确定是哪一天发生的登录,但你的额外要求是特定的!


0
投票

Windows 10 64位。PowerShell 5

如何使用CMD或Powershell显示一个本地所有用户账户的最后登录时间戳。如何显示用户名称和或日期和或时间和或小时。这些命令都不需要管理员权限。

PowerShell。

# How to display the lastlogon timestamp for one / all local user accounts.
# PowerShell 5. https://stackoverflow.com/a/61387391/8826818
# Interactive, searchable, gridview window. All accounts except default accounts named: DefaultAccount, Guest, WDAGUtilityAccount
get-localuser | where {$_.name -notmatch 'defaultaccount|guest|WDAGUtilityAccount'} | select-object lastlogon,name | out-gridview 
# All accounts except default accounts named: DefaultAccount, Guest, WDAGUtilityAccount  
get-localuser | where {$_.name -notmatch 'DefaultAccount|Guest|WDAGUtilityAccount'} | select-object lastlogon,name | format-table -hidetableheaders 
# All local accounts name, date and time. Remove name if not wanted.
$adsi = [ADSI]"WinNT://$env:COMPUTERNAME" 
$adsi.Children | where {$_.SchemaClassName -eq 'user'} | format-table lastlogin,name -HideTableHeaders 
# Account by name:
Get-LocalUser -Name Administrator,_9doug | Select-Object lastlogon,name | Format-Table -HideTableHeaders
# All accounts:
get-localuser | where {$_.name} | select-object lastlogon,name | format-table -hidetableheaders 

交互式的,可搜索的网格视图窗口。

screenshot of gridview window

或者:

Administrator 4/12/2020 7  :  32  :  09 PM
_7doug        11/18/2019 11  :  13  :  53 PM
_8doug        10/25/2019 4  :  47  :  09 PM
_9doug        4/23/2020 6  :  49  :  41 AM

CMD:

rem date and time
for /f "tokens=2,*" %g in ('net user administrator ^| findstr /C:"Last logon"') do echo %h
rem only the hour 
for /f "tokens=4" %g in ('net user administrator ^| findstr /C:"Last logon"') do echo Last logon hour was: %g

脚本:

rem date and time
for /f "tokens=2,*" %%g in ('net user administrator ^| findstr /C:"Last logon"') do echo %%h
rem only the hour 
for /f "tokens=4" %%g in ('net user administrator ^| findstr /C:"Last logon"') do echo Last logon hour was: %%g

不寻常的输出 net user 命令。

screenshot of net user output

对于

QUSER:只对当前用户有效。

CMD:

rem date and time
for /f "skip=1 tokens=6-8" %g in ('quser administrator') do echo %g %h %i
rem only the hour 
for /f "skip=1 tokens=7 delims=: " %g in ('quser administrator') do echo Last logon hour was: %g

脚本。

rem date and time
for /f "skip=1 tokens=6-8" %%g in ('quser administrator') do echo %%g %%h %%i
rem only the hour 
for /f "skip=1 tokens=7 delims=: " %%g in ('quser administrator') do echo Last logon hour was: %%g
© www.soinside.com 2019 - 2024. All rights reserved.