Created
February 26, 2010 10:40
-
-
Save velaskec/315620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################################# | |
# PowerSlim 0.1 | |
############################################################################################################# | |
import os,re | |
import clr,os | |
clr.AddReference("System.Management") | |
clr.AddReference("System.Management.Automation") | |
from System.Management.Automation import * | |
from System.Management.Automation.Host import * | |
from System.Management.Automation.Runspaces import * | |
from System.Management.Automation.Runspaces import RunspaceFactory | |
class PowerShellWrapper: | |
def __init__(self,alias = lambda x:x): | |
self.runspace = RunspaceFactory.CreateRunspace() | |
self.runspace.Open() | |
self.ri = RunspaceInvoke(self.runspace) | |
self.output = [] | |
self._alias = alias | |
def include(self,files=[]): | |
for fl in files: | |
self.cmd( open(fl).read() ) | |
return self | |
# run q and fill output | |
def cmd(self,q): | |
self.output = self.ri.Invoke( self._alias(q) ) | |
return self | |
# try convert output to string | |
def toStr(self): | |
return "".join([ str(i) for i in self.output ]) | |
def out(self): | |
return [ [[k,getattr(i.ImmediateBaseObject,k)] for k in dir(i.ImmediateBaseObject) if i.ImmediateBaseObject.GetType().GetProperty(k) and hasattr(i.ImmediateBaseObject,k)]+ | |
[[k.Name,i.ImmediateBaseObject.GetAttribute(k.Name)] for k in i.ImmediateBaseObject.Attributes if hasattr(i.ImmediateBaseObject,"Attributes")] for i in self.output ] | |
############################################################################################################# | |
powershell = PowerShellWrapper(alias=lambda x:x.replace('^','$')) | |
class PowerShell(object): | |
def __init__(self, query): | |
self.out = powershell.cmd(query).out() | |
def eval(self,query): | |
return powershell.cmd(query).toStr() | |
def query(self): | |
return self.out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment