2015年10月30日金曜日

◆OSバージョンを調べる

Finding Operating System Version - Power Tips - PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources

[Environment]クラスのstaticプロパティで取ってこれるよってだけの話。

 

メンバーの値を一覧表示するにはどうするんだろう・・・。

とりあえずベタにやるとこんな感じ?

>[environment] | gm -static -type property | ?{$_.name -ne "stacktrace"} |
    %{$_.name + " = " + [Environment]::($_.name)}

stacktraceは結果が長くなるのでとりあえず除外している。

image

2015年9月17日木曜日

◆svchostでホストされているサービスを表示する

Analyzing svchost Processes - Power Tips - PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources

 

サービスとプロセスをぶつけてsvchostでホストされているサービスを一覧表示する。(Ver3)

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018

$service = @{
  Name 
= 'Service'
  Expression =
 
    {
     
if ($_.Name -eq 'svchost'
)
      {
      
$processID = $_.
ID
       (
$serviceList.$processID).Name -join ', '
      }
    }
}


$serviceList = gwmi -Class Win32_Service |
  group -Property ProcessID -AsString -AsHashTable

ps |
  Select -Property Name, ID, CPU, $service |
  Out-GridView

image

2015年8月18日火曜日

◆2つの配列の差(和、積)を取る

$a1 = 1..10
$a2 = 2,4,80,98

"<差>"
compare  $a1  $a2  |  %  inputobject  |  sort

"<和>"
$a1 + $a2  |  sort -Unique

"<和>"
compare  $a1  $a2 –IncludeEqual  |  %  inputobject  | sort

"<積>"
compare  $a1  $a2 -IncludeEqual –ExcludeDifferent  | %  inputobject  | sort

image

2015年7月29日水曜日

◆Zipファイルを解凍する

Windows10が今日からダウンロード可能になるそうな。

私的にはもう少し様子を見てからと思っていますが、無償アップグレードということなので、かなりのPCがWindows10に変わっていくことが予想されます。

Windows8の評判がすこぶる悪かったため、PowerShellも2.0の使用を基本に考えてきましたが、これからはWindows10に搭載される5.0を前提にしようかと考えています。
(クライアントで動かすようなスクリプトは)

 

5.0ではZipの解凍も標準コマンド(Function)でできるようです。

Expand-Archive  -Path  Zipファイルのパス  -Dest  解凍先フォルダーのパス

当然、圧縮も「Compress-Archive」が用意されています。

これまで圧縮はちょっと面倒だったので、これだけでもすごく嬉しい・・・。

2015年7月24日金曜日

◆対話ログオンしているユーザーを表示する(RDP含む)

Finding Logged On Users - Power Tips - PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources

前回は物理的に対面でログオンしているユーザーの表示だったが、今度はリモートデスクトップを含んだログオンユーザーの表示。

gwmi -Class Win32_LogonSession  | % {
  $_.GetRelated('Win32_UserAccount') |
  select -expandp caption |
  select -unique
}

ただ、試してみた感じだとログオフせずに切断した情報とかが残って表示されている様な気もする・・・。

以前UPしたPowerShell: ◆ログオンしているユーザーを表示するのほうが純粋に今の情報に近い様な気がする。(詳しくは調べてみないと判らない・・・)

2015年7月23日木曜日

◆対話ログオンしているユーザーを表示する

Find Physically Logged On User - Power Tips - PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources

へぇー、確かにリモートログオンの場合は表示されない・・・。

>Get-WmiObject -Class Win32_ComputerSystem | select username