JavaScript サンプルを切り取り、貼り付け、および編集することで、一般的な PowerShell タスクのスクリプトを記述できます。
スクリプティングの詳細については、『vRealize Orchestrator 開発者ガイド』を参照してください。
API 経由での PowerShell スクリプトの実行
JavaScript を使用して、プラグイン API 経由で PowerShell スクリプトを実行できます。
このサンプル スクリプトは、次のアクションを実行します。
- PowerShell ホストのセッションを開きます。
- 実行するスクリプトを指定します。
- 呼び出し結果を確認します。
- セッションを閉じます。
var sess;
try {
//Open session to PowerShell host
var sess = host.openSession()
//Set executed script
var result = sess.invokeScript('dir')
//Check for errors
if (result.invocationState == 'Failed'){
throw "PowerShellInvocationError: Errors found while executing script \n" + result.getErrors();
}
//Show result
System.log( result.getHostOutput() );
} catch (ex){
System.error (ex)
} finally {
if (sess) {
//Close session
host.closeSession( sess.getSessionId() );
}
}
結果の使用
JavaScript を使用して、PowerShell スクリプトの実行結果を処理することができます。
このサンプル スクリプトは、次のアクションを実行します。
- 呼び出し状態を確認します。
- 結果から値を抽出します。
- RemotePSObject タイプを確認します。
var sess = host.openSession()
sess.addCommandFromString("dir " + directory)
var invResult = sess.invokePipeline();
//Show result
System.log( invResult.getHostOutput() );
//Check for errors
if (invResult.invocationState == 'Failed'){
System.error(invResult.getErrors());
} else {
//Get PowerShellRemotePSObject
var psObject = invResult.getResults();
var directories = psObject.getRootObject();
var isList = directories instanceof Array
if ( isList ){
for (idx in directories){
var item = directories[idx];
if ( item.instanceOf('System.IO.FileInfo') ){//Check type of object
System.log( item.getProperty('FullName') );//Extract value from result
}
}
} else {
System.log( directories.getProperty('FullName') );//Extract value from result
}
}
host.closeSession( sess.getSessionId());
カスタムの資格情報と連携する
JavaScript を使用して、PowerShell ホストをカスタム認証情報と連携させることができます。
var sess;
try {
sess = host.openSessionAs(userName, password);
var invResult = sess.invokeScript('$env:username');
//Check for errors
if (invResult.invocationState == 'Failed'){
System.error(invResult.getErrors());
} else {
//Show result
System.log( invResult.getHostOutput() );
}
} catch (ex){
System.error (ex)
} finally {
if (sess) {
host.closeSession( sess.getSessionId());
}
}