PowerShellでArcGIS Server 10.5のインストールを自動化してみた

インストーラーの解凍だけはGUI必須だけど他は自動化できた！

自動化した処理

- ArcGIS Server 10.5のインストール
- ArcGIS Server 10.5の認証
- プライマリサイトの作成

## インストーラーを解凍する

ArcGIS_Server_Windows_105_ja_155416.exeを実行する.
解凍先を適宜設定する.
「今すぐプログラムを実行」のチェックを外すこと.

## インストーラーを実行する

``` powershell
$username = "arcgis"
$password = "pass"
$arcGISServerSetupExe = "E:\ArcGISInstaller\Server\setup.exe"
$arcGISServerSetupArg = "/qn /quiet USER_NAME={0} PASSWORD={1}" -f $username, $password

Start-Process -FilePath $arcGISServerSetupExe -ArgumentList $arcGISServerSetupArg -Verb runas -Wait
```

## ライセンス認証を行う

``` powershell
$prvcPath = "E:\PrvcFiles\ArcGISServerStandard10.5.prvc"
$softwareAuthorizationExe = "C:\Program Files\Common Files\ArcGIS\bin\softwareauthorization.exe"
$softwareAuthorizationArg = "-S -LIF {0}" -f $prvcPath

Start-Process -FilePath $softwareAuthorizationExe -ArgumentList $softwareAuthorizationArg -Verb runas -Wait
```

``` txt:ArcGISServerStandard10.5.prvc
// User Information

First Name=Hoge

Last Name=Fuga

Organization=Some Company

Department=System Development

Email=someone@somecompany.co.jp

Address 1=1-1-1 Shinagawa

City=Shinagawa-ku

State/Province=Tokyo

Country=Japan

Zip/Postal Code=111-1111

Phone Number=+81(0)3-1234-5678

// Features and authorization numbers

ArcGIS Server=ECP000000000

```
## プライマリサイトを作成する

``` powershell
$pythonExe = "C:\Python27\ArcGISx6410.5\python.exe"
$createPrimarySiteArg = "-B E:\ArcPy\create_new_site.py"

Start-Process -FilePath $pythonExe -ArgumentList $createPrimarySiteArg -Verb runas -PassThru
```

``` py:create_new_site.py
# coding:utf-8
import httplib
import urllib
import json
import sys
import os
import getpass

def create_new_site(): 
    # プライマリサイトの管理者ユーザー名
    username = "someone"
    # プライマリサイトの管理者パスワード
    password = "pass"
    # ルートサーバー名
    server_name  = "arcgis_server"
    # ルートサーバーのポート番号
    server_port = 6080
    # ルートサーバーディレクトリ
    root_dir_path = r"C:\arcgisserver\directories"
    # 構成ストアディレクトリ
    config_store_dir_path = r"C:\arcgisserver\config-store"
    
    # 構成ストアの設定
    config_store_connection={"connectionString": config_store_dir_path, "type": "FILESYSTEM"}
 
    # サーバーディレクトリの設定
    cache_dir_path = os.path.join(root_dir_path, "arcgiscache")
    jobs_dir_path = os.path.join(root_dir_path, "arcgisjobs")
    output_dir_path = os.path.join(root_dir_path, "arcgisoutput")
    system_dir_path = os.path.join(root_dir_path, "arcgissystem")
   
    # サーバーディレクトリのリストをJSON形式で作成
    cache_dir = dict(name = "arcgiscache",physicalPath = cache_dir_path,directoryType = "CACHE",cleanupMode = "NONE",maxFileAge = 0,description = "Stores tile caches used by map, globe, and image services for rapid performance.", virtualPath = "")    
    jobs_dir = dict(name = "arcgisjobs",physicalPath = jobs_dir_path, directoryType = "JOBS",cleanupMode = "TIME_ELAPSED_SINCE_LAST_MODIFIED",maxFileAge = 360,description = "Stores results and other information from geoprocessing services.", virtualPath = "")
    output_dir = dict(name = "arcgisoutput",physicalPath = output_dir_path,directoryType = "OUTPUT",cleanupMode = "TIME_ELAPSED_SINCE_LAST_MODIFIED",maxFileAge = 10,description = "Stores various information generated by services, such as map images.", virtualPath = "")
    system_dir = dict(name = "arcgissystem",physicalPath = system_dir_path,directoryType = "SYSTEM",cleanupMode = "NONE",maxFileAge = 0,description = "Stores files that are used internally by the GIS server.", virtualPath = "")
    directories_JSON = json.dumps(dict(directories = [cache_dir, jobs_dir, output_dir, system_dir]))      
  
    # サイト作成APIのURL
    create_new_sute_url = "/arcgis/admin/createNewSite"
    
    # リクエスト作成/送信
    params = urllib.urlencode({'username': username, 'password': password, 'configStoreConnection': config_store_connection, 'directories':directories_JSON, 'f': 'json'})    
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    http_conn = httplib.HTTPConnection(server_name, server_port)
    http_conn.request("POST", create_new_sute_url, params, headers)
    
    # レスポンス取得
    response = http_conn.getresponse()
    if (response.status != 200):
        http_conn.close()
        print u"プライマリサイト作成中にエラーが発生しました."
        return
    else:
        data = response.read()
        http_conn.close()
        
        # リクエスト結果確認
        if not assert_json_success(data):          
            print u"プライマリサイト作成中にエラーが発生しました." + str(data)
        else:
            print u"プライマリサイトの作成が完了しました."
     
        return
    

def assert_json_success(data):
    obj = json.loads(data)
    if 'status' in obj and obj['status'] == "error":
        return False
    else:
        return True
        
create_new_site()
```

### 参考

- https://enterprise.arcgis.com/ja/server/latest/install/windows/silently-install-arcgis-server.htm
- http://resources.arcgis.com/en/help/server-admin-api/createsite.html
- http://enterprise.arcgis.com/ja/server/10.5/administer/windows/example-create-a-site.htm