batch-file - 截断 SystemInfo / Boot Time 输出并设置为变量?
问题描述
我正在制作一个批处理脚本来检查本地联网 PC,并希望将每台 PC 的启动时间或正常运行时间设置为一个变量,以便我可以随意显示数据。
我使用此命令打印到屏幕:
SystemInfo /s PC_NAME | find "Boot Time:"
并得到这个结果:
System Boot Time: 27/09/2019, 9:15:16 AM
但我只想将“27/09/2019, 9:15:16 AM”部分设置为变量。
谢谢
解决方案
请注意 systeminfo 已本地化。所以“引导时间”只适用于英文版的 Windows。
因此,最好将 WMIC 用于任何系统语言,如下面的代码:
@echo off
Title Last Boot Time
for /f %%a in ('WMIC OS GET lastbootuptime ^| find "."') DO set "DTS=%%a"
set "BOOTTIME=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2% %DTS:~8,2%:%DTS:~10,2%"
echo DTS : %DTS%
echo BOOTTIME : %BOOTTIME%
pause
如果你喜欢这样的 vbscript:
Option Explicit
' Declare variables
Dim colItems, objItem, objWMIService,ws,Title
Dim strBoot, strBootDate, strBootDay, strBootHour, strBootMins
Dim strBootMonth, strBootTime, strBootYear, strComputer, strMsg, strQuery
Title = "La dernière heure de démarrage de votre ordinateur by Hackoo 2018"
Set ws = CreateObject( "WScript.Shell" )
strComputer = ws.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
If IsEmpty(strComputer) Then
WScript.quit()
ElseIf strComputer = "" Then
strComputer = "."
End If
' Connect to specified computer
Set objWMIService = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
Set colItems = objWMIService.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
For Each objItem in colItems
strBootYear = Left( objItem.LastBootUpTime, 4 )
strBootMonth = Mid( objItem.LastBootUpTime, 5, 2 )
strBootDay = Mid( objItem.LastBootUpTime, 7, 2 )
strBootDate = DateValue( strBootDay & "-" & strBootMonth & "-" & strBootYear )
strBootHour = Mid( objItem.LastBootUpTime, 9, 2 )
strBootMins = Mid( objItem.LastBootUpTime, 11, 2 )
strBootTime = strBootHour & ":" & strBootMins
strBoot = strBootDate & ", " & strBootTime
strMsg = "La dernière heure de démarrage du PC : " & chr(34) & strComputer & chr(34) & " est : " &_
vbCrlf & strBoot
Next
' Display results
MsgBox strMsg,vbInformation,Title
'Done
WScript.Quit(0)
推荐阅读
- ruby-on-rails - Heroku Rails 6 multiple connection not working
- python - 文件“C:\Python38-32\lib\os.py”,第 411 行语法错误
- react-native - Expo - AppLoading threw an unexpected error when loading
- php - 如何增加会话数组值?
- c# - 使用 Entity Framework sql 将 Null 插入数据库
- python - Upload multi img to 1 post on Django ? How do that?
- python - 使用 Python z3 API 简化方程
- android - Viewpager inside recyclerView dissapears when scrolling up or down
- c++ - 使用填充构造函数初始化向量的向量
- javascript - 在一个功能中加载图像