Thursday, July 19, 2007

HTA - Python : Gestionnaire de Tâches


<html>
<head>
<title>Python Process Lister</title>
<HTA:APPLICATION
ID="processlister"
APPLICATIONNAME="Python Process Lister"
SCROLL="auto"
SCROLLFLAT = "yes"
SINGLEINSTANCE="yes"
>

<style type="text/css">

body {
background-color: #ababcd;
font-family: Verdana
font-size: 0.9 em;
color: #fff;
}

a:link {text-decoration: none; color:#343456}
a:visited {text-decoration: none;color:#343456}
a:active {text-decoration: none; color:#343456}
a:hover {text-decoration: underline; color:452200;}

</style>
</head>


<body onLoad="Initialisation()">
<center><h2>Liste des Process</h2></center>
<span id = "ProcessList"></span>


<!-- Script Python -->
<SCRIPT LANGUAGE="Python">
from win32com.client import GetObject
import win32gui

doc = document.All

ChaineConnexion = r"WinMgMts:\\%s\%s"

listProcess = []

process = None

def displayProc(args):
proc = None
numproc = int(args)
rep = window.confirm("Voulez-vous terminer le processus %s ?"%args )
if rep:
for p in process:
proc = p
if int(p.Handle) == numproc:
alert(u"Le processus %d va être supprimé"%numproc)
break
proc.Terminate

def getAllObjects(serveur = ".", espaceDenom = "root\cimv2", classe = "Win32_Process"):
c = GetObject(ChaineConnexion%(serveur,espaceDenom))
objects = c.ExecQuery(r"select * from %s"%classe)
return objects

def Initialisation():
win32gui.MessageBox(0, " Hello From Python", "Message d'Invite", 0)
RefreshList()
iTimerID = window.setInterval("RefreshList()", 10000)

def RefreshList():
global process

process = getAllObjects()

result = "<table>"
for processus in process:
result += """<tr><td align="right">%4d</td>
<td style="padding-left:15px;"><a class="maya" href="#" onClick="displayProc(%d)">%s</a></div></td>
<td>%s</td>
</tr>"""%(int(processus.Handle),int(processus.Handle),processus.Name,processus.CommandLine)

result += "</table>"

doc.ProcessList.InnerHTML = result

</SCRIPT>

</body>
</html>

Sunday, July 15, 2007

HTA : Arguments en ligne de commande


<HTML>

<HEAD>

<TITLE>HTA Demo</TITLE>
<HTA:APPLICATION ID="oHTA"
APPLICATIONNAME="myApp"
BORDER="thin"
BORDERSTYLE="normal"
CAPTION="yes"
ICON=""
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="no"
SINGLEINSTANCE="no"
SYSMENU="yes"
VERSION="1.0"
WINDOWSTATE="maximize"/>

<SCRIPT language="Python">
d = document.all
lienMSN = "http://msdn2.microsoft.com/en-us/library/ms536495.aspx"
def window_onload():
sTempStr = "applicationName = " + d.oHTA.applicationName + "<br />"
sTempStr += "commandLineName = " + d.oHTA.commandLine + "<br />"
d.oPre.innerHTML = '<a href="%s">%s</a>'%(lienMSN,lienMSN) + "<br />" + sTempStr

</SCRIPT>

</HEAD>

<BODY SCROLL="no">

<p>Les arguments doivent suivre le nom du script et être entourés de guillemets</p>
<PRE ID=oPre></PRE>

</BODY>

</HTML>

Python et WMI

WMI (Windows Managment Instrumentation) est un ensemble de classes permettant d'auditer des serveurs Windows.
L'accés à ces classe est très aisée en Python comme le montre cet exemple:


from win32com.client import GetObject

ChaineConnexion = r"WinMgMts:\\%s\%s"

def getAllObjects(serveur = ".", espaceDenom = "root\cimv2", classe = "Win32_Process"):
c = GetObject(ChaineConnexion%(serveur,espaceDenom))
objects = c.ExecQuery(r"select * from %s"%classe)
return objects

def test():
allObj = getAllObjects()
for proc in allObj:
print proc.Name

if __name__ == "__main__":
test()