PowerCLIでvCenterサーバに接続(Connect-VIServer)する際のエラー処理

vSphere環境でvCenterサーバにConnect-VIServerで接続する際に、エラーとなった場合に、どのようにするべきか悩んだ。

まずは、単純にtry/catchで書いてみる。

try{
    Connect-VIServer -Server $vcenter -User $vcenterusername -Password $vcenterpassword -WarningAction 0
} catch {
    Write-Host "vCenterサーバへの接続で問題が発生しました。処理を終了します。"
    Write-Host $Error[0]
    exit 1
}

これを$vcenterなどの変数設定なしに実行すると以下のエラーとなり、想定どおりの出力とはなる。

vCenterサーバへの接続で問題が発生しました。処理を終了します。
Cannot validate argument on parameter 'Server'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

しかし、下記のように$vcenterなどに値を入れているが、実はでたらめで接続できなかった場合を試してみると問題が発生した。

$vcenter="testvcenter"
$vcenterusername="administrator@vsphere.local"
$vcenterpassword="test"
try{
    Connect-VIServer -Server $vcenter -User $vcenterusername -Password $vcenterpassword -WarningAction 0
} catch {
    Write-Host "vCenterサーバへの接続で問題が発生しました。処理を終了します。"
    Write-Host $Error[0]
    exit 1
}

実行結果は以下のようになり、catch内が実行されていない。

Connect-VIServer : 2017/02/09 17:55:54    Connect-VIServer        Could not resolve the requested VC server.
Additional Information: There was no endpoint listening at https://testvcenter/sdk that could accept the message. This is often caused by an incorrect address or SOAP a
ction. See InnerException, if present, for more details.    
At line:6 char:5
+     Connect-VIServer -Server $vcenter -User $vcenterusername -Passwor ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Connect-VIServer], ViServerConnectionException
    + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_NameResolutionFailure,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer

これを、Connect-VIServerの実行結果を見て、falseだったら例外を発生させる、ということにより、対応させることができた。

$vcenter="testvcenter"
$vcenterusername="administrator@vsphere.local"
$vcenterpassword="test"
try{
    Connect-VIServer -Server $vcenter -User $vcenterusername -Password $vcenterpassword -WarningAction 0
    if($? -eq $false){ throw }
} catch {
    Write-Host "vCenterサーバへの接続で問題が発生しました。処理を終了します。"
    Write-Host $Error[0]
    exit 1
}

誤りの場合の実行例は以下のようになる。

Connect-VIServer : 2017/02/09 18:00:57    Connect-VIServer        Could not resolve the requested VC server.
Additional Information: There was no endpoint listening at https://testvcenter/sdk that could accept the message. This is often caused by an incorrect address 
or SOAP action. See InnerException, if present, for more details.    
At line:6 char:5
+     Connect-VIServer -Server $vcenter -User $vcenterusername -Passwor ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Connect-VIServer], ViServerConnectionException
    + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_NameResolutionFailure,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer
 
vCenterサーバへの接続で問題が発生しました。処理を終了します。
ScriptHalted

2018/07/06追記

PowerCLI 10.0以降では、自己証明はデフォルト拒否になりました。 このため、上にあげたスクリプトを実行すると下記の様にエラーとなります。

PS /root> Connect-VIServer -Server IPアドレス -User root -Password password -WarningAction 0
Connect-VIServer : 2018/07/06 14:29:03  Connect-VIServer                Error: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction option to Ignore to ignore the certificate errors for this connection.
Additional Information: Could not establish trust relationship for the SSL/TLS secure channel with authority 'IPアドレス'.
At line:1 char:1
+ Connect-VIServer -Server IPアドレス -User root -Password password - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : SecurityError: (:) [Connect-VIServer], ViSecurityNegotiationException
+ FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_CertificateError,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer

PS /root>

VMware PowerCLI Blog「New Release: VMware PowerCLI 10.0.0」の「Default Certificate Handling」に記載があるように、全体設定として無視するための「Set-PowerCLIConfiguration -InvalidCertificateAction Ignore」を設定する必要があります。

