Skip to content

VMware SDK 4.0 付属サンプルを Visual Studio 2010 でビルドする

VMware vSphere Web Services SDK 4.0 を使うと、VMware の操作を作り込み、自動化することが出来ます。VMware vSphere Web Services SDK 4.0(以下、SDK)付属のサンプルを Visual Studio 2010 でビルドしてみたので、記録を残しておきます。ちなみに、Windows 環境下からの自動化が目的であれば、Automation Tools にある PowerShell を使った CLI の方が、自動化は容易かも知れません。

展開する

ダウンロードした SDK は圧縮されていますので、適当なディレクトリに展開しておきます。今回は C:\SDK に展開しました。

コマンドプロンプトを開く

スタートメニューから「Microsoft Visual Studio 2010 → Visual Studio Tools → Visual Studio コマンドプロンプト」をクリックし、Visual Studio 関連の環境変数が定義されたコマンドプロンプトを起動します。

環境変数の定義

Visual Studio 2010 用に、コマンドプロンプトから以下の環境変数を定義します。

1
set VSINSTALLDIR="C:\Program Files\Microsoft Visual Studio 10.0"

WSDL をコピーする

WSDL を用意します。コマンドプロンプトから以下を実行します。

1
2
3
C:
cd C:\SDK
copy wsdl\vim25\* samples\DotNet

作業用スクリプトを修正する

SDK の samples\DotNet ディレクトリには作業用のスクリプト「genvimstubs.cmd」が付属していますが、これは Visual Studio 2005 をターゲットにしたもののようです。これを以下のように修正し、Visual Studio 2010 に対応するよう、変更します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$ diff genvimstubs.cmd genvimstubs_new.cmd 
6,7c6,7
< if not exist %VSINSTALLDIR%\SDK\v2.0\bin\wsdl.exe goto err_no_VSTOOLS
< if not exist %WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc.exe goto err_no_VSTOOLS



-


> @rem if not exist %VSINSTALLDIR%\SDK\v2.0\bin\wsdl.exe goto err_no_VSTOOLS
> @rem if not exist %WINDIR%\Microsoft.NET\Framework\v2.0.50727\csc.exe goto err_no_VSTOOLS
95c95
< wsdl.exe /l:CS /n:%_NAMESPACE% /out:%_STUBDIR%\%_STUBFILENAME% %_WSDLDIR%\vim.wsdl %_WSDLDIR%\vimService.wsdl



-


> "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\wsdl.exe" /l:CS /n:%_NAMESPACE% /out:%_STUBDIR%\%_STUBFILENAME% %_WSDLDIR%\vim.wsdl %_WSDLDIR%\vimService.wsdl
99c99
< csc.exe /t:library /out:%_DLLDIR%\%_DLLNAME%.dll %_STUBDIR%\%_STUBFILENAME%



-


> C:\WINDOWS\Microsoft.NET\Framework\v3.5\csc.exe /t:library /out:%_DLLDIR%\%_DLLNAME%.dll %_STUBDIR%\%_STUBFILENAME%
103c103
< sgen.exe /p /out:%_DLLDIR% %_DLLDIR%\%_DLLNAME%.dll



-


> "C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\sgen.exe" /p /out:%_DLLDIR% %_DLLDIR%\%_DLLNAME%.dll

修正が完了したら、作業用スクリプトを実行します。

1
2
cd samples\DotNet
genvimstubs_new.cmd .\

ソリューションを Visual Studio 2010 で開く

ここまで準備が完了したら、SDK 付属のサンプルを Visual Studio 2010 で開きます。具体的には、エクスプローラ上からソリューションファイル「samples\DotNet\cs\Samples2005.sln」を開きます。ただし、このソリューションファイルは Visual Studio 2005 用なので、変換ウィザードで Visual Studio 2010 対応に変換しておきます。

サンプルソースコードの修正

しかし、このままビルドすると名前空間の不足から、大量のエラーが発生し、サンプルをビルド出来ません。Visual Studio 2010 の置換機能を使って、「Vim25Api」という文字列を「VimApi」に全て置換します。

ビルドする

これで SDK 付属のサンプルを Visual Studio 2010 でビルド出来るようになりました。通常通り、ビルドします。ただし、SDK が 4.0 にバージョンアップされた際に API のインターフェイスが変更された にもかかわらず、SDK 付属のサンプルは修正が行われていないようで、幾つかのエラーが残ります。

サンプルを実行してみる

試しに「Connect」というサンプルを実行してみます。「Connect」は「samples\DotNet\cs\Connect\bin\Debug」というディレクトリに実行ファイルが置かれているはずです(もちろん、Release ビルドの場合はディレクトリ位置を読み替えます)。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
> Connect.exe --url https://10.0.1.147/sdk --username root --password password
[ 2010/02/16 0:11:41 ] Begin Log.
Started
There were one or more problems with the server certificate:

* A certification chain processed correctly, but terminated in a root certificat
e which isn't trusted by the trust provider.

* The certificate's CN name does not match the passed value.


Connected Successfully
Server Time -: 2010/02/16 0:11:47
Press enter to exit.