vSphere PowerCLIの一部コマンドがPowerShell ISEで動作しない

2016/12/15追記

Add-Snappinは、古いPowerShellの場合だけだったようなので、新しいバージョンの場合にも対応したものを下記に記載しています。

vSphere PowerCLIの一部コマンドがPowerShell ISEで動作しない 2016/12/15版


vSphere PowerCLIで変な動作に遭遇した。
vSphere PowerCLIの一部コマンドがPowerShell ISE上だと動作しない、という問題である。
元々、PowerShell ISEの標準設定ではvSphere PowerCLIのコマンドは動かない。
「Add-PSSnapin VMware.VimAutomation.Core」を追加し、モジュールを読み込ませる必要があるのだが、それを行っても動かないものがいくつかあることが分かった。

まずは状況の確認から・・・

現在のvSphere Power CLIのバージョンを確認
Determining the build number for vSphere PowerCLI or VI Toolkit for Windows」より「Get-VIToolkitVersion」を実行。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VIToolkitVersion

PowerCLI Version
----------------
   VMware vSphere PowerCLI 6.0 Release 1 build 2548067
---------------
Component Versions
---------------
   VMWare AutoDeploy PowerCLI Component 6.0 build 2358282
   VMWare ImageBuilder PowerCLI Component 6.0 build 2358282
   VMware License PowerCLI Component 6.0 build 2315846
   VMware vSphere PowerCLI Component 6.0 build 2548068
   VMware Cloud Infrastructure Suite PowerCLI Component 6.0 build 2548068
   VMware HA PowerCLI Component 6.0 build 2510422
   VMware PowerCLI Component for Storage Management 6.0 build 2522368
   VMware VDS PowerCLI Component 6.0 build 2548068



PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

(ちなみに、この記事を書いてる時点では、これより新しいバージョンが出ている)

vCenterサーバに接続

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Connect-VIServer -Server vCenterServer -User root -Password vmware

Name                           Port  User
----                           ----  ----
vCenterServer                  443   root


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

そして、「Get-VdsCommand」を実行してvSphre PowerCLIが提供するvDS関連コマンド一覧を取得。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-VdsCommand

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Cmdlet          Add-VDSwitchPhysicalNetworkAdapter                 VMware.Vi...
Cmdlet          Add-VDSwitchVMHost                                 VMware.Vi...
Cmdlet          Export-VDPortGroup                                 VMware.Vi...
Cmdlet          Export-VDSwitch                                    VMware.Vi...
Cmdlet          Get-VDBlockedPolicy                                VMware.Vi...
Cmdlet          Get-VDPort                                         VMware.Vi...
Cmdlet          Get-VDPortgroup                                    VMware.Vi...
Cmdlet          Get-VDPortgroupOverridePolicy                      VMware.Vi...
Cmdlet          Get-VDSecurityPolicy                               VMware.Vi...
Cmdlet          Get-VDSwitch                                       VMware.Vi...
Cmdlet          Get-VDSwitchPrivateVlan                            VMware.Vi...
Cmdlet          Get-VDTrafficShapingPolicy                         VMware.Vi...
Cmdlet          Get-VDUplinkLacpPolicy                             VMware.Vi...
Cmdlet          Get-VDUplinkTeamingPolicy                          VMware.Vi...
Cmdlet          New-VDPortgroup                                    VMware.Vi...
Cmdlet          New-VDSwitch                                       VMware.Vi...
Cmdlet          New-VDSwitchPrivateVlan                            VMware.Vi...
Cmdlet          Remove-VDPortGroup                                 VMware.Vi...
Cmdlet          Remove-VDSwitch                                    VMware.Vi...
Cmdlet          Remove-VDSwitchPhysicalNetworkAdapter              VMware.Vi...
Cmdlet          Remove-VDSwitchPrivateVlan                         VMware.Vi...
Cmdlet          Remove-VDSwitchVMHost                              VMware.Vi...
Cmdlet          Set-VDBlockedPolicy                                VMware.Vi...
Cmdlet          Set-VDPort                                         VMware.Vi...
Cmdlet          Set-VDPortgroup                                    VMware.Vi...
Cmdlet          Set-VDPortgroupOverridePolicy                      VMware.Vi...
Cmdlet          Set-VDSecurityPolicy                               VMware.Vi...
Cmdlet          Set-VDSwitch                                       VMware.Vi...
Cmdlet          Set-VDTrafficShapingPolicy                         VMware.Vi...
Cmdlet          Set-VDUplinkLacpPolicy                             VMware.Vi...
Cmdlet          Set-VDUplinkTeamingPolicy                          VMware.Vi...
Cmdlet          Set-VDVlanConfiguration                            VMware.Vi...


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> 

