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());
}
}