Top Banner
PowerShell 3.0 の のの Windows 8 のののののの のののののの #1 2013/07/06 @oota_ken
21

Power shell 3.0の魅力

Jan 15, 2015

Download

Technology

めとb
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Power shell 3.0の魅力

PowerShell 3.0 の魅力~ Windows 8 の隠れた主役~めとべや東京 #1 2013/07/06@oota_ken

Page 2: Power shell 3.0の魅力

自己紹介 仕事 Jenkins とかで自動化する PowerShell の Blog を書く

好きなもの シェル SQL

嫌いなもの Excel 方眼紙

OTL

Page 3: Power shell 3.0の魅力

僕の最強のデスクトップ スタート画面なんて飾りです。偉い人にはそれがわからんのです

Page 4: Power shell 3.0の魅力

PowerShell の魅力

Page 5: Power shell 3.0の魅力

全てがオブジェクト 例: 20MB 以上のメモリを消費しているプロセス

C:\>Get-Process | ? PM -gt 20MB

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName------- ------ ----- ----- ----- ------ -- ----------- 1880 262 54804 149460 718 50.00 7884 explorer 651 34 27744 43108 187 2332 integratedoffice 1017 44 23064 23624 1133 3.35 6624 LiveComm 475 83 92792 81440 238 2696 MsMpEng 266 33 31448 3280 629 0.16 5164 pcee4 1414 124 231488 295532 781 142.26 4192 powerpnt 433 27 64484 76128 627 15.90 3320 powershell 163 26 32872 32572 516 1988 PresentationFontCache

Page 6: Power shell 3.0の魅力

.NET, COM, WMI が簡単に使えるExcel の関数も PowerShell から簡単に呼び出せる

C:\> $xl = New-Object -ComObject Excel.ApplicationC:\> $wf = $xl.WorksheetFunctionC:\> $wf.Average(@(1, 2, 3, 4, 5))3C:> $wf.Sum(@(1, 2, 3, 4, 5))15C:> $wf.Slope(@(2, 4, 8), @(1, 2, 4))2

Page 7: Power shell 3.0の魅力

PowerShell の魅力 その 3 高階関数 ( スクリプトブロック ) 使える (F# っぽく書ける )

ただし、 F# のような部分適用はできないC:\> Get-Content .\inject.ps1function inject { param ($init, [ScriptBlock] $block) begin { $_l = $init } process { $_l = & $block $_l $_ }end { $_l }}1, 2, 3 | inject 0 { param($sum, $i) $sum + $i }C:\> .\inject.ps16C:\> $add = { param($x) { param($y) $x + $y}.GetNewClosure() }C:\> $add3 = & $add 3C:\> & $add3 58

Page 8: Power shell 3.0の魅力

その他 ネットワークが遮断された環境でもプログラミングできる (* ▽ *)⌒ ⌒ Windows 7 以降で標準組み込み Excel 方眼紙環境でも Excel の COM が全部使える

コマンドレットの動詞と名詞が標準化されている 最小限のキーワードさえ覚えていれば、補完とインテリセンスでなんとかなる UNIX のコマンドやコマンドプロンプトのように複雑怪奇ではない

よくやく Cygwin いらなくなった PowerShell のコマンドレット+ .NET 、 COM オブジェクトでほぼ全ての作業が足りる

Page 9: Power shell 3.0の魅力

PowerShell3.0 の新機能

Page 10: Power shell 3.0の魅力

連想配列からのオブジェクト作成がシンプルに

C:\> $powerShell2Item = New-Object PSObject -Property @{ Id = 1; Name = "ThinkPad X1"; price = 150000 }C:\> $powerShell2Item

price Name Id ----- ---- -- 150000 ThinkPad X1 1

C:\> $powerShell3Item = [pscustomobject] @{ Id = 2; Name = "ThinkPad X1 Carbon"; price = 170000 }C:\> $powerShell3Item

Id Name price -- ---- ----- 2 ThinkPad X1 Carbon 170000

Page 11: Power shell 3.0の魅力

Where-Object や ForEach-Object の記法がシンプルにPowerShell 2.0

PowerShell 3.0

C:\> get-process | ? { $_.PM -gt 20MB }

C:\> get-process | ? PM -gt 20MB

Page 12: Power shell 3.0の魅力

Web へのアクセスがコマンドレットで標準にPowerShell 2.0

PowerShell 3.0

C:\> (New-Object System.Net.WebClient).DownloadString("http://www.microsoft.com")

C:\> Invoke-WebRequest "http://www.microsoft.com"

Page 13: Power shell 3.0の魅力

PowerShell ISE で Intellisense が有効に

Page 14: Power shell 3.0の魅力

UNIX の .*sh な方へ

Page 15: Power shell 3.0の魅力

Exit すると history が消えちゃうんだけど $profile に記述して、 Invoke-History で呼び出せます とはいえ、 Ctrl-P では呼び出せないです><

C:\> Get-Content $PROFILE | Select-String "Set-Alias"

Set-Alias Word "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"Set-Alias Excel "C:\Program Files\Microsoft Office 15\root\office15\excel.exe"Set-Alias PowerPoint "C:\Program Files\Microsoft Office 15\root\office15\powerpnt.exe"

C:\> Excel

Page 16: Power shell 3.0の魅力

.*sh のキーバインドにしたいんだけど 残念ながら PowerShell 自体にはキーバインドの設定はありません が、 AutoHotKey を使って設定出来ます

#IfWinActive, ahk_class ConsoleWindowClass^a::SendInput {HOME}^e::SendInput {END}^u::SendInput {ESC}^p::SendInput {Up}^n::SendInput {Down}^l::SendInput {ESC}cls{ENTER}^k::SendInput {F4}^f::SendInput {right}^b::SendInput {left}#IfWinActive

Page 17: Power shell 3.0の魅力

ウィンドウを最大化したいんだけど Terminal2 や ckw を使いましょう! オマケに透過設定もできちゃいますよ

Page 18: Power shell 3.0の魅力

yum や apt-get みたいなのないの? Chocolatey があります。 Git から Chrome まで何でも入ります

インストール

パッケージインストール

C:\> iex ((new-object net.webclient).DownloadString("http://bit.ly/psChocInstall"))

C:\> “git”, “pester”| % { cinst $_ }

Page 19: Power shell 3.0の魅力

スタートメニューがないと嘆く方へ

Page 20: Power shell 3.0の魅力

全て PowerShell から起動するのです Set-Alias で PowerShell から全て起動すれば何の問題もありません

C:\> Get-Content $profile | Select-String "Set-Alias"

Set-Alias Word "C:\Program Files\Microsoft Office 15\root\office15\winword.exe"Set-Alias Excel "C:\Program Files\Microsoft Office 15\root\office15\excel.exe"Set-Alias PowerPoint "C:\Program Files\Microsoft Office 15\root\office15\powerpnt.exe"

C:\> Excel

Page 21: Power shell 3.0の魅力

まとめ スタート画面なんて飾りです。偉い人にはそれがわからんのです