ここまでは良い。

PowerShell ISEでの動作が問題。
PowerShell ISE上部の編集画面に下記を入力。

Add-PSSnapin VMware.VimAutomation.Core
Get-VIToolkitVersion
Connect-VIServer -Server vCenterServer -User root -Password vmware
Get-VdsCommand

最初の1行はvSphere PowerCLIのコマンドを実行させるため設定。
これを実行すると・・・

PS C:\Users\osakanataro> Add-PSSnapin VMware.VimAutomation.Core
Get-VIToolkitVersion
Connect-VIServer -Server vCenterServer -User root -Password vmware
Get-VdsCommand

Get-VIToolkitVersion : 用語 'Get-VIToolkitVersion' は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。名前が正しく記述されていることを確認し、パスが含まれている場合はそのパスが正しいことを確認してから、
再試行してください。
発生場所 行:2 文字:1
+ Get-VIToolkitVersion
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-VIToolkitVersion:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 

Name                           Port  User                          
----                           ----  ----                          
vCenterServer                  443   root                          
Get-VdsCommand : 用語 'Get-VdsCommand' は、コマンドレット、関数、スクリプト ファイル、または操作可能なプログラムの名前として認識されません。名前が正しく記述されていることを確認し、パスが含まれている場合はそのパスが正しいことを確認してから、再試行してください。
発生場所 行:4 文字:1
+ Get-VdsCommand
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-VdsCommand:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 



PS C:\Users\osakanataro> 

エラーになる・・・
なぜなのか・・・

まずはPowerShell ISE内で「Get-Module」を実行し、現在使用できるコマンドを確認

PS C:\Users\osakanataro> Get-Module

ModuleType Version    Name                                ExportedCommands                                                                            
---------- -------    ----                                ----------------                                                                            
Script     1.0.0.0    ISE                                 {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}                                         
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}                          
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}   
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}                                   
Manifest   3.0.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}             



PS C:\Users\osakanataro> 

