PowerShell脚本获取系统运行时间x天x小时x分钟
test.ps1[code]$os = Get-WmiObject win32_operatingsystem
$uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
$Display = "Uptime: " + $Uptime.Days + " days, " + $Uptime.Hours + " hours, " + $Uptime.Minutes + " minutes"
Write-Output $Display[/code]
执行效果:
[table=100%,black][tr][td][color=white]C:\>powershell -f test.ps1
Uptime: 0 days, 4 hours, 16 minutes
[/color][/td][/tr][/table]
Get-Uptime.ps1
[code]#############################################################################
# Get-Uptime.ps1
# This script will report uptime of given computer since last reboot.
#
# Pre-Requisites: Requires PowerShell 2.0 and WMI access to target computers (admin access).
#
# Usage syntax:
# For local computer where script is being run: .\Get-Uptime.ps1.
# For list of remote computers: .\Get-Uptime.ps1 -ComputerList "c:\temp\computerlist.txt"
#
# Usage Examples:
#
# .\Get-Uptime.ps1 -Computer ComputerName
# .\Get-Uptime.ps1 -ComputerList "c:\temp\computerlist.txt" | Export-Csv uptime-report.csv -NoTypeInformation
#
# Last Modified: 3/20/2012
#
# Created by
# Bhargav Shukla
# http://blogs.technet.com/bshukla
# http://www.bhargavs.com
#
# DISCLAIMER
# ==========
# THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
# RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#############################################################################
#Requires -Version 2.0
param
(
[Parameter(Position=0,ValuefromPipeline=$true)][string][alias("cn")]$computer,
[Parameter(Position=1,ValuefromPipeline=$false)][string]$computerlist
)
If (-not ($computer -or $computerlist))
{
$computers = $Env:COMPUTERNAME
}
If ($computer)
{
$computers = $computer
}
If ($computerlist)
{
$computers = Get-Content $computerlist
}
foreach ($computer in $computers)
{
$Computerobj = "" | select ComputerName, Uptime, LastReboot
$wmi = Get-WmiObject -ComputerName $computer -Query "SELECT LastBootUpTime FROM Win32_OperatingSystem"
$now = Get-Date
$boottime = $wmi.ConvertToDateTime($wmi.LastBootUpTime)
$uptime = $now - $boottime
$d =$uptime.days
$h =$uptime.hours
$m =$uptime.Minutes
$s = $uptime.Seconds
$Computerobj.ComputerName = $computer
$Computerobj.Uptime = "$d Days $h Hours $m Min $s Sec"
$Computerobj.LastReboot = $boottime
$Computerobj
}[/code]
执行效果:
[table=100%,black][tr][td][color=white]C:\>powershell -f Get-Uptime.ps1
ComputerName Uptime LastReboot
------------ ------ ----------
HAT 0 Days 4 Hours 23 Min 43 Sec 2015/4/24 8:22:51
[/color][/td][/tr][/table]
转自 http://blogs.technet.com/b/bshukla/archive/2010/12/09/powershell-script-to-report-uptime.aspx [i=s] 本帖最后由 xxpinqz 于 2016-8-26 23:27 编辑 [/i]
两个都不能运行,提示:[code]无法加载文件 .。。。。。,因为在此系统上禁止运行脚本。
+ CategoryInfo : SecurityError: (:) [],ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess[/code]原来要先Set-ExecutionPolicy RemoteSigned
已解决这错误提示。
这个可行:[code][Timespan]::FromMilliseconds([Environment]::TickCount)[/code] [i=s] 本帖最后由 Nsqs 于 2017-3-27 20:52 编辑 [/i]
太冷清.贴个源函数实现方法[code]Function GettickCount(){
$t=Add-Type -MemberDefinition @'
[DllImport("kernel32")]
public static extern int GetTickCount();
'@ -passthru -name SystemTickTime
Return $t::GetTickCount()
}
[string]$s=[regex]'(\d+)\..+|\d+'
[int]$t=(GetTickCount)
[string[]]$r=($t/1000/86400),($t/1000/3600),($t/1000/60%60),($t/1000%60) -replace $s,'$1'
$r[0]+'天'+$r[1]+'时'+$r[2]+'分'+$r[3]+'秒'[/code]
页:
[1]