Puede cortar, pegar y editar los ejemplos de JavaScript a fin de escribir scripts para tareas de PowerShell comunes.
Para obtener más información sobre la creación de scripts, consulte vRealize Orchestrator Developer's Guide.
Ejecutar un script de PowerShell mediante la API
Puede utilizar JavaScript para ejecutar un script de PowerShell a través de la API del complemento.
Este ejemplo de script realiza las acciones siguientes:
- Abre una sesión en un host de PowerShell.
- Proporciona un script para ejecutar.
- Comprueba los resultados de la invocación.
- Cierra la sesión.
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() );
}
}
Utilizar el resultado
Puede usar JavaScript para trabajar con el resultado de una ejecución de un script de PowerShell.
Este ejemplo de script realiza las acciones siguientes:
- Comprueba el estado de la invocación.
- Extrae un valor del resultado.
- Comprueba el tipo 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());
Conectar con credenciales personalizadas
Puede usar JavaScript para conectarse a un host de PowerShell con credenciales personalizadas.
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());
}
}