def create_filter(**kwargs): conditions = ['1'] for key in kwargs: if key.startswith('!'): continue conditions.append('$_.%s -eq %s' % (key, escape(str(kwargs[key])))) filter = '? { %s }' % ' -and '.join(conditions) return filter
def create_setattr(obj, **kwargs): statements = [] for key in kwargs: if key.startswith('!'): continue value = kwargs[key] statements.append('$%s.%s = %s' % (obj, key, escape(str(value)))) statement = ';'.join(statements) + ';' return statement
def create_cmdline(**kwargs): arguments = [] for key in kwargs: value = kwargs[key] if key.startswith('!'): continue elif value is None: pass elif value is True: arguments.append('-%s' % key) elif key.endswith('Object'): # XXX: ugly hack: arguments.append('-%s %s' % (key, value)) else: arguments.append('-%s %s' % (key, escape(str(value)))) cmdline = ' '.join(arguments) return cmdline
def list(self, **filter): query = filter.pop('query', 'vms:') filter = create_filter(**filter) result = powershell.execute('Select-Vm -SearchText %s | %s' % (escape(query), filter)) return result