たいちの何か

趣味を中心に車・クラウド・サーバーなどの話を勝手気ままに書いています

Azure - Powershellを使ってAzureに接続してみる - 接続編

今回はPowershell経由でAzureに接続してみたいと思います。

[2017.08.15追加]
サービスプリンシパルを使った自動ログオンの方法を書きました。
edge.hateblo.jp

目次

前回の記事はこちら。
edge.hateblo.jp

How to connect to Azure using Powershell

Powershellを使ってAzureに接続するためには、Webブラウザ経由で接続と同様にログインが必要です。 (ログインなしで接続で来たらセキュリティ的にまずいですからね)

PowershellでAzureにログインするためには以下のコマンドを実行します。

add-azureaccount

すると下記の画面が出てくるのでIDとパスワードを入れればログイン完了です。
f:id:taitioooo:20150812212822j:plain

簡単ですね。
でも!毎回ログインするのって面倒じゃないですか?
Powershellを起動したら、Azureにログインが完了していてほしいと思いませんか?
スクリプトを使ってAzureのコマンドをたたきたいときはどうすればいいのと思いませんか?
(少なくとも私はそう思います笑)

この辺りは天下のマイクロソフト様なのでしょうか。ちゃんと自動ログインする方法が用意されています。
早速自動ログインの設定をしてみます。

import PublishSettingsFile to logon Azure automatically

1. login Azure portal

まずWebブラウザを起動し、 Azureポータルにログインしておきます。
f:id:taitioooo:20150812213818j:plain

2. Invork Get-AzurePublishSettingsFile

次にPowershell*1を起動し以下のコマンドを実行します

Get-AzurePublishSettingsFile

実行するとWebブラウザに以下のページが表示され従量課金-x-xx-xxxx-credentials.publishsettingsというファイルのダウンロードが始まります。
このファイルをダウンロードします。(設定変更後削除するので、わかるところに保存しておいてください)
f:id:taitioooo:20150812214354j:plain

3. import Publishsettings file

以下のコマンドを実行して、先ほどダウンロードしたpublishsettingsfileをインポートします。

import-AzurePublishSettingsFile -PubliseSetiingsFile <ファイルのフルパス> 


以上で自動ログインの設定終わりです。
これでAzureサブスクリプションに対する自動ログインが可能になりました。

さてpublishsettingsfileをインポートしたらAzureに接続できるようになりましたが、このファイルは何なのでしょうか。
ググってみたところこんな記事を見つけました。

About Windows Azure Publish Settings File and how to create your own Publish Settings File

A publish settings file is an XML file which contains information about your subscription. It contains information about all subscriptions associated with a user’s Live Id (i.e. all subscriptions for which a user is either an administrator or a co-administrator). It also contains a management certificate which can be used to authenticate Windows Azure Service Management API requests. Typically a publish settings file look something like this:

確かにmmcを起動して証明書の[個人]-[証明書]を見てみると[Windows Azure Tools]という証明書が入っているではないですか。 有効期限は1年のようですね。つまり1年に1回は更新する必要があるということですね。 f:id:taitioooo:20150812221019j:plain

ちなみに、PublishsettingsFileをインポートすると、下記に設定ファイル(.json)が作られるようです。

%UserProfile%\AppData\Roaming\Windows Azure Powershell
f:id:taitioooo:20150812221644j:plain

jsonファイルの中身はこんな感じです。 (個人情報は削っています)
自動ログインに必要がなくなったら、下記のファイルと証明書を削除すればよさそうですね。

{  
  "Environments": [],  
edge.hateblo.jp


  "Subscriptions": [  
    {  
      "Id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",  
      "Name": "従量課金",  
      "Environment": "AzureCloud",  
      "Account": "xxxxxxxxxxxxxxxxxxxxxxxx",  
      "Properties": {  
        "SupportedModes": "AzureServiceManagement",  
        "Default": "True"  
      }  
    }  
  ],  
  "Accounts": [  
    {  
      "Id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",  
      "Type": 0,  
      "Properties": {  
        "Subscriptions": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  
      }  
    }  
  ]  
}  

関連ページ

Azure - Powershellを使ってAzureに接続してみる - 接続編 - たいちの何か Azure - Powershellのバージョン確認方法とアップデート - たいちの何か

*1:管理者権限・ユーザー権限どちらでも構いません

Copyright © 2014 - taitioooo. All Rights Reserved.