IronPython整合Windows PowerShell

简介:
  Windows PowerShell 是微软为 Windows 环境所开发的 shell 及脚本语言技术,这项全新的技术提供了丰富的控制与自动化的系统管理能力;关于PowerShell参看 易学易用的Windows PowerShell  。IronPython也是脚本语言,两种脚本语言的联姻可以解决Windows 系统管理的任务,是系统管理员的必备工具。这里有一篇文章在提醒DBA们要开始学PowerShell   RTFM : [url]http://weblog.infoworld.com/dbunderground/archives/2007/01/rtfm.html[/url]
     PowerShell托管的API为IronPython整合PowerShell提供了条件。可以在IronPython直接访问PowerShell,直接利用PowerShell的强大Shell功能。下面的例子演示了IronPython中如何使用PowerShell。
D:\IronPython\Samples\IPPowerShell>ipy -i powershell.py
Run 'dir(shell)' to get a list of available PowerShell commands!
In general, IronPython PowerShell commands are accessed using the form:
shell.get_process("cmd").select(First=2)
>>> shell.dir()
目录: Microsoft.PowerShell.Core\FileSystem::D:\IronPython\Samples\IPPowerShel
l
Mode LastWriteTime Length Name
---- ------------- ------ ----
-ar-- 2006-9-14 14:46 3583 minsysreq.py
-ar-- 2006-9-14 14:44 3136 minsysreq_ps.py
-ar-- 2006-9-11 12:21 6261 powershell.py
-ar-- 2006-9-14 14:51 12911 readme.htm
>>> shell.dir().sort("LastWriteTime",Descending=True).select_object("Name")
Name
----
readme.htm
minsysreq.py
minsysreq_ps.py
powershell.py
>>>
例子代码可以从这里获取: IronPython Sample ,有一个IPPowerShell例子,下载解压后有一个IronPython script文件powershell.py文件。可以在通过在你的IronPython脚本文件中通过使用这个powershell模块来使用PowerShell,当然你要安装PowerShell。
下面我们来一起学习一下powershell.py文件:
# **********************************************************************************
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# This source code is subject to terms and conditions of the Shared Source License
# for IronPython. A copy of the license can be found in the License.html file
# at the root of this distribution. If you can not locate the Shared Source License
# for IronPython, please send an email to [email]ironpy@microsoft.com[/email].
# By using this source code in any fashion, you are agreeing to be bound by
# the terms of the Shared Source License for IronPython.
#
# You must not remove this notice, or any other, from this software.
#
# **********************************************************************************
import clr
clr.AddReference('System.Management.Automation')
clr.AddReference('IronPython')
from System.Management.Automation import *
from System.Management.Automation.Host import *
from System.Management.Automation.Runspaces import *
import System
#Create a new runspace to execute powershell commands within
_runspace = RunspaceFactory.CreateRunspace()
_runspace.Open()
_intrinsics = _runspace.SessionStateProxy.GetVariable("ExecutionContext")
def translate(name):
'''
Utility function converts a string, name, to lowercase.
Also replaces hyphens with underscores.
'''
name = name.lower()
return name.replace('-', '_')
def fix_arg(arg):
'''
Utility function converts arg (of type string, PSObjectWrapper, or
ShellOutput) to type string.
'''
if isinstance(arg, str):
arg = _intrinsics.InvokeCommand.ExpandString(arg)
elif isinstance(arg, PSObjectWrapper):
arg = arg.data
elif isinstance(arg, ShellOutput):
arg = arg.data
return arg
def InvokeCommand(_cmdName, _input=None, *args, **kws):
'''
Used to actually invoke a powershell command.
'''
#print 'invoke', _cmdName, _input
cmd = Command(_cmdName)
#Adds parameters to the command
for arg in args:
cmd.Parameters.Add(CommandParameter(None, fix_arg(arg)))
for name, value in kws.items():
cmd.Parameters.Add(CommandParameter(name, fix_arg(value)))
#Create a pipeline to run the command within and invoke
#the command.
pipeline = _runspace.CreatePipeline()
pipeline.Commands.Add(cmd)
if _input:
ret = pipeline.Invoke(fix_arg(_input))
else:
ret = pipeline.Invoke()
#return the output of the command formatted special
#using the ShellOutput class
return ShellOutput(ret)
class Shell:
'''
Instances of this class are like pseudo PowerShell
shells. That is, this class essentially has a method for
each PowerShell command available.
'''
def __init__(self, data):
'''
Standard constructor. Just copies a dictionary mapping
PowerShell commands to names as members of this class.
'''
for key, value in data.items():
setattr(self, key, value)
class ShellCommand(object):
'''
Wrapper class for shell commands.
'''
def __init__(self, name, input=None):
'''
'''
self.name = name
self.input = input
def __call__(self, *args, **kws):
'''
''' 
return InvokeCommand(self.name, self.input, *args, **kws)
def __get__(self, instance, context=None):
'''
'''
return ShellCommand(self.name, instance)
def __repr__(self):
'''
'''
return "<ShellCommand %s>" % self.name
class ShellOutput(object):
'''
'''
def __init__(self, data):
'''
'''
self.data = data
def __len__(self):
'''
'''
return self.data.Count
def __repr__(self):
'''
'''
if self.data.Count == 0: return ''
return str(self.out_string(Width=System.Console.BufferWidth-1)[0]).strip()
def __getitem__(self, index):
'''
'''
if index >= self.data.Count: raise IndexError
ret = self.data[index]
if isinstance(ret, PSObject):
return PSObjectWrapper(ret)
class PSObjectWrapper(object):
'''
'''
def __init__(self, data):
'''
'''
self.data = data
def __getattr__(self, name):
'''
'''
member = self.data.Members[name]
if member is not None:
ret = member.Value
if isinstance(ret, PSMethod):
ret = InvokeToCallableAdapter(ret)
return ret
raise AttributeError(name)
def __repr__(self):
'''
'''
return self.data.Members['ToString'].Invoke()
def dump(o):
'''
'''
print str(o.out_string(Width=System.Console.BufferWidth-1)[0]).strip()
class InvokeToCallableAdapter:
'''
'''
def __init__(self, meth):
'''
'''
self.meth = meth
def __call__(self, *args):
'''
'''
return self.meth.Invoke(*args)
def init_runspace():
'''
'''
global shell
#build up a dictionary of native PowerShell commands where 
#each value consists of the PS command wrapped within 
#the ShellCommand helper class
cmds = {}
for cmdlet in InvokeCommand('get-command'):
cmds[translate(cmdlet.Name)] = ShellCommand(cmdlet.Name)
#look for all aliases and for each of them that map directly
#into a native PowerShell command, support them directly
#from the dictionary
for alias in InvokeCommand('get-alias'):
cmdName = translate(alias.ReferencedCommand.Name)
if cmdName in cmds:
cmds[translate(alias.Name)] = cmds[cmdName]
shell = Shell(cmds)
ShellOutput.__dict__.update(cmds)
init_runspace()
if __name__ == '__main__':
print """Run \'dir(shell)\' to get a list of available PowerShell commands!
In general, IronPython PowerShell commands are accessed using the form:
shell.get_process("cmd").select(First=2)
"""
PowerShell里面有几个对象: RunSpacePSMethodPSObject等,具体可以参看PowerShell SDK。这个PowerShell模块对PowerShell对象进行了一个包装成对象shell,这个对象包装了PS的cmdlets 和aliases。





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/74384,如需转载请自行联系原作者
目录
相关文章
|
5月前
|
Unix Shell Windows
Windows PowerShell技巧:使用findstr实现类似grep的功能
显示带有线路编号**: `/N`选项将显示每条结果前面带有其在线路上出现位置编号。
943 7
|
5月前
|
存储 Windows
Windows PowerShell操作:如何删除环境变量
此外,还有一些第三方工具可以用来管理环境变量,这些工具通常提供了更为用户友好的界面来添加、编辑或删除环境变量,但是使用PowerShell可以更直接地控制这些设置,并且可以很容易地集成到脚本中以自动化环境配置的管理。
779 7
|
Windows
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
525 10
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
676 0
|
安全 Shell Linux
内网渗透测试基础——Windows PowerShell篇
内网渗透测试基础——Windows PowerShell篇
727 0
|
Windows
【vscode】 VsCode终端崩溃C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe已终止,退出代码:2
【vscode】 VsCode终端崩溃C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe已终止,退出代码:2
3767 1
|
存储 Ubuntu Linux
windows可以安装Ubuntu,ubuntu上也可以安装Powershell
powerhsell除了可以在windows上使用外,还可以在Ubuntu上部署开发环境。下面介绍Ubuntu上安装powershell的方法。
730 0
|
Shell Linux 开发工具
windows中cmd和PowerShell批处理命令
之前在 Git 批量删除本地分支,有用到 Linux 或 MacOS 下的批处理命令,这个命令中的 grep、xargs 本身是 Shell script,在 windows 中的 cmd 和 PowerShell 中是不能用的
|
JavaScript Windows
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
[Vue]解决 Windows PowerShell 不识别 vue 命令的问题
|
Windows
使用PowerShell获取Windows当前锁屏壁纸
使用PowerShell获取Windows当前锁屏壁纸 如果原始图片丢了,用这段代码就可以提取当前锁屏壁纸了!
370 0

热门文章

最新文章