LoginSignup
0
0

More than 5 years have passed since last update.

AWS Cloud9 から Azure PowerShell に接続する

Last updated at Posted at 2019-03-01

AWS Cloud9からAzure PowerShellに接続する手順を確立させたのでそのメモです。

具体的には、Cloud9上でUbuntuのDockerコンテナを起動して、その上にPowerShell CoreとAzモジュールをインストールし、AzureにPowerShellで接続可能にしました。

手順

すること

Cloud9のターミナルにてコマンドを以下の順に実行していきます。

Cloud9上のbash

  • docker run -it -d ubuntu:18.04
  • docker exec -it $(docker ps -l -q) /bin/bash

Dockerコンテナ上のUbuntu

  • apt update
  • apt install -y wget
  • wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
  • dpkg -i packages-microsoft-prod.deb
  • apt update
  • apt install -y powershell
  • mkdir $HOME/.config/powershell
  • pwsh

Ubuntu上のPowerShell Core

  • Install-Module Az
  • Import-Module Az
  • Enable-AzureRmAlias -Scope CurrentUser
  • Connect-AzureRmAccount

実際にやってみる

#DockerのUbuntu18.04イメージからコンテナを起動
ec2-user:~/environment $ docker run -it -d ubuntu:18.04
Unable to find image 'ubuntu:18.04' locally
18.10: Pulling from library/ubuntu
a13xxxxxxxxx: Pull complete 
e03xxxxxxxxx: Pull complete 
c57xxxxxxxxx: Pull complete 
d21xxxxxxxxx: Pull complete 
Digest: sha256:f4dc4a22fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Status: Downloaded newer image for ubuntu:18.04
af658cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

#先程起動したコンテナにログイン
ec2-user:~/environment $ docker exec -it $(docker ps -l -q) /bin/bash

#ここでupdateをしないと次のwgetインストールがエラーになる
root@af658cxxxxxx:/# apt update
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
(中略)
Building dependency tree       
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.

#wgetのインストール
root@af658cxxxxxx:/# apt install -y wget
Reading package lists... Done
Building dependency tree       
Reading state information... Done
(中略)
Running hooks in /etc/ca-certificates/update.d...
done.

#.NET Coreのリポジトリをaptコマンドの管理対象に登録
root@af658cxxxxxx:/# wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
root@af658cxxxxxx:/# dpkg -i packages-microsoft-prod.deb
Selecting previously unselected package packages-microsoft-prod.
(Reading database ... 4379 files and directories currently installed.)
Preparing to unpack packages-microsoft-prod.deb ...
Unpacking packages-microsoft-prod (1.0-ubuntu18.04.2) ...
Setting up packages-microsoft-prod (1.0-ubuntu18.04.2) ...

#ここでupdateをしないと次のPowerShellインストールがエラーになる
root@af658cxxxxxx:/# apt update
Get:1 https://packages.microsoft.com/ubuntu/18.04/prod bionic InRelease [3226 B]
(中略)
Building dependency tree       
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.

#PowerShellのインストール
root@af658cxxxxxx:/# apt install -y powershell
Reading package lists... Done
Building dependency tree       
Reading state information... Done
(中略)
Setting up powershell (6.1.3-1.ubuntu.18.04) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...

#PowerShellプロファイルの格納パス作成
root@af658cxxxxxx:/# mkdir $HOME/.config/powershell

#PowerShell起動
root@af658cxxxxxx:/# pwsh
PowerShell 6.1.3
Copyright (c) Microsoft Corporation. All rights reserved.
(略)

#Azモジュールをインストール
PS /> Install-Module Az
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): Y

#AzモジュールをPowerShellにインポート
PS /> Import-Module Az

#AzureRMのコマンド名でAzコマンドを実行できるようにする
PS /> Enable-AzureRmAlias -Scope CurrentUser

#Azureアカウントで認証してAzureRmのPowerShellセッション開始
PS /> Connect-AzureRmAccount                                                                                                                                   
WARNING: To sign in, use a web browser to open the page
https://microsoft.com/devicelogin and enter the code H4WXXXXXX to authenticate.

ブラウザでhttps://microsoft.com/deviceloginを開いてターミナル上に表示されている認証コードH4WXXXXXXを入力します。
スクリーンショット 2019-03-03 13.51.18.png
ログインを求められたらAzureアカウントでログインします。
このログイン方法であれば多要素認証が有効なアカウントであっても認証が可能となります。
ブラウザでのログインが完了したらターミナルに戻り、以下のようにログイン情報が表示されて認証が成功していることを確認します。

#認証が成功していることを確認。
PS /> Connect-AzureRmAccount
WARNING: To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code H4WXXXXXX to authenticate.                        

Account         SubscriptionName TenantId                             Environment                                                                                 
-------         ---------------- --------                             -----------
XXX@contoso.com YYYY             xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx AzureCloud

AzコマンドとAzureRMコマンド(実際はAzコマンド)が利用可能になりました。

PS /> Get-AzResourceGroup
ResourceGroupName : hogehoge
Location          : japaneast
ProvisioningState : Succeeded
Tags              : 
ResourceId        : /subscriptions/XXXXXXX・・・

PS /> Get-AzureRMResourceGroup
ResourceGroupName : hogehoge
Location          : japaneast
ProvisioningState : Succeeded
Tags              : 
ResourceId        : /subscriptions/XXXXXXX・・・

備考

  • ただブラウザからAzure PowerShellを利用したいということであれば、Azure Cloud Shellを利用された方がいいと思います。(わたしの場合はCloud9上にAzureほか各クラウドの設定Configを集約してGithubにpushする運用をしたいがため、このようなことをしています)

  • UbuntuでのPowerShell Coreの利用は、18.04が安定版のようです。18.10で実際に試してみましたがエラーとなり利用できませんでした。

  • Cloud9のターミナルでPowerShellを利用しているとターミナルの動作がすごく不安定になるときがあります。(スクロールが効かなくなる、コマンドが打てなくなる、等)。もしそうなった場合は新規ターミナルを開き直してください。

  • Linux上でOffice365 や Exchange PowerShell も使いたいのですが、 .Net Core 対応はまだ先のようです。(追記:なんとこちらで無理やり使ってらっしゃる方がいました。そのうちわたしも検証してみます。)

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