比較のためvSphere PowerCLIの方でも「Get-Module」を実行してみる

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        Initialize-VMware_VimAutomation_Cis
Script     0.0        Initialize-VMware_VimAutomation_Vds
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-T...
Binary     6.0.0.0    VMware.VimAutomation.Cis.Core       {Connect-CisServer...
Manifest   6.0.0.0    VMware.VimAutomation.Core           {Add-PassthroughDe...
Binary     6.0.0.0    VMware.VimAutomation.HA             Get-DrmInfo
Manifest   6.0.0.0    VMware.VimAutomation.Sdk
Binary     6.0.0.0    VMware.VimAutomation.Storage        {Export-SpbmStorag...
Binary     6.0.0.0    VMware.VimAutomation.Vds            {Add-VDSwitchPhysi...


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

全然違う
「VMware.VimAutomation.Vds」を追加すると実行できそうな雰囲気・・・

しかし、「Add-PSSnapin VMware.VimAutomation.Vds」は下記のエラーとなる。

Add-PSSnapin : Windows PowerShell スナップイン 'VMware.VimAutomation.Vds' がこのコンピューターにインストールされていません。
発生場所 行:2 文字:1
+ Add-PSSnapin VMware.VimAutomation.Vds
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (VMware.VimAutomation.Vds:String) [Add-PSSnapin]、PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

ではどうするか?

「Get-Command -Module モジュール名」で指定したモジュールでどんなコマンドが実行できるかを表示すると実行でいるようになるらしい。

そんな馬鹿な、と思いつつ、$DUMMY環境変数に放り込むように変更。

Add-PSSnapin VMware.VimAutomation.Core
$DUMMY=Get-Command -Module VMware.VimAutomation.Vds
Get-VIToolkitVersion
Connect-VIServer -Server vCenterServer -User root -Password vmware
Get-VdsCommand

そして実行

PS C:\Users\osakanataro> Add-PSSnapin VMware.VimAutomation.Core
$DUMMY=Get-Command -Module VMware.VimAutomation.Vds
Get-VIToolkitVersion
Connect-VIServer -Server vCenterServer -User root -Password vmware
Get-VdsCommand


PowerCLI Version
----------------
   VMware vSphere PowerCLI 6.0 Release 1 build 2548067
---------------
Component Versions
---------------
   VMWare AutoDeploy PowerCLI Component 6.0 build 2358282
   VMWare ImageBuilder PowerCLI Component 6.0 build 2358282
   VMware License PowerCLI Component 6.0 build 2315846
   VMware vSphere PowerCLI Component 6.0 build 2548068
   VMware VDS PowerCLI Component 6.0 build 2548068


IsConnected   : True
Id            : /VIServer=root@vCenterServer:443/
ServiceUri    : https://vCenterServer/sdk
SessionSecret : cb8924f9fb4b2cb6cb75a357d8127e2154376d17
Name          : vCenterServer
Port          : 443
SessionId     : cb8924f9fb4b2cb6cb75a357d8127e2154376d17
User          : root
Uid           : /VIServer=root@vCenterServer:443/
Version       : 6.0
Build         : 2776510
ProductLine   : vpx
InstanceUuid  : 1a31cfc8-0931-4752-9573-461f044cc2d4
RefCount      : 6
ExtensionData : VMware.Vim.ServiceInstance
Client        : VMware.VimAutomation.ViCore.Impl.V1.VimClient


Verb                : Add
Noun                : VDSwitchPhysicalNetworkAdapter
HelpFile            : VMware.VimAutomation.Vds.Commands.dll-Help.xml
PSSnapIn            : 
ImplementingType    : VMware.VimAutomation.Vds.Commands.Cmdlets.AddVDSwitchPhysicalNetworkAdapter
Definition          : 
                      Add-VDSwitchPhysicalNetworkAdapter [-VMHostPhysicalNic] <PhysicalNic&#91;&#93;> [-DistributedSwitch] <DistributedSwitch> [-VirtualNicPor
                      tgroup <VDPortgroup&#91;&#93;>] [-VMHostVirtualNic <HostVirtualNic&#91;&#93;>] [-Server <VIServer&#91;&#93;>] [-WhatIf] [-Confirm] [<CommonParameters>]
                      
DefaultParameterSet : 
OutputType          : {System.Void}
Options             : ReadOnly
Name                : Add-VDSwitchPhysicalNetworkAdapter
CommandType         : Cmdlet
Visibility          : Public
ModuleName          : VMware.VimAutomation.Vds
Module              : VMware.VimAutomation.Vds
RemotingCapability  : PowerShell
Parameters          : {[VMHostPhysicalNic, System.Management.Automation.ParameterMetadata], [DistributedSwitch, System.Management.Automation.Parameter
                      Metadata], [VirtualNicPortgroup, System.Management.Automation.ParameterMetadata], [VMHostVirtualNic, System.Management.Automatio
                      n.ParameterMetadata]...}
ParameterSets       : {[-VMHostPhysicalNic] <PhysicalNic&#91;&#93;> [-DistributedSwitch] <DistributedSwitch> [-VirtualNicPortgroup <VDPortgroup&#91;&#93;>] [-VMHostVi
                      rtualNic <HostVirtualNic&#91;&#93;>] [-Server <VIServer&#91;&#93;>] [-WhatIf] [-Confirm] [<CommonParameters>]}
HelpUri             : http://www.vmware.com/support/developer/PowerCLI/PowerCLI60R1/html/Add-VDSwitchPhysicalNetworkAdapter.html
DLL                 : C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules\VMware.VimAutomation.Vds\VMware.VimAutomation.Vds.Commands
                      .dll

<略>

Verb                : Set
Noun                : VDVlanConfiguration
HelpFile            : VMware.VimAutomation.Vds.Commands.dll-Help.xml
PSSnapIn            : 
ImplementingType    : VMware.VimAutomation.Vds.Commands.Cmdlets.Policies.SetVDVlanConfiguration
Definition          : 
                      Set-VDVlanConfiguration -VDPortgroup <VDPortgroup&#91;&#93;> [-DisableVlan] [-VlanId <int>] [-VlanTrunkRange <VlanRangeList>] [-PrivateV
                      lanId <int>] [-WhatIf] [-Confirm] [<CommonParameters>]
                      
                      Set-VDVlanConfiguration -VDSwitch <VDSwitch&#91;&#93;> [-DisableVlan] [-VlanId <int>] [-VlanTrunkRange <VlanRangeList>] [-PrivateVlanId 
                      <int>] [-WhatIf] [-Confirm] [<CommonParameters>]
                      
                      Set-VDVlanConfiguration -VDPort <VDPort&#91;&#93;> [-DisableVlan] [-VlanId <int>] [-VlanTrunkRange <VlanRangeList>] [-PrivateVlanId <int
                      >] [-WhatIf] [-Confirm] [<CommonParameters>]
                      
DefaultParameterSet : 
OutputType          : {VMware.VimAutomation.Vds.Types.V1.VlanConfiguration}
Options             : ReadOnly
Name                : Set-VDVlanConfiguration
CommandType         : Cmdlet
Visibility          : Public
ModuleName          : VMware.VimAutomation.Vds
Module              : VMware.VimAutomation.Vds
RemotingCapability  : PowerShell
Parameters          : {[VDPortgroup, System.Management.Automation.ParameterMetadata], [VDSwitch, System.Management.Automation.ParameterMetadata], [VDP
                      ort, System.Management.Automation.ParameterMetadata], [DisableVlan, System.Management.Automation.ParameterMetadata]...}
ParameterSets       : {-VDPortgroup <VDPortgroup&#91;&#93;> [-DisableVlan] [-VlanId <int>] [-VlanTrunkRange <VlanRangeList>] [-PrivateVlanId <int>] [-WhatIf] 
                      [-Confirm] [<CommonParameters>], -VDSwitch <VDSwitch&#91;&#93;> [-DisableVlan] [-VlanId <int>] [-VlanTrunkRange <VlanRangeList>] [-Priva
                      teVlanId <int>] [-WhatIf] [-Confirm] [<CommonParameters>], -VDPort <VDPort&#91;&#93;> [-DisableVlan] [-VlanId <int>] [-VlanTrunkRange <V
                      lanRangeList>] [-PrivateVlanId <int>] [-WhatIf] [-Confirm] [<CommonParameters>]}
HelpUri             : http://www.vmware.com/support/developer/PowerCLI/PowerCLI60R1/html/Set-VDVlanConfiguration.html
DLL                 : C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Modules\VMware.VimAutomation.Vds\VMware.VimAutomation.Vds.Commands
                      .dll




PS C:\Users\osakanataro> 

ほんとに実行できたーーー
じゃぁ、この段階での「Get-Module」はどうなってるか確認してみると、ちゃんとVMware.VimAutomation.Vdsが読み込まれてる・・・
謎な動作をしてますね・・・

PS C:\Users\osakanataro> Get-Module

ModuleType Version    Name                                ExportedCommands                                                                            
---------- -------    ----                                ----------------                                                                            
Script     0.0        Initialize-VMware_VimAutomation_Vds                                                                                             
Script     1.0.0.0    ISE                                 {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}                                         
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}                          
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl, Get-AuthenticodeSignature...}   
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}                                   
Manifest   3.0.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, Enable-WSManCredSSP...}             
Manifest   6.0.0.0    VMware.VimAutomation.Core                                                                                                       
Manifest   6.0.0.0    VMware.VimAutomation.Sdk                                                                                                        
Binary     6.0.0.0    VMware.VimAutomation.Vds            {Add-VDSwitchPhysicalNetworkAdapter, Add-VDSwitchVMHost, Export-VDPortGroup, Export-VDSwi...



PS C:\Users\osakanataro> 

PowerShellを使ってVMwareのテンプレートからデプロイで「既存のカスタマイズ仕様を使用してカスタマイズする」を行う方法

VMware vSphere環境にて、一度、GUI操作でテンプレートのデプロイを行い、その結果をカスタマイズ仕様として保存している、という状態で、VMware Power CLIを使用して、GUIの「既存のカスタマイズ仕様を使用してカスタマイズする」相当の操作を行う場合、どのようにするかを説明する。

手順のポイント

・カスタマイズ仕様は「OSCustomizationSpec」と「OSCustomizationNicMapping」の2つから成り立っている

・既存のカスタマイズ仕様をコピーした一時的なカスタマイズ仕様を作成し、それを編集して使用する

・仮想マシンの電源を1度入れないとカスタマイズされない(カスタマイズ後、再起動される)

・使うcmdletは以下
  カスタマイズ仕様関連
    Get-OSCustomizationSpec, New-OSCustomizationSpec
    Get-OSCustomizationNicMapping, Set-OSCustomizationNicMapping
  テンプレートデプロイ関連
    Get-VMHost
    New-VM
  仮想マシンのハードウェア構成変更
    Get-VM, Set-VM

手順解説

その1 vCenterサーバに接続

vSphere PowerCLIによる操作を行う際、まずは「Connect-VIServer」により、操作対象のvCenterサーバに接続します。

下記はvCenter vApp(VCSA)の初期設定である「root」「vmware」で接続する場合の例です。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Connect-VIServer -Server IPアドレス -User root -Password vmware

Name                           Port  User
----                           ----  ----
IPアドレス                     443   root


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

その2 既存カスタマイズ仕様をコピーしたカスタマイズ仕様を作成

既存のカスタマイズ仕様を一時的に変更してデプロイする、ということができないため、コピーしたカスタマイズ仕様を変更する、という手順を取る。

ただ、コピーの際に「-Type NonPersistent」というオプションを指定すると、このセッション中のみ使用できる一時的なカスタマイズ仕様として作成することができるため、デプロイ後の削除まで考える必要はない。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-OSCustomizationSpec cent6-dep | New-OSCustomizationSpec -Name cent6-temp -Type NonPersistent

Name                                         Description Type          OSType
----                                         ----------- ----          ------
cent6-temp                                               NonPersistent Linux


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

その3 IPアドレスなど設定

IPアドレスやデフォルトゲートウェイなどネットワークに関する設定は「Get-OSCustomizationNicMapping」にて行う。

NICを増やす場合もここで行う。(ホスト名についてはここでは無い)

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-OSCustomizationNicMapping -Spec cent6-temp | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 172.17.44.55 -SubnetMask 255.255.0.0 -DefaultGateway 172.17.44.39

Position IPMode           IPAddress       DefaultGateway
-------- ------           ---------       --------------
       1 UseStaticIP      172.17.44.55    172.17.44.39


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

その4 仮想マシンホスト名の命名規則指定

仮想マシンにつけるホスト名をどのようなルールで行うかというのを指定する。

「-NamingScheme vm」は、仮想マシンの登録名とホスト名を同じとする、というものになる。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-OSCustomizationSpec cent6-temp | Set-OSCustomizationSpec -NamingScheme vm

Name                                         Description Type          OSType
----                                         ----------- ----          ------
cent6-temp                                               NonPersistent Linux


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

その5 仮想マシンのデプロイ

今回は「nimblestorage」という名前のDataStoreを指定して、仮想マシンのデプロイを実施するという形で実行している。

先の項目で、仮想マシンの登録名とホスト名を同じとしているので「-Name cent6-ip55」で指定した「cent6-ip55」がホスト名となる。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> New-VM -Name cent6-ip55  -VMHost vmserver21.vmlab.local -Template cent6-deploy -OSCustomizationSpec cent6-temp -Datastore nimblestorage

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
cent6-ip55           PoweredOff 1        2.000


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

なお、デプロイ中に強制終了を行うと、それ以後、動作がおかしくなることがあるようだ。

今回遭遇した事例としては、下記の様なエラーがでるが、デプロイ自体は実施されていた。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> New-VM -Name cent6-ip55 -VMHost vmserver21.vmlab.local -Template cent6-deploy -OSCustomizationSpec cent6-temp -Datastore nimblestorage
New-VM : 2015/07/16 18:03:02    New-VM        オブジェクトの現在の状態に問題が
あるため、操作は有効ではありません。
発生場所 行:1 文字:1
+ New-VM -Name cent6-ip55 -VMHost vmserver21.vmlab.local -Template cent6-depl
oy  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : NotSpecified: (:) [New-VM], VimException
    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomatio
   n.ViCore.Cmdlets.Commands.NewVM

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

その6 CPU/メモリの変更

作成した仮想マシンのCPUとメモリの割り当てを「Set-VM」で変更します。

なお、このSet-VMには「-Name」というオプションがありますが、仮想マシンを指定する際は「-VM 仮想マシン名」という風に指定します。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Set-VM -VM cent6-ip55 -NumCpu 2 -MemoryMB 4

Confirmation
Proceed to configure the following parameters of the virtual machine with name
'cent6-ip55'?
New MemoryMB: 4MB
New NumCpu: 2
[Y] はい(Y)  [A] すべて続行(A)  [N] いいえ(N)  [L] すべて無視(L)  [S] 中断(S)
[?] ヘルプ(既定値は "Y"): y

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
cent6-ip55           PoweredOff 2        0.004


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

上記の出力結果をよく見てみるとわかるのですが、実はメモリ容量はMB単位での指定となっています。このため、現状はなんと4MBしか割り当てられていません。これではMS-DOSぐらいしか起動できません。

「4G」と指定したいところですが、サポートされていないため、「4096」という形で指定します。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Set-VM -VM cent6-ip55 -NumCpu 2 -MemoryMB 4096

Confirmation
Proceed to configure the following parameters of the virtual machine with name
'cent6-ip55'?
New MemoryMB: 4096MB
New NumCpu: 2
[Y] はい(Y)  [A] すべて続行(A)  [N] いいえ(N)  [L] すべて無視(L)  [S] 中断(S)
[?] ヘルプ(既定値は "Y"): y

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
cent6-ip55           PoweredOff 2        4.000


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

その7 仮想マシンの電源ON

デプロイ時の初期設定は仮想マシンの初回起動時に行われるため、まずはPowerShellから電源オンを行うため「Start-VM」を実行します。

PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Start-VM -VM cent6-ip55

Name                 PowerState Num CPUs MemoryGB
----                 ---------- -------- --------
cent6-ip55           PoweredOn  2        4.000


PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>

電源投入後、初回起動時にホスト名/IPアドレスの設定スクリプトが実行されます。また、完了後、自動的に再起動も行われます。

その8 デプロイ完了

再起動が終わったらデプロイが完了です。

proxmox/openvzでCentOS7のコンテナを作ったけど通信ができない

Debianベースの仮想化環境Proxmox VEは、KVM/qemuベースのハードウェア仮想と、OpenVZのコンテナ仮想の2種類が使用できる。

このうち、OpenVZのコンテナの方で、CentOS7を作ったところ、通信が行えないという現象が発生した。

元となるCentOS7のOpenVZテンプレートは、「OpenVZ公式 Download/template/precreated」から入手した。

で、普通に導入して起動してみると、IPアドレスが割り当てられていない。

root@proxmox:~# vzctl start 777
Starting container ...
Container is mounted
Adding IP address(es): 192.168.35.20
vSetting CPU units: 1000
Setting CPUs: 2
zContainer start in progress...
root@proxmox:~# vzctl enter 777
entered into CT 777
[root@centos7 /]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
    link/void
    inet 127.0.0.1/32 scope host venet0
[root@centos7 /]#

検索してみると、すぐに情報が出てきた。
NO IP ADDRESS IN PROXMOX OPENVZ CENTOS 7 CONTAINER

RHEL/CentOS 7.1におけるバグで、「Bug 1207975 – ifup-aliases does not proper catch arping failure」にて修正されているとのこと。

起動時にネットワークを有効化している /etc/sysconfig/network-scripts/ifup-aliases 内の

if ! /sbin/arping -q -c 2 -w ${ARPING_WAIT:-3} -D -I ${parent_device} ${IPADDR} ; then

という条件式の書き方に問題があるようで

/sbin/arping -q -c 2 -w ${ARPING_WAIT:-3} -D -I ${parent_device} ${IPADDR}
if [ $? = 1 ] ; then

と2行に分けることで回避できるとのこと。

なので、起動しているコンテナ内の/etc/sysconfig/network-scripts/ifup-aliasesを書き換えることで対処できました。

[root@centos7 ~]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: venet0: <BROADCAST,POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN
    link/void
    inet 127.0.0.1/32 scope host venet0
    inet 192.168.35.20/32 brd 192.168.35.20 scope global venet0:0
[root@centos7 ~]#

Debian/proxmoxでのmpt-statusdによるRAIDステータスの監視

リモートでやるのが怖くて、実施できていなかったProxmoxのメジャーバージョンアップを実施した。

Debianのメジャーバージョンアップ、ということになるのだが、案の定、失敗。
(アップデートに必要なファイル群が404エラーで取得できなかった)
調査が面倒だったので、さくっと消去して新バージョンで再インストールした。

で、使用しているサーバはLSI Logic系のRAID/SCSIカードを使っているので、mpt-statusdによるRAIDステータス監視が行える。

古いバージョンのときは、以下のような/etc/default/mpt-statusdを書いていた。

# cat /etc/default/mpt-statusd
modprobe mptctl
ID="3 -n"
#

(「modprobe mptctl」を書かないと/dev/mptctlが作られず、mpt-statusがエラーになる)

通常は「ID=”3″」にするのだと思うが、これだと、syncのパーセンテージとかが表示されないという欠点がある。

root@ns5:/etc/init.d# mpt-status -i 3
ioc0 vol_id 3 type IM, 2 phy, 231 GB, state OPTIMAL, flags ENABLED
ioc0 phy 0 scsi_id 9 ATA      WDC WD2502ABYS-1 3B04, 232 GB, state ONLINE, flags NONE
ioc0 phy 1 scsi_id 4 ATA      WDC WD5000AAJS-5 3B01, 465 GB, state ONLINE, flags NONE
#

このため、syncのステータスが表示される「-n」オプション付にすることにしていた。

# mpt-status -i 3 -n
ioc:0 vol_id:3 type:IM raidlevel:RAID-1 num_disks:2 size(GB):231 state: OPTIMAL flags: ENABLED
ioc:0 phys_id:0 scsi_id:9 vendor:ATA      product_id:WDC WD2502ABYS-1 revision:3B04 size(GB):232 state: ONLINE flags: NONE sync_state: 100 ASC/ASCQ:0x11/0x00 SMART ASC/ASCQ:0xff/0xff
ioc:0 phys_id:1 scsi_id:4 vendor:ATA      product_id:WDC WD5000AAJS-5 revision:3B01 size(GB):465 state: ONLINE flags: NONE sync_state: 100 ASC/ASCQ:0xff/0xff SMART ASC/ASCQ:0xff/0xff
scsi_id:0 100%
scsi_id:1 100%
#

同期中のサンプル

# mpt-status -i 3 -n
ioc:0 vol_id:3 type:IM raidlevel:RAID-1 num_disks:2 size(GB):231 state: DEGRADED flags: ENABLED RESYNC_IN_PROGRESS
ioc:0 phys_id:0 scsi_id:9 vendor:ATA      product_id:WDC WD2502ABYS-1 revision:3B04 size(GB):232 state: ONLINE flags: NONE sync_state: 1 ASC/ASCQ:0x11/0x00 SMART ASC/ASCQ:0xff/0xff
ioc:0 phys_id:1 scsi_id:4 vendor:ATA      product_id:WDC WD5000AAJS-5 revision:3B01 size(GB):465 state: ONLINE flags: OUT_OF_SYNC sync_state: 1 ASC/ASCQ:0xff/0xff SMART ASC/ASCQ:0xff/0xff
scsi_id:0 1%
scsi_id:1 1%
#

壊れているときのサンプル

# mpt-status -i 3 -n
ioc:0 vol_id:3 type:IM raidlevel:RAID-1 num_disks:2 size(GB):231 state: DEGRADED flags: ENABLED
ioc:0 phys_id:1 scsi_id:9 vendor:ATA      product_id:WDC WD2502ABYS-1 revision:3B04 size(GB):232 state: ONLINE flags: NONE sync_state: 100 ASC/ASCQ:0x11/0x00 SMART ASC/ASCQ:0xff/0xff
ioc:0 phys_id:0 scsi_id:4 vendor: product_id: revision: size(GB):232 state: MISSING flags: OUT_OF_SYNC sync_state: 100 ASC/ASCQ:0x00/0x00 SMART ASC/ASCQ:0x00/0x00
scsi_id:1 100%
scsi_id:0 100%
#

これで問題ないだろうと思っていたのですが、ステータス変化が無くても2時間おきにメールが・・・

mpt-statusdってどういう仕組みなのか/etc/init.d/mpt-statusdの中身を確認・・・
単純に一定時間間隔でmpt-statusコマンドを実行し、その結果を比較しているだけ、と判明。

        if (mpt-status -i $ID) |grep -q 'state OPTIMAL' ; then
            BADRAID=false
        else
            BADRAID=true
            logger -t mpt-statusd "detected non-optimal RAID status"
        fi

今回、問題となっているのは上記の部分だった。
-nのときは「state: OPTIMAL」と、コロン入りのステータス表示になっているためだった。

なので、下記のように「state: OPTIMAL」を見るように変更することで解決した。

        if (mpt-status -i $ID) |grep -q 'state: OPTIMAL' ; then
            BADRAID=false
        else
            BADRAID=true
            logger -t mpt-statusd "detected non-optimal RAID status"
        fi

Windows7でGet-AzureVM | Out-GridViewが動作しない

Azure環境をCLIで操作するためのMicrosoft Azure PowerShellですが、最近のバージョンでは、Windows 7 SP1のPowerShell 3.0では正常に動作しないコマンドがあるようです。

まず、現在のPower Shellのバージョンは「$PSversiontable」で確認できます。

PS C:\> $PSversiontable

Name                           Value
----                           -----
PSVersion                      3.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34209
BuildVersion                   6.2.9200.16398
PSCompatibleVersions           {1.0, 2.0, 3.0}
PSRemotingProtocolVersion      2.2


PS C:\>

上記の「PSVersion」がPower Shellのバージョンになります。

次に、Azure Power Shellのバージョンは「(Get-Module Azure).version」で確認できます。

PS C:\> (Get-Module azure).version

Major  Minor  Build  Revision
-----  -----  -----  --------
0      9      2      -1

PS C:\>

上記の場合「0.9.2」です

この場合、「$vm = Get-AzureVM | Out-GridView -Title “Select a VM …” -PassThru」を実行すると、

PS C:\> $vm = Get-AzureVM | Out-GridView -Title "Select a VM ..." -PassThru
Get-AzureVM : パイプラインが停止されています。
発生場所 行:1 文字:7
+ $vm = Get-AzureVM | Out-GridView -Title "Select a VM ..." -PassThru
+       ~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureVM]、PipelineStoppedException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand

