Skip to content

Instantly share code, notes, and snippets.

@stknohg
Created January 17, 2014 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stknohg/8473279 to your computer and use it in GitHub Desktop.
Save stknohg/8473279 to your computer and use it in GitHub Desktop.
PowerShellでuptimeを取得するワンライナー。 TimeSpan型を返すものとUnix風の文字列を返すバージョンを作りました。
#
# PowerShellでuptimeを取得するワンライナー
#
# 1.TimeSpan型のオブジェクトを返すバージョン
# PowerShellらしくオブジェクトで返す。後続の処理と連携させたい場合等に。
#
&{$w=gwmi Win32_OperatingSystem;$w.ConvertToDateTime($w.LocalDateTime)-$w.ConvertToDateTime($w.LastBootUpTime);}
#
# PowerShellでuptimeを取得するワンライナー
#
# 2.Unix風の文字列を返すバージョン
# 見慣れた表示形式で。単純にuptimeを見たい場合はこっち。
# ログインユーザー数やロードアベレージは流石に出ない。
#
&{$w=gwmi Win32_OperatingSystem;$b=$w.ConvertToDateTime($w.LastBootUpTime);$n=$w.ConvertToDateTime($w.LocalDateTime);"{0:T} up {1:%d} days, {1:hh\:mm}" -f $n,($n-$b)}
#
# PowerShellでuptimeを取得するワンライナー
#
# 常用するなら以下の様な感じファンクションに登録+Alias設定しても良いと思う。
# まあ、それならもっと綺麗に書いた方が...
function Get-Uptime {$w=gwmi Win32_OperatingSystem;$b=$w.ConvertToDateTime($w.LastBootUpTime);$n=$w.ConvertToDateTime($w.LocalDateTime);"{0:T} up {1:%d} days, {1:hh\:mm}" -f $n,($n-$b)}
Set-Alias uptime Get-Uptime
@guitarrapc
Copy link

変数使わずパイプラインで渡すとかもありますね

gwmi Win32_OperatingSystem | %{$_.ConvertToDateTime($_.LocalDateTime)-$_.ConvertToDateTime($_.LastBootUpTime)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment