Created
January 17, 2014 13:22
-
-
Save stknohg/8473279 to your computer and use it in GitHub Desktop.
PowerShellでuptimeを取得するワンライナー。
TimeSpan型を返すものとUnix風の文字列を返すバージョンを作りました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# PowerShellでuptimeを取得するワンライナー | |
# | |
# 1.TimeSpan型のオブジェクトを返すバージョン | |
# PowerShellらしくオブジェクトで返す。後続の処理と連携させたい場合等に。 | |
# | |
&{$w=gwmi Win32_OperatingSystem;$w.ConvertToDateTime($w.LocalDateTime)-$w.ConvertToDateTime($w.LastBootUpTime);} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
変数使わずパイプラインで渡すとかもありますね