Out-GridView : インデックスが範囲を超えています。負でない値で、コレクションのサイズよりも小さくなければなりません。
パラメーター名:index
発生場所 行:1 文字:21
+ $vm = Get-AzureVM | Out-GridView -Title "Select a VM ..." -PassThru
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Out-GridView], ArgumentOutOfRangeException
    + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.OutGridViewCommand

PS C:\>

Out-GridViewが「ArgumentOutOfRangeException」ということで実行できません。
(「(Get-AzureVM).Name | Out-GridView -PassThue」は実行できるんだけど、得られる出力では次の処理ができない)

この現象はMicrosoftがgithubに開設しているアカウントに報告されている。
Possible breaking change Get-AzureVM breaks pipeline with Out-Gridview (#3047) #13

これによると、どうやら、Power Shell 4.0が必要らしい。

Windows Management Framework 4.0」の一部としてPower Shell 4.0が配布されているので、こちらからダウンロードしてインストールする。
リンク先でダウンロードすると、いくつかのファイルがダウンロードできるが、Windows7の64bit環境で必要なものは「Windows6.1-KB2819745-x64-MultiPkg.msu」だけである。Windows7 32bitの場合は Windows6.1-KB2819745-x86.msu」

インストール&再起動後にPower Shellの状況を確認すると、以下のようになる。

PS C:\>  $PSversiontable

Name                           Value
----                           -----
PSVersion                      4.0
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.34209
BuildVersion                   6.3.9600.16406
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion      2.2


PS C:\> (Get-Module azure).version

Major  Minor  Build  Revision
-----  -----  -----  --------
0      9      2      -1


PS C:\>

で・・改めて「$vm = Get-AzureVM | Out-GridView -Title “Select a VM …” -PassThru」を実行!

PS C:\> $vm = Get-AzureVM | Out-GridView -Title "Select a VM ..." -PassThru
Get-AzureVM : パイプラインが停止されています。発生場所 行:1 文字:7
+ $vm = Get-AzureVM | Out-GridView -Title "Select a VM ..." -PassThru
+       ~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureVM]、PipelineStoppedException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand

Out-GridView : インデックスが範囲を超えています。負でない値で、コレクションのサイズよりも小さくなければなりません。
パラメーター名:index発生場所 行:1 文字:21
+ $vm = Get-AzureVM | Out-GridView -Title "Select a VM ..." -PassThru
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Out-GridView], ArgumentOutOfRangeException
    + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.OutGridViewCommand

PS C:\>

・・・あの・・・?

というわけで、当初もくろんでいたPower Shellを4.0にすることで対処できる、というわけではなく
アップデートしても、なんともならない、という救いようのない状態に・・・・

う~ん・・・


2015/07/21追記

githubのissue」にコメントが付いた

It’s a bug in PS 3.0 and 4.0 in that it fails to load the object and/or type info, if TableControl is not correctly configured.

This will be patched on our end for 3.0/4.0 in the incoming release. Stay tune. Thanks.

PowerShell 3.0と4.0におけるバグで、今度のリリースで修正される、とのこと。