Voici quelques exemples de l”utilisation de Windows Scripting.
Ces exemples requiert=
que Windows Scripting version 5.6 so=
it
installé. Vous pouvez vous le procurer : http://msdn.microsoft.com/library/defaul=
t.asp?url=3D/downloads/list/webdev.asp.
Comment obtenir l’information
de base d’un ordinateur.=
p>
CODE
WshNetwork =3D <=
span
class=3DSpellE>CreateObject('WScript.Network')
lcMessage=
=3D'Domain =3D ' + WshNetwork.UserDomain
+ CHR(13)
lcMessage=
=3DlcMessage+ 'Comp=
uter Name =3D' + WshNetwork.ComputerName+CHR(13)
lcMessage=
=3DlcMessage+ 'User=
Name =3D ' + WshNetwork.UserName
MESSAGEBOX(lcMessage)=
p>
Comment obtenir l’information sur votre CD-ROM(s)=
p>
CODE<=
span
style=3D'color:black'>
LOCAL strComputer<=
/span>
Local lcString
strComputer =3D '.'
lcString =
=3D ''
objWMIService =3D Getobject('winmgmts:'+ 'impersonationLevel=3Dimpersonate}!\' =
+ strComputer + 'rootcimv=
2')
colItems =
=3D objWMIService.ExecQuery=
('Select
* from Win32_CDROMDrive')
For Each objItem<=
/span>
In colItems
=
lcString
=3D lcString +
'Description: '+objItem.Descrip=
tion+Chr(13)
=
lcString
=3D lcString + 'N=
ame: '+objItem.Name+Chr(13)
=
lcString
=3D lcString +
'Manufacturer:' +objItem.manufa=
cturer+Chr(13)
=
lcString
=3D lcString + 'M=
edia type:
'+objItem.mediaType+Chr<=
/span>(13)
=
lcString
=3D lcString + 'P=
NP Device
ID:' + objItem.PNPDeviceID
+Chr(13)
Next
Messagebox=
(lcString)
Comment connecter un drive de reseau.
CODE<=
span
style=3D'color:black'>
oNet =3D &nb=
sp; CreateObject('WScript.Network') =
oNet.MapNetworkDrive('I','myservermyFiles’,.T.,'mike'=
,'password')
Comment déconnecter un drive de reseau.
CODE<=
span
style=3D'color:black'>
WshNetwo=
rk =3D &nb=
sp; CreateObject('WScript.Network')
WshNetwork.RemoveNetwork=
Drive('E')
Comment ajouter une c=
onnection
d’imprimante.
CODE<=
span
style=3D'color:black'>
oNet =3D &nb=
sp; createobject('WScript.Network')
oNet.AddWindowsPrinterCo=
nnection('ServerNamePrinterName')=
Comment forcer une imprimante Windows par =
defaut,
CODE<=
span
style=3D'color:black'>
oNet =3D &nb=
sp; CreateObject('WScript.Network') =
oNet.SetDefaultPrinter=
span>('ServerNamePrinterName')=
Comment verifier
l’espace vide restante sur un disque dur.=
p>
CODE
objFSO =3D <=
span
class=3DSpellE>CreateObject('Scripting.FileSystemObject')
objDrive =
=3D objFSO.GetDrive('C:')
MESSAGEBOX('Available space:
' + chr(13)+TRANS=
FORM(objDrive.AvailableSpace,'999,999,999,999,999'+' &nb=
sp; kb' ))
Comment copier une filière.
CODE
FSO =3D CreateObject('Scripting.FileSystemObject')
FSO.CopyFile('c:COMPlusLog.txt','c:x\')
Comment créer un dossier.=
p>
CODE
fso =3D createobject('Scripting.FileSystemObject')
fldr =3D <=
span
class=3DSpellE>fso.CreateFolder('C:MyTest')
Comment supprimer un dossier.=
span>=
p>
CODE<=
span
style=3D'color:black'>
fso =3Dcreateobject('Scripting.FileSystemObject')
fldr =3D <=
span
class=3DSpellE>fso.DeleteFolder('C:MyTest')
Comment determiner
si un dossier existe.
CODE<=
span
style=3D'color:black'>
fso =3Dcreateobject('Scripting.FileSystemObject')
? fso.FolderExists('C:MyTest')
Comment créer une
filière.
CODE
fso =3D CreateObject('Scripting.FileSystemObject')
f1 =3D fso.CreateTextFil=
e('c:testfile.txt',
.T.)
Comment créer un=
filière et écrire du texte.
CODE<=
span
style=3D'color:black'>
fso =3D CreateObject('Scripting.FileSystemObject')
tf =3D fso.CreateTextFile('c:t=
estfile.txt',
.t.)
tf.WriteLine('Testing
1, 2, 3.')
tf.WriteBlankLines(3)
&& Skip three lines
tf.Write (=
'This is
a test.')
tf.Close
MODIFY FILE 'c:testfile.txt'
Comment créer un icone
sur le bureau (avec chemin)
CODE
oShell =3D <=
span
class=3DSpellE>CreateObject('WScript.Shell')
DesktopPath =3D oShell.SpecialFolders('=
Desktop')
oURL =3D <=
span
class=3DSpellE>oShell.CreateShortcut(<=
span
class=3DSpellE>DesktopPath + 'MSDN Scripting.URL')
oURL =3D <=
span
class=3DSpellE>oShell.CreateShortcut(<=
span
class=3DSpellE>DesktopPath + 'MSDN Scripting.URL')
oURL.TargetPath=
span> =3D
'HTTP://MSDN.Microsoft.com/scripting/'
oURL.Save<=
/span>=
p>
Comment écrire une entrée dans le régistre de Windows.=
p>
CODE
oSh =3D CreateObject('WScript.S=
hell')
key =3D 'HKEY_CURRENT_USER\=
'
oSh.RegWrite( key + 'WS=
HTest\','testkeydefault')
oSh.RegWrite(key + 'WSHTeststring1', 'testkeystring1')
oSh.RegWrite( key + 'WSHTeststring2', 'testkeystring2',
'REG_SZ')
oSh.RegWrite( key + 'WSHTeststring3', 'testkeystring3',
'REG_EXPAND_SZ')
oSh.RegWrite( key + 'WS=
HTestint',
123, 'REG_DWORD')
Comment supprimer une entrée dans le régistre de Windows.=
p>
CODE
oSh =3D CreateObject('WScript.Shell')
oSh.RegDelete ('HKCUSoftwareACMEFortuneTellerMindRead=
er')
oSh.RegDelete('HKCUSoftwareACMEFortuneTeller<=
/span>\')
oSh.RegDelete ('HKCUSoftwareACME\')
Une rempl=
acement
du messagebox &nb=
sp; de
FoxPro.
CODE<=
span
style=3D'color:black'>
WshShell=
=3D &nb=
sp; Createobject('WScript.Shell')
BtnCode =
=3D WshShell.Popup('Do &nbs=
p; you
feel alright?', 7, 'Answer This Question:', 4 + 32)
Do Case
Case BtnCode=3D6
=
WSHSHELL.Popup('Glad
to hear you feel alright.')
Case BtnCode=3D7
<=
span
class=3Dgrame>WSHSHELL.Popup('Hope you're feeling better soon.')
Endcase
Comment créer un racourci sur
le bureau.
CODE<=
span
style=3D'color:black'>
Shell =3D CreateOb=
ject(‘WScript.Shell’)
DesktopPath =3D Shell.SpecialFolders(=
8216;Desktop’)
link =3D Shell.CreateSho=
rtcut(DesktopPath+’test.lnk’)
link.Arguments =3D
‘1 2 3’
link.Description<=
/span> =3D
‘test shortcut’
link.HotKey =3D
‘CTRL+ALT+SHIFT+’
link.IconLocation=
=3D ‘app.exe,1’
link.TargetPath=
span> =3D
‘c:blahapp.exe’=
link.Win=
dowStyle =3D &nb=
sp; 3
link.WorkingDirectory
=3D ‘c:blah’
link.Save(=
)
Uliser
le fileOpen  =
; de
Windows qui permet des selectio=
ns
multiples (Windows XP)
CODE<=
span
style=3D'color:black'>
oDlg=3D Createobject(‘userAccounts.commonDialog’)
oDlg.flags=
=3D
‘&h1a04’
oDlg.Filter=3D
‘All Files|*.*|’+ ‘Text and Batch
Files|*.txt;*.bat’
oDlg.filterIndex<=
/span>=3D
2
oDlg.initialDir=
span>=3D’C:’
qSln=3D oDlg.showOpen
If qSln
=
Messagebox(oDlg.fileName)
Else
=
Messagebox(‘Dialog
cancelled.’)
Endif
Demonstration=
comment converter=
span>
les valeurs UTC (Universal
Time coordinate)utilsées
par WMI en valeurs de temps standard.
CODE<=
span
style=3D'color:black'>
strCompu=
ter =3D &nb=
sp;
‘.’
objWMIService =3D Getobject(‘winmgmts:’ + strComputer +
‘rootcimv2’)
objOS =3D =
objWMIService.ExecQuery=
(‘Select
* from Win32_OperatingSystem’)
For Each strOS In objOS
=
dtmInstallDate
=3D strOS.InstallDate
MESSAGEBOX(TRANSFORM(=
WMIDateStringToDate(dtmInstallDate)))
Next
Function WMIDateStringTo=
Date(dtmInstallDate)
PRIVATE ldRetVal<=
/span>
RETURN CTOT(SUBSTR(dtmIn=
stallDate,
5, 2) + ‘/’ + SUBSTR(dtmInstallDate, 7, 2) + ‘/’ + Le=
ft(dtmInstallDate, 4) + =
8216;
‘ +;
SUBSTR(dtmInstallD=
ate,
9, 2) + ‘:’ + SUBSTR(dtmInstallDate,
11, 2) + ‘:’ + SUBSTR(dtmInstallDate,13, 2))
ENDFUNC
Obtenir l’heure et la d=
ate
locales incluant la journée de la semaine, le
quart et la semaine du mois.
CODE<=
span
style=3D'color:black'>
strCompu=
ter =3D &nb=
sp;
‘.’
lcStr=3D''=
objWMIService =3D Getobject(‘winmgmts:’ + strComputer +
‘rootcimv2’)
colItems =
=3D objWMIService.ExecQuery=
(‘Select
* from Win32_LocalTime’)
Set Step On
For Each objItem<=
/span>
In colItems
=
lcStr=3DlcStr+’Year: &nbs=
p;
‘ + Transform(objItem.Yea=
r)+Chr(13)
=
lcStr=3DlcStr+’Month: &nb=
sp;
‘ + Transform(objItem.Mon=
th)+Chr(13)
=
lcStr=3DlcStr+’Day:
‘ + Transform(objItem.Day=
)+Chr(13)
=
lcStr=3DlcStr+’Hour: &nbs=
p;
‘ + Transform(objItem.Hou=
r)+Chr(13)
=
lcStr=3DlcStr+’Minute: &n=
bsp;
‘ + Transform(objItem.Min=
ute)+Chr(13)
=
lcStr=3DlcStr+’Second: &n=
bsp;
‘ +Transform( objItem.Sec=
ond)+Chr(13)
=
lcStr=3DlcStr+’Day =
Of the
Week: ‘ + Transform(objIt=
em.Dayofweek)+Chr(13)
=
lcStr=3DlcStr+ ‘We=
ek In
the Month: ‘ +Transf(
objItem.WeekInMonth)+Chr(13)
=
lcStr=3DlcStr+’Quarter: &=
nbsp;
‘ +Transform( objItem.Qua=
rter)
Next
Messagebox=
(lcStr)
Démarrer un tache cédulée,
plutôt que d’attendre la cédule.=
p>
CODE<=
span
style=3D'color:black'>
ssfCONTR=
OLS =3D &nb=
sp;
3 && Control Panel's Schedule Tasks folder
sJobName =
=3D ‘fta’ &=
amp;&
Name of the task to run
sRunVerb =
=3D ‘R&un’ &n=
bsp;&&
Executing command
sEndVerb =
=3D
‘&End Task’ && Cancelling command
shellApp =
=3D Createobject(‘shell.application’=
;)
oControlPanel =3D shellApp.Namespace(ssfCONTROLS) &&
Schedule Tasks folder
oST =3D ''=
For Each folderitem
In oControlPanel.items=
span> &&
Loop though the items in the Control Panel items
If folderitem.Name =3D
‘Scheduled Tasks’
oST =3D folderitem.getfolder() && Found it
Exit
=
Endif
Next
If Vartype=
(oST) !=3D 'O'
=
Messagebox(‘Couldn't
find 'TS' folder’)
Endif
oJob =3D '=
'
For Each folderitem
In oST.items =
; &&
Loop through the different scheduled tasks until we fiind it.
If Lo=
wer(folderitem.Name) &=
nbsp;=3D
Lower(sJobName) &=
nbsp;
oJob =3D folderitem && Found it
Exit
=
Endif
Next
If Vartype=
(oJob) !=3D‘O̵=
7;
=
Messagebox(
‘Couldn't find ‘ + sJobName
+ ‘ item’)
Else
=
bEnabled
=3D .T.
=
oRunVerb
=3D ''
=
oEndVerb
=3D ''
s =3D ‘Verbs: ‘ + Chr(13)
For Each Verb In oJob.verbs && Loop
through the different commands in the scheduled task until we find right
one.
s =3D s + Chr<=
/span>(13) + Verb.Name
If Verb.Name =3D sRunVerb
<=
span
class=3DSpellE>oRunVerb =3D Verb
<=
span
class=3DSpellE>bEnabled =3D .F.
Endif
If Verb.Name =3D sEndVerb
<=
span
class=3DSpellE>oEndVerb =3D Verb
Endif
Next
If bEnabled
oJob.InvokeVerb(oEndVerb) && Cancel the t=
ask
Else
Wait Window Nowait ‘executing
job’
oJob.InvokeVerb(sRunVerb) && Run the task=
!
Endif
Endif=
p>
Fonction qui énumère le procress qui roulent.=
p>
CODE
FUNCTION enumerateProcess
lcComputer=
=3D
‘.’
loWMISer=
vice =3D Getobject(‘winmgmts:’ ;
+ ‘{impersonationLevel=3Dimpersonate}!’ &n=
bsp; + lcComputer +
‘rootcimv2’)
colProcessList =3D loWMIService.ExecQuery =
;
(‘Select * from Win32_Process’=
)
Create Cursor Process (Name c(20),Id i,Thread i,pagefile
i,pagefault i,workingset c(20))
Index On Name Tag Name
For Each loProcess
In colProcessList=
Insert Into Process (=
Name,Id,Thread,pagefile,pagefault,workingset=
);
VALUES
(loProcess.Name,loProcess.ProcessID,loProcess.ThreadCount,loProcess.PageF=
ileUsage,;
loProcess.pagefaults,loProcess.WorkingSetSize)
Next
BROWSE normal=
Fonction qui termine tous les process
qui roulent.
CODE<=
span
style=3D'color:black'>
FUNCTION terminateProcess(lcProcess)
lcComputer=
=3D
‘.’
loWMI=
Service
=3D Getobject(=
216;winmgmts:’ ;
+ ‘{impersonationLevel=3Dimpersonate}!’ &n=
bsp; + lcComputer +
‘rootcimv2’)
colProcessList =3D loWMIService.ExecQuery =
;
(‘Select * from Win32_Process’=
)
For Each loProcess
In colProcessList=
IF UPPER(loProcess.name)
=3D lcProcess
loProcess.terminate()
endif<=
/span>
Next
Comment forcer le applet ‘Ajouter un imprimate’
CODE<=
span
style=3D'color:black'>
oShell=
span> =3D &nb=
sp; CreateObject("WScript.Shell")
oShell.Run=
("rundll32.exe
shell32.dll,SHHelpShortcuts_RunDLL AddPrinter")
Comment accéder au différentes applets du Control=
Pannel
CODE<=
span
style=3D'color:black'>
oShell=
span> =3D &nb=
sp; CreateObject('WScript.Shell')
oShell.Run=
('Control.exe
Sysdm.cpl,,0') && System =
Propeties
general Tab.
oShell.Run=
('Control.exe
Sysdm.cpl,,1') && System Properties - Computer name tab.
oShell.Run=
('Control.exe
Sysdm.cpl,,2') && System properties - Hardware tab.
oShell.Run=
('Control.exe
Sysdm.cpl,,3') && System properties - Advanced tab.
oShell.Run=
('Control.exe
Sysdm.cpl,,4') && System properties - System Restore tab (ME and &n=
bsp;
XP).
oShel=
l.Run('Control.exe Sysdm.cpl,,=
5') &&am=
p;
System properties - Automatic Updates tab.
oShell.Run=
('Control.exe
Sysdm.cpl,,6') && System properties - Remote tab.
The parameters apply to the different applets. The last parameter
refers to the tab that has the focus.
CODE<=
span
style=3D'color:black'>
oShell.R=
un('Contro=
l.exe
Access.cpl,,1') && Accessi=
bilty
Options applet focused on tab 1
oShell.Run=
('Control.exe
AppWiz.cpl,,1') && Add/Remove Programs applet focused on tab 1
oShell.Run=
('Control.exe
Desk.cpl,,1') && Display Properties applet focused on tab 1=
The rest of the Applets wo=
rks
in the same principle.
InetCpl.cpl
Internet Options
Intl.cpl C=
ountry
Joy.cpl Jo=
ystick
Main.cpl M=
ouse,
keyboard, printers and fonts
MmSys.cpl
Multimedia and sounds
Modem.cpl =
Modem
OdbcCp32.cpl ODBC data source
Password.cpl
Password
Ports.cpl =
Ports
PowerCfg.cpl Power
Management
PrefsCpl.cpl Real
Player G2
Ras.cpl Re=
mote
Access
SrvMgr.cpl
Server
SysDm.cpl =
System
Telephon.cpl
Telephony
Themes.cpl=
Desktop
themes
TimeDate.cpl
Date/Time
TweakUI.cpl TweakUI
Ups.cpl Sp=
are
power supply
|