PS /root> Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

Perform operation?
Performing operation 'Update PowerCLI configuration.'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):y

Scope    ProxyPolicy     DefaultVIServerMode InvalidCertificateAction  DisplayD
                                                                       eprecati
                                                                       onWarnin
                                                                       gs
-----    -----------     ------------------- ------------------------  --------
Session  UseSystemProxy  Multiple            Ignore                    True
User                                         Ignore
AllUsers


PS /root>

この処理を実行後に、改めて接続を実行

PS /root> Connect-VIServer -Server IPアドレス -User root -Password password
Name Port User
—- —- —-
IPアドレス 443 root

PS /root>

Armbianの初期設定項目

いつもなんとなくやっちゃってるArmbianの初期設定
忘れないようにメモ書きを作成

その1: rootの初期パスワード
「1234」
初回ログイン時に変更する

その2: とりあえずOSアップデート
Armbian自体の更新+Ubuntu/Debianの更新を実施
「apt-get update」「apt-get upgrade」

その3: NFSクライアントインストール
うちの環境ではOrange Pi Plus2によるNFSサーバがあり、データ共有をしているのでnfs-commonをインストール
「apt-get install nfs-common」

その4: Locale/Timezone設定
言語設定
「dpkg-reconfigure locales」
タイムゾーン設定
「dpkg-reconfigure tzdata」
キーボードの言語設定(キー配列)
「dpkg-reconfigure keyboard-configuration」

その5:動作CPU範囲設定
ファイル「/etc/default/cpufrequtils」にて記載。
機種によって異なり、Orange Pi Oneでは下記の様に、「480MHz~1200MHz」がCPUの動作範囲となる

ENABLE=true
MIN_SPEED=480000
MAX_SPEED=1200000
GOVERNOR=interactive

ファイルを編集した場合は、「service cpufrequtils restart」で適用

Power Shell Core 6.0をCentOS7で使ってみる

2017/02/01にPowerShell Core 6.0がLinux環境向けにもリリースされた
Installing latest PowerShell Core 6.0 Release on Linux just got easier!

PowerShellの開発はGithubの「https://github.com/PowerShell/PowerShell」で行われており、導入手順も書かれている。
github上の「Linux向けインストール手順」では、直接RPMファイル/debファイルを指定してインストールする、というものが記載されている。

しかし、「Installing latest PowerShell Core 6.0 Release on Linux just got easier!」の記事の中では、yumやaptなどでプログラムの更新をサポートする形でのインストール手順が示されている。

CentOS7だと下記のようになる

# curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/microsoft.repo
# yum install powershell
#

実際に実行してみると下記の様になります。

[root@blog ~]# curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/microsoft.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   193  100   193    0     0    209      0 --:--:-- --:--:-- --:--:--   210
[root@blog ~]# yum install powershell
読み込んだプラグイン:fastestmirror, langpacks
packages-microsoft-com-prod                              | 2.9 kB     00:00
packages-microsoft-com-prod/primary_db                     | 9.9 kB   00:00
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.riken.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ powershell.x86_64 0:6.0.0_alpha.15-1.el7.centos を インストール
--> 依存性の処理をしています: uuid のパッケージ: powershell-6.0.0_alpha.15-1.el7.centos.x86_64
--> 依存性の処理をしています: libicu のパッケージ: powershell-6.0.0_alpha.15-1.el7.centos.x86_64
--> 依存性の処理をしています: libunwind のパッケージ: powershell-6.0.0_alpha.15-1.el7.centos.x86_64
--> トランザクションの確認を実行しています。
---> パッケージ libicu.x86_64 0:50.1.2-15.el7 を インストール
---> パッケージ libunwind.x86_64 2:1.1-5.el7_2.2 を インストール
---> パッケージ uuid.x86_64 0:1.6.2-26.el7 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

================================================================================
 Package   アーキテクチャー
                  バージョン                  リポジトリー                 容量
