[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[转载代码] PowerShell脚本获取系统运行时间x天x小时x分钟

test.ps1
  1. $os = Get-WmiObject win32_operatingsystem
  2. $uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
  3. $Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes"
  4. Write-Output $Display
复制代码

执行效果:
C:\>powershell -f test.ps1
Uptime: 0 days, 4 hours, 16 minutes


Get-Uptime.ps1
  1. #############################################################################
  2. # Get-Uptime.ps1
  3. # This script will report uptime of given computer since last reboot.
  4. #
  5. # Pre-Requisites: Requires PowerShell 2.0 and WMI access to target computers (admin access).
  6. #
  7. # Usage syntax:
  8. # For local computer where script is being run: .\Get-Uptime.ps1.
  9. # For list of remote computers: .\Get-Uptime.ps1 -ComputerList "c:\temp\computerlist.txt"
  10. #
  11. # Usage Examples:
  12. #
  13. # .\Get-Uptime.ps1 -Computer ComputerName
  14. # .\Get-Uptime.ps1 -ComputerList "c:\temp\computerlist.txt" | Export-Csv uptime-report.csv -NoTypeInformation
  15. #
  16. # Last Modified: 3/20/2012
  17. #
  18. # Created by
  19. # Bhargav Shukla
  20. # http://blogs.technet.com/bshukla
  21. # http://www.bhargavs.com
  22. #
  23. # DISCLAIMER
  24. # ==========
  25. # THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
  26. # RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
  27. #############################################################################
  28. #Requires -Version 2.0
  29. param
  30. (
  31. [Parameter(Position=0,ValuefromPipeline=$true)][string][alias("cn")]$computer,
  32. [Parameter(Position=1,ValuefromPipeline=$false)][string]$computerlist
  33. )
  34. If (-not ($computer -or $computerlist))
  35. {
  36. $computers = $Env:COMPUTERNAME
  37. }
  38. If ($computer)
  39. {
  40. $computers = $computer
  41. }
  42. If ($computerlist)
  43. {
  44. $computers = Get-Content $computerlist
  45. }
  46. foreach ($computer in $computers)
  47. {
  48. $Computerobj = "" | select ComputerName, Uptime, LastReboot
  49. $wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
  50. $now = Get-Date
  51. $boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
  52. $uptime = $now - $boottime
  53. $d =$uptime.days
  54. $h =$uptime.hours
  55. $m =$uptime.Minutes
  56. $s = $uptime.Seconds
  57. $Computerobj.ComputerName = $computer
  58. $Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
  59. $Computerobj.LastReboot = $boottime
  60. $Computerobj
  61. }
复制代码


执行效果:
C:\>powershell -f Get-Uptime.ps1

ComputerName                      Uptime                           LastReboot
------------                      ------                           ----------
HAT                         0 Days 4 Hours 23 Min 43 Sec     2015/4/24 8:22:51


转自 http://blogs.technet.com/b/bshukla/archive/2010/12/09/powershell-script-to-report-uptime.aspx

本帖最后由 Nsqs 于 2017-3-27 20:52 编辑

太冷清.贴个源函数实现方法
  1. Function GettickCount(){
  2. $t=Add-Type -MemberDefinition @'
  3. [DllImport("kernel32")]
  4. public static extern int GetTickCount();  
  5. '@ -passthru -name SystemTickTime
  6.     Return $t::GetTickCount()
  7. }
  8. [string]$s=[regex]'(\d+)\..+|\d+'
  9. [int]$t=(GetTickCount)
  10. [string[]]$r=($t/1000/86400),($t/1000/3600),($t/1000/60%60),($t/1000%60) -replace $s,'$1'
  11. $r[0]+'天'+$r[1]+'时'+$r[2]+'分'+$r[3]+'秒'
复制代码
1

评分人数

TOP

本帖最后由 xxpinqz 于 2016-8-26 23:27 编辑

两个都不能运行,提示:
  1. 无法加载文件 .。。。。。,因为在此系统上禁止运行脚本。
  2. + CategoryInfo          : SecurityError: (:) [],ParentContainsErrorRecordException
  3. + FullyQualifiedErrorId : UnauthorizedAccess
复制代码
原来要先Set-ExecutionPolicy RemoteSigned
已解决这错误提示。

这个可行:
  1. [Timespan]::FromMilliseconds([Environment]::TickCount)
复制代码
初学BAT,非专业。代码不适当之处还望前辈们多多指点。在此表示感谢!

TOP

返回列表