LoginSignup
0
0

More than 5 years have passed since last update.

PowerShell for Linuxの動作検証 to GMOクラウドALTUS

Last updated at Posted at 2016-09-29

はじめに

Linux等のサーバをリモートで操作する際に重要なのは、シェル環境ですよね?
私もbashを愛用しておりますが、たまにCシェル系のサーバに入ると、戸惑うことも多々あります。
Windowsサーバ環境では、PowerShellを利用される方も見かけるようになってきました。
PowerShellを利用されてる方は、Linux上で利用することで、慣れたシェル環境を手に入れることができるかと思います。

そこで、Linux上で動くPowerShellが発表されたので、さっそくインストールと動作確認を
GMOクラウドALTUSで行いたいと思います。

準備

まず、インストールする環境を用意しました。
シェルですので、小さい環境でも十分動いてくれることを期待して、Mini Serverを用意しました。
OSは、CentOS7とします。

ハイパーバイザ XenServer
テンプレート CentOS 7.1.1503 64bit_20160223
仮想サーバー Mini Server

PowerShell for Linuxは、gitでrpmとして提供されていますので、事前にファイルをサーバにダウンロードしておきます。

PowerShell - git
https://github.com/PowerShell/PowerShell

PowerShell - CentOS7 rpm
https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/powershell-6.0.0_alpha.9-1.el7.centos.x86_64.rpm
[2016/9/13現在]

インストール

rpmをインストールするだけなので、手間が省けて助かります。

URLから直接インストールする場合は、このように

# yum install https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/powershell-6.0.0_alpha.9-1.el7.centos.x86_64.rpm

Dependencies Resolved

===================================================================================================================================================================================================================
 Package                                   Arch                                      Version                                        Repository                                                                    Size
 ===================================================================================================================================================================================================================
 Installing:
  powershell                                x86_64                                6.0.0_alpha.9-1                                /powershell-6.0.0_alpha.9-1.el7.centos.x86_64                                121 M
 Installing for dependencies:
  libicu                                    x86_64                                50.1.2-15.el7                                  base                                                                         6.9 M
  libunwind                                 x86_64                                2:1.1-5.el7_2.2                                updates                                                                       56 k

 Transaction Summary
 ===================================================================================================================================================================================================================
 Install  1 Package (+2 Dependent packages)

Mini Serverで直接URLからインストールして、3分前後でした。

シェルの起動

インストール後、powershellのコマンドが用意され、コマンドを実行するとすぐにシェルが起動します。

# powershell
PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS /root>

動作確認

PowerShell独自のコマンドを入力して動作確認してみました。
Linux環境下のため、コマンドの大文字、小文字の区別なく動作するようです。

PS /root> Get-Process | Select-Object -first 5

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
      0       0        0          2      0.010   4922 642 abrt-dbus
      0       0        0          1      0.000    658 658 abrt-watch-log
      0       0        0          1      0.010    632 632 abrtd
      0       0        0          0      0.000    669 669 agetty
      0       0        0          0      0.000    670 670 agetty


PS /root> get-process | select-object -first 5

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
      0       0        0          2      0.010   4922 642 abrt-dbus
      0       0        0          1      0.000    658 658 abrt-watch-log
      0       0        0          1      0.010    632 632 abrtd
      0       0        0          0      0.000    669 669 agetty
      0       0        0          0      0.000    670 670 agetty

スクリプトの動作確認

スクリプトが動作するか検証してみます。

Microsoft社のスクリプトセンターには、さまざまなスクリプトが公開されていますので、
その中のカレンダー表示するスクリプトが動くか検証してみます。

カレンダースクリプト - マイクロソフトスクリプトセンター
https://gallery.technet.microsoft.com/scriptcenter/753c225a-aba3-4092-be76-903348dacd97

スクリプトの内容は、割愛しますが、
直接関数を呼び出せるようにするためglobal修飾子を記載します。

変更前
function Show-Calendar
{                       

 変更後          
function global:Show-Calendar
{                       

ファイルを保存後、ファイル設置ディレクトリにて実行すると、下記のように無事動作しました。

PS /root> ./Show-Calendar.ps1
PS /root> ./Show-Calendar
            2016/9
 Sun Mon Tue Wed Thu Fri Sat
                   1   2   3
   4   5   6   7   8   9  10
  11  12  13  14  15  16  17
  18  19  20  21  22  23  24
  25  26  27  28  29  30

PS /root> ./Show-Calendar 2020 3
            2020/3
 Sun Mon Tue Wed Thu Fri Sat
   1   2   3   4   5   6   7
   8   9  10  11  12  13  14
  15  16  17  18  19  20  21
  22  23  24  25  26  27  28
  29  30  31

利用できないコマンド

一部のコマンドは、利用できないようです。
特にSet-ExecutionPolicyのように権限を設定するようなものは、サポートされないみたいです。

PS /root> Set-ExecutionPolicy Unrestricted
Set-ExecutionPolicy : Operation is not supported on this platform.
At line:1 char:1
+ Set-ExecutionPolicy Unrestricted
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Set-ExecutionPolicy], PlatformNotSupportedException
+ FullyQualifiedErrorId :     System.PlatformNotSupportedException,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand

まとめ

私自身は、PowerShellをほとんど利用したことがないですが、だいぶリッチなシェル環境です。
α版であり、Linux版であるので使い勝手は、Windows版のようにいかないようですが、
Windowsサーバを管理してる方は、Linuxサーバの運用にもノウハウが利用できそうです。
また、Windowsサーバの管理する予習としても、利用できそうですね。

0
0
0

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
0
0