================================================================================
インストール中:
 powershell
           x86_64 6.0.0_alpha.15-1.el7.centos packages-microsoft-com-prod  39 M
依存性関連でのインストールをします:
 libicu    x86_64 50.1.2-15.el7               base                        6.9 M
 libunwind x86_64 2:1.1-5.el7_2.2             base                         56 k
 uuid      x86_64 1.6.2-26.el7                base                         55 k

トランザクションの要約
================================================================================
インストール  1 パッケージ (+3 個の依存関係のパッケージ)

総ダウンロード容量: 46 M
インストール容量: 64 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): uuid-1.6.2-26.el7.x86_64.rpm                        |  55 kB   00:00
(2/4): libunwind-1.1-5.el7_2.2.x86_64.rpm                  |  56 kB   00:00
(3/4): libicu-50.1.2-15.el7.x86_64.rpm                     | 6.9 MB   00:04
warning: /var/cache/yum/x86_64/7/packages-microsoft-com-prod/packages/powershell-6.0.0_alpha.15-1.el7.centos.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID be1229cf: NOKEY
powershell-6.0.0_alpha.15-1.el7.centos.x86_64.rpm の公開鍵がインストールされていません
(4/4): powershell-6.0.0_alpha.15-1.el7.centos.x86_64.rpm   |  39 MB   00:21
--------------------------------------------------------------------------------
合計                                               2.2 MB/s |  46 MB  00:21
https://packages.microsoft.com/keys/microsoft.asc から鍵を取得中です。
Importing GPG key 0xBE1229CF:
 Userid     : "Microsoft (Release signing) <gpgsecurity@microsoft.com>"
 Fingerprint: bc52 8686 b50d 79e3 39d3 721c eb3e 94ad be12 29cf
 From       : https://packages.microsoft.com/keys/microsoft.asc
上記の処理を行います。よろしいでしょうか? [y/N]y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : uuid-1.6.2-26.el7.x86_64                        1/4
  インストール中          : 2:libunwind-1.1-5.el7_2.2.x86_64                2/4
  インストール中          : libicu-50.1.2-15.el7.x86_64                     3/4
  インストール中          : powershell-6.0.0_alpha.15-1.el7.centos.x86_64   4/4
  検証中                  : powershell-6.0.0_alpha.15-1.el7.centos.x86_64   1/4
  検証中                  : libicu-50.1.2-15.el7.x86_64                     2/4
  検証中                  : 2:libunwind-1.1-5.el7_2.2.x86_64                3/4
  検証中                  : uuid-1.6.2-26.el7.x86_64                        4/4

インストール:
  powershell.x86_64 0:6.0.0_alpha.15-1.el7.centos

依存性関連をインストールしました:
  libicu.x86_64 0:50.1.2-15.el7         libunwind.x86_64 2:1.1-5.el7_2.2
  uuid.x86_64 0:1.6.2-26.el7

完了しました!
[root@blog ~]#

が・・・
私の環境では、いつもの癖で「ls」と実行してしまったら・・・

bash-4.2$ ls
11-0-1-0048_SAS_FW_Image_1-40-342-1650.zip  MegaSAS.log  sql  web  work
bash-4.2$ powershell
PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS /home/osakanataro> ls
11-0-1-0048_SAS_FW_Image_1-40-342-1650.zip  MegaSAS.log  sql  web  work
(ここで応答が無くなる)

ん???
UNIX系コマンドをPowerShellから実行した場合の動作に難有りなのかも?

PS /home/osakanataro> Get-Item *| ForEach-Object { $_ }


    Directory: /home/osakanataro


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---       2017/02/06     17:35                sql
d-----       2017/02/03     19:08                web
d-----       2017/02/03     19:19                work
------       2017/01/04     13:01        1528036 11-0-1-0048_SAS_FW_Image_1-40-
                                                 342-1650.zip
--r---       2017/01/04     13:07            390 MegaSAS.log

