LoginSignup
18
13

More than 5 years have passed since last update.

Linux版PowerShellのインストールとHello World! +α

Last updated at Posted at 2016-08-19

※ 2018/01/16 追記: PowerShell Core 6.0 正式版がリリースされました。
http://www.publickey1.jp/blog/18/windowslinuxmacospowershell_core_60.html

【はじめに】Linux/Mac OS X版も公開されたPowerShell

PowerShellがOSS化し、LinuxやMac OS Xに対応するバージョンが公開されました。
Linux環境にインストールして、簡単な動作確認を試します。

1. Linux版PowerShellのインストール

今回はUbuntu 16.04(64bit)にインストールします。
PowerShellのGitHubの PowerShell/docs/installation/linux.md を参考に進めます。

インストール
# 他パッケージの準備(結果的にはインストール済みでした)
ubuntu@ubuntu:~$ sudo apt-get install libunwind8 libicu55                                                    
パッケージリストを読み込んでいます... 完了                                                                 
依存関係ツリーを作成しています                                                                             
状態情報を読み取っています... 完了                                                                         
libicu55 はすでに最新バージョン (55.1-7) です。                                                            
libicu55 は手動でインストールしたと設定されました。                                                        
libunwind8 はすでに最新バージョン (1.1-4.1) です。                                                         
libunwind8 は手動でインストールしたと設定されました。   

# PowerShellのインストールパッケージのダウンロード                                                                          
ubuntu@ubuntu:~$ wget https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/powershell_6.
0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb
()                                                                      

# インストール
ubuntu@ubuntu:~$ sudo dpkg -i powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb                            
()

2. 環境確認

2.1. 起動確認

とりあえず powershell と打ってみます。インタラクティブなPowerShell環境が起動しました。
インストールが成功したことが確認できました。

起動確認
ubuntu@ubuntu:~$ powershell
PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.
PS /home/ubuntu>

2.2. バージョン確認

続いて、バージョンの確認をします。Windowsと同様に$PSVersionTableの中身を表示させます。
PSVersionで「6.0.0-alpha」と示されています。確認できたらexitでいったん抜けます。

バージョン確認

PS /home/ubuntu> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.9
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS /home/ubuntu> exit
ubuntu@ubuntu:~$

3. Hello World!の実行

今度は簡単なスクリプトファイルを用意して実行してみます。

まず、以下の内容のps1ファイルを用意します。

helloworld.ps1
Write-Host "Hello World!"

実行します。

PowerShellスクリプトの実行
ubuntu@ubuntu:~$ powershell ./helloworld.ps1 
Hello World!
ubuntu@ubuntu:~$ 

以上で Linux版 PowerShellでの Hello World が確認できました。

4. サンプル: ifconfig 結果からIPアドレスを抽出する

How to Create and Run PowerShell Scripts にある、
ifconfig 結果からIPアドレスを抽出するスクリプトを試します。

まず、以下の内容のスクリプトを用意します。

show_ip.ps1
$ipInfo = ifconfig | Select-String 'inet'
$ipInfo = [regex]::matches($ipInfo,"addr:\b(?:\d{1,3}\.){3}\d{1,3}\b") | ForEach-Object value
foreach ($ip in $ipInfo) {
    $ip.Replace('addr:','')
}

スクリプトを実行します。

ubuntu@ubuntu:~$ LANG=C                      # スクリプトの内容に合わせるため
ubuntu@ubuntu:~$ powershell ./show_ip.ps1    # 実行
192.168.0.110
127.0.0.1
ubuntu@ubuntu:~$

表示されました。

5. インタラクティブシェルのお試し

5.1. linuxコマンドの実行

「2.1. 起動確認」で示したのように、powerhsellだけ打つとインタラクティブシェルが起動します。linux自身のコマンドも実行できます。

ubuntu@ubuntu:~$ powershell
PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS /home/ubuntu> ifconfig    ←linuxコマンドの実行
enp0s3    Link encap:Ethernet  HWaddr 08:00:27:xx:xx:xx
          inet addr:192.168.0.110  Bcast:192.168.0.255  Mask:255.255.255.0
          (略)

5.2. コマンドレットの補完

コマンドレットの途中で、Tabキーを押すと補完ができます。

PS /home/ubuntu> Get-P     ←途中でTabを押して候補表示
Get-Package                              Get-SProvider
Get-PackageProvider                      Get-PSReadlineKeyHandler
Get-PackageSource                        Get-PSReadlineOption 
Get-PositionInfo                         Get-PSRepository         
Get-Process                              
(略)
PS /home/ubuntu> Get-Process  ←「Get-Pr」のあとTabで補完される

5.3. プロパティの補完

. のあとに Tabキーを押すとプロパティ、メソッドが表示されます。

PS /home/ubuntu> "12345".    ← . のあとにTabを押してプロパティ、メソッドを表示
Length            GetType           PadRight          ToChar            ToLower           ToUInt64
CompareTo         GetTypeCode       Remove            ToCharArray       ToLowerInvariant  ToUpper
Contains          IndexOf           Replace           ToDateTime        ToSByte           ToUpperInvariant
CopyTo            IndexOfAny        Split             ToDecimal         ToSingle          Trim
EndsWith          Insert            StartsWith        ToDouble          ToString          TrimEnd
Equals            LastIndexOf       Substring         ToInt16           ToType            TrimStart
GetEnumerator     LastIndexOfAny    ToBoolean         ToInt32           ToUInt16          Chars
GetHashCode       PadLeft           ToByte            ToInt64           ToUInt32
PS /home/ubuntu> "12345".Length
5

6. その他

6.1. 開発環境

Visual Studio Code もLinux版があり、PowerShell用の拡張もあります。
詳細は以下のページをご覧ください。
Using Visual Studio Code for PowerShell Development

18
13
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
13