我需要通过用户个人资料文件夹\ computer \ C $ \ users搜索工作组计算机列表。我不总是知道完整的登录名或帐户的命名约定,因此需要调用通配符,例如Simon Heffner可能有一个Heffensi,Simonhe或Heffnsi的登录文件夹(根据命名惯例),事物的类型。
使用用户文件夹中某个文件的上次修改日期我想列出他在脚本运行时可能登录的计算机,或者至少在过去两周内列出以防他们使用的计算机/分享多个。
研究告诉我这个文件是登录活动的一个很好的标识符。
"\\%%A\C$\Users\*%search%*\AppData\Local\Microsoft\Windows\UsrClass.dat"
我有一些有效的代码,但到目前为止还需要改进。
:start
CLS
echo.
echo ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set /p search=Input search string:-
for /f "usebackq tokens=*" %%A in ("%~dp0hostsfs.txt") do (
if exist "\\%%A\C$\Users\*%search%*" echo "%search% string was found in users folder on %%A" && powershell -Command "& Get-ChildItem "\\%%A\C$\Users\*%search%*\AppData\Local\temp"
)
pause & goto start
这可以按预期工作,但不使用我之前引用的文件(usrclass.dat),如果用户已经登录到多台PC(通常是这样) power-shell命令的输出过于冗长,无序,并且很难找到最近使用的配置文件。如果我将(usrclass.dat)文件添加到脚本中,则power-shell命令不会执行任何操作,似乎会跳过它。
我想要一些帮助: -
让power-shell与(usrclass.dat)文件一起使用,以获得更准确的上次登录报告。
- 输出示例 -
"test string was found in users folder on TEST-PC2"
Directory: \\TEST-PC2\C$\Users\test\AppData\Local
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 09/11/2016 21:40 Temp
"test string was found in users folder on TEST-PC3"
Directory: \\TEST-PC3\C$\Users\test\AppData\Local
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 09/11/2015 21:40 Temp
"test string was found in users folder on TEST-PC4"
Directory: \\TEST-PC4\C$\Users\test\AppData\Local
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 09/11/2013 21:40 Temp
- 期望的输出示例 -
"test string was found in users folder on TEST-PC2"
User LastWriteTime
---- -------------
test 11/11/2016 5:44:34 PM
test 1/15/2015 5:44:34
"test string was found in users folder on TEST-PC3"
User LastWriteTime
---- -------------
test 1/15/2015 5:44:34
"test string was found in users folder on TEST-PC4"
User LastWriteTime
---- -------------
test 1/15/2014 5:44:34
所以从这里我可以很容易地看到测试PC2很可能是他在网络上使用的PC,然后我可以完成任务(无声地加载软件等)到那台PC,而不是他明显没有的#&# 39;使用。
echo ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set /p search=Input search string:-
for /f "usebackq tokens=*" %%A in ("%~dp0hostsfs.txt") do (
For /d %%i in ("\\%%A\C$\Users\*%search%*") DO Psexec \\%%A -i net user %%i | find "Last logon"
)
:start
cls
echo.
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set /p search=Input search string:-
for /f "usebackq tokens=*" %%A in ("%~dp0hosts.txt") do (
echo.
For /d %%i in ("\\%%A\C$\Users\*%search%*") DO echo %%~nxi found on %%A & Psexec \\%%A net user %%~nxi | find "Last logon"
)
pause & goto start
进一步编辑,这样可以更容易阅读和分隔多个条目
:start
cls
echo.
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set /p search=Input search string:-
for /f "usebackq tokens=*" %%A in ("%~dp0hosts.txt") do (
echo.
For /d %%i in ("\\%%A\C$\Users\*%search%*") DO (
echo %%~nxi found on %%A & (
echo.
%~dp0Psexec.exe \\%%A net user %%~nxi | find "Last logon")
)
)
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
pause & goto start
答案 0 :(得分:0)
最终答案,格式正确
:start
cls
echo.
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set /p search=Input search string:-
for /f "usebackq tokens=*" %%A in ("%~dp0pxhosts.txt") do (
echo.
For /d %%i in ("\\%%A\C$\Users\*%search%*") DO (
echo %%~nxi found on %%A & (
echo.
%~dp0Psexec.exe \\%%A net user %%~nxi | find "Last logon")
)
)
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
pause & goto start
答案 1 :(得分:0)
我放在一起产生非常简洁易读的输出的另一种方法是查询用户配置文件usrclass.dat文件的修改日期(每次登录时更新)。
使用PSEXEC的上一个答案可能过于冗长,并且即使找到用户进行通配符搜索也会一直报告错误。很难在几台PC上找到多个条目。
用主机名列表填写pxhosts&跑。只要找到PC用户名上次修改数据(登录),就可以获得很好的列表。
:start
cls
echo.
echo ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set /p search=Input search string:-
echo.
for /f "usebackq tokens=*" %%A in ("%~dp0pxhosts.txt") do (
for /d %%i in ("\\%%A\C$\Users\*%search%*") DO (
for %%g in ("%%i\AppData\Local\Microsoft\Windows\usrclass.dat") DO Echo [PC]'%%A' [User]'%%~nxi' [Last Login]'%%~tg'
)
)
echo.
echo ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
pause & goto start
示例输出: -
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Input search string:-adm
[PC]'DESKTOP-6285F8K' [User]'Admin' [Last Login]'09/11/2016 22:08'
[PC]'DESKTOP-6285F8K' [User]'Administrator' [Last Login]'25/10/2016 20:33'
[PC]'SRV-2008' [User]'admin' [Last Login]'21/11/2016 20:36'
[PC]'SRV-2008' [User]'Administrator' [Last Login]'21/11/2016 13:32'
[PC]'TEST-PC2' [User]'Admin' [Last Login]'21/11/2016 20:21'
[PC]'TEST-PC2' [User]'Administrator' [Last Login]'21/11/2016 20:48'
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Press any key to continue . . .