PS /home/osakanataro> dir


    Directory: /home/osakanataro


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---       2017/02/06     17:35                sql
d-----       2017/02/03     19:08                web
d-----       2017/02/03     19:19                work
------       2017/01/04     13:01        1528036 11-0-1-0048_SAS_FW_Image_1-40-
                                                 342-1650.zip
--r---       2017/01/04     13:07            390 MegaSAS.log


PS /home/osakanataro>

PwerShellらしいこと、ということで、下記の処理をやってみる

PS /home/osakanataro> $results=Get-Item *| ForEach-Object { $_ }
PS /home/osakanataro> $results | Export-Csv test.csv -Encoding UTF  (タブ補完で候補を出す)
UTF32  UTF7   UTF8
PS /home/osakanataro> $results | Export-Csv test.csv -Encoding UTF8 -No  (タブ補完で候補を出す)
NoClobber          NoOverwrite        NoTypeInformation
PS /home/osakanataro> $results | Export-Csv test.csv -Encoding UTF8 -NoTypeInformation
PS /home/osakanataro>

出力結果のcsvは、Windows上のPowerShellで実行した場合と同じ書式のモノとなりました。

$PSVersionTableの内容は下記のようになっていました。

$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.15
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

以下はおまけ。
Microsoftのレポジトリとして指定されている「https://packages.microsoft.com/rhel/7/prod/」とか「https://packages.microsoft.com/config/rhel/7/」を見ていくとなかなか面白い
https://packages.microsoft.com/config/rhel/7/mssql-server.repo」という用にCentOS7向けのMSSQL Serverっぽいレポジトリファイルがある。RPMファイルは「https://packages.microsoft.com/rhel/7/mssql-server/」にある。

MSSQL Serverのインストール手順については「Install SQL Server on Linux」を参照のこと。

Allwinner H5搭載Orange Pi PC2向けにArmbianの開発中版が出た

Allwinner H5搭載のOrange Pi PC2で動くLinux OSは、Orange Pi公式が出しているDebianとUbuntuだけでした。

Orange Pi公式の問題点は1つ。
kernelのアップデート提供実績がほぼ無い。
ある場合はOSイメージまるごと配布。

こんなんじゃ、継続運用に使えないわけです。
Allwinner系を中心にARM SoC向けにDebian/Ubuntuベースのディストリビューションとして「Armbian」というものがあります。
Allwinner H3搭載のOrange Pi向けも各機種出てますが、この度、Orange Pi PC2向けも登場しました。

Armbain for Orange Pi PC2

現状は、まだNightly版ということで開発中のモノしかありません。
「Warning: nightly downloads are automated untested builds and no end user support is provided for them!」とあるように、とりあえず出している、というやつです。

しかしながら、X-Windowが表示されるDesktop版も、しばらく使ってみた感じでは問題無く動いているようですし、一般的な用途では使えそうです。
ただ、ヒートシンクもしくは冷却ファンが必須です。
標準状態ではかなり発熱します。

2017/02/07時点でダウンロード可能な「Armbian_5.24.170202_Orangepipc2_Ubuntu_xenial_dev_4.10.0_desktop.7z」は、Ubuntu 16.04.1で、Linux kernelは4.10.0と、現在開発中の最新Linux kernelをベースとしています。

LinkStation LS410Dの1TBモデルを4TBに変えた

buffshopでLinkStaton LS410Dの1TBモデル(LS410D0101)が7,900円で売っていた。
そして、4TB HDD単品が8,800円だった。

LinkStationは、HDD初期状態からOSをセットアップできる技術が確立している・・・

というわけで、買ってみました。

  1. LS410Dの中を開ける
  2. HDDを交換
  3. LinkStationのアップデートzipを入手し、uImageとinitrdを取り出す(パスワードはググれ)
  4. Windowsマシンにtftpサーバを立てる
  5. LinkStationの電源を入れていろいろやる(詳しくはググれ)
  6. LinkStationのアップデータのexeを実行して、firmwareアップデート実施(詳しくはググれ
  7. LinkStationの初期化が終わって正常起動する

という感じで、問題無く4TBモデルが完成しました。

DTCP-IP機能も問題なく動作しています。