1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
| <job>
<runtime>
<description author="jcb">
--------------------------------------------------------
Ce script permet d'activer ou désactiver une connexion
réseau local d'un ordinateur local en ligne de commande.
JCB © 2004
--------------------------------------------------------
</description>
<named
name = "c"
helpstring = "Nom de la connexion
Ce nom peut contenir des caractères génériques (? et *)
s'il n'y a pas d'ambiguïté.
Si ce paramètre est omis, la liste des connexions et
de leurs états respectifs est affichée."
type = "string"
required = "false" />
<named
name = "a"
helpstring = "Action à entreprendre :
ON -> activation de la connexion
OFF -> désactivation de la connexion
Si ce paramètre est omis, l'état de la connexion est affiché."
type = "string"
required = "false" />
<unnamed name = "?"
helpstring = "Affiche la présente aide"
type = "string"
required = "false" />
<example>
Exemples:
connexion.wsf
affiche la liste des connexions de l'ordinateur
connexion.wsf /c:"LAN2" /a:on
active la connexion "LAN2"
connexion.wsf /c:"Connexion*" /a:OFF
désactive la connexion "Connexion au réseau local"
connexion.wsf /c:"VM*8"
affiche l'état la connexion "VMware Network Adapter VMnet8"
--------------------------------------------------------
</example>
</runtime>
<script language="VBScript">
Const HKEY_LOCAL_MACHINE = &H80000002
Const ssfCONTROLS = 3
StateMsg=array("désactivée","activée","?")
Set net = Wscript.CreateObject("WScript.Network")
Set shell = WScript.CreateObject("WScript.Shell")
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set args = Wscript.Arguments
nbargs=args.count
TestHost false
Set Named = WScript.Arguments.Named
Set UnNamed = WScript.Arguments.UnNamed
nu=UnNamed.count
If (nu>0) then
If instr(UnNamed(0),"?")>0 Then
WScript.Arguments.ShowUsage
WScript.Quit
End If
End If
curConnexion=""
If named.Exists("c") Then curConnexion=named.Item("c")
NewState=2
If named.Exists("a") Then
Select Case lcase(named.Item("a"))
Case "off"
NewState=0
Case "on"
NewState=1
Case else
WScript.Arguments.ShowUsage
WScript.Quit
End Select
end if
If (NewState<>2) and (curConnexion="") Then
WScript.Arguments.ShowUsage
WScript.Quit
End If
'on error resume next
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\." & _
"\root\default:StdRegProv")
dim Number
Trouve=false
KeyClass = "SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}"
KeyNet = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
oReg.EnumKey HKEY_LOCAL_MACHINE, KeyClass, Number
For Each Num In Number
Key= KeyClass & "\" & Num
oReg.GetStringValue HKEY_LOCAL_MACHINE,Key,"DriverDesc",Driver
oReg.GetStringValue HKEY_LOCAL_MACHINE,Key,"NetCfgInstanceId",ID
oReg.GetStringValue HKEY_LOCAL_MACHINE,Key & "\Ndi","Service",Service
Key= KeyNet & "\" & ID & "\Connection"
rc=oReg.GetStringValue(HKEY_LOCAL_MACHINE,Key,"Name",Name)
If rc=0 Then
If CurConnexion="" Then
WScript.Echo Name & " : " & StateMsg(State(Service))
Else
If TestConnexion(CurConnexion,Name) Then
trouve=true
exit for
End If
End If
end if
Next
If CurConnexion="" then wscript.quit
If not trouve Then Abort "Connexion " & curConnexion & " non trouvée",1
If NewState=2 Then Abort Name & " : " & StateMsg(State(Service)),0
If NewState=State(Service) Then Abort "Connexion " & Name & " déjà " & StateMsg(State(Service)),2
' Détermination du dossier système des connexions distantes
keyFolder="SOFTWARE\Classes\CLSID\{7007ACC7-3202-11D1-AAD2-00805FC1270E}"
oReg.GetStringValue HKEY_LOCAL_MACHINE,keyFolder,"",ConnectionFolder
set shellApp = createobject("shell.application")
set oControlPanel = shellApp.Namespace(ssfCONTROLS)
set oNetConnections = nothing
for each folderitem in oControlPanel.items
if folderitem.name = ConnectionFolder then
set oNetConnections = folderitem.getfolder
exit for
end if
next
if oNetConnections is nothing then Abort "Dossier " & ConnectionFolder & " non trouvé",3
set oLanConnection = nothing
for each folderitem in oNetConnections.items
if lcase(folderitem.name)=lcase(Name) then
set oLanConnection = folderitem
exit for
end if
next
if oLanConnection is nothing then Abort "Connexion " & Name & " non trouvée dans " & ConnectionFolder,4
for each verb in oLanConnection.verbs
verb.doit
exit for
next
wscript.sleep 1000
wscript.echo "Connexion " & Name & " " & StateMsg(NewState)
WScript.Quit
'--------------------------------------------------------------------
Function TestConnexion(Connexion,Name)
TestConnexion=false
c=Replace(Connexion,".","\.")
c=Replace(c,"$","\$")
c=Replace(c,"?",".")
c=Replace(c,"*","[\s\S]*") & "$"
Set regEx = New RegExp
regEx.Pattern = c
regEx.IgnoreCase = True
TestConnexion=regEx.test(Name)
End Function
'--------------------------------------------------------------------
Sub abort(msg,code)
wscript.echo msg
wscript.quit code
End Sub
'--------------------------------------------------------------------
'Sous-programme de test du moteur
'Vu les sorties générées, c'est CSCRIPT (et non pas WSCRIPT)
'qui doit être utilisé de préférence
Sub TestHost(force)
dim rep
strappli=lcase(Wscript.ScriptFullName)
strFullName =lcase(WScript.FullName)
i=InStr(1,strFullName,".exe",1)
j=InStrRev(strFullName,"\",i,1)
strCommand=Mid(strFullName,j+1,i-j-1)
if strCommand<>"cscript" then
If force then
Init="Ce script doit être lancé avec CSCRIPT"
Else
Init="Il est préférable de lancer ce script avec CSCRIPT"
End If
rep=MsgBox(Init & VBCRLF & _
"Cela peut être rendu permanent avec la commande" & VBCRLF & _
"cscript //H:CScript //S /Nologo" & VBCRLF & _
"Voulez-vous que ce soit fait automatiquement?", _
vbYesNo + vbQuestion,strappli)
if rep=vbYes then
nomcmd="setscript.bat"
Set ficcmd = fso.CreateTextFile(nomcmd)
ficcmd.writeline "@echo off"
ficcmd.writeline "cscript //H:CScript //S /Nologo"
ficcmd.writeline "pause"
params=""
For i = 0 To nbargs-1
params=params & " " & args(i)
next
ficcmd.writeline chr(34) & strappli & chr(34) & params
ficcmd.writeline "pause"
ficcmd.close
shell.Run nomcmd, SW_SHOWNORMAL,true
force=true
end if
If force then WScript.Quit
end if
end sub
'--------------------------------------------------------------------
Function state(service)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_SystemDriver where Name="""& Service & """",,48)
For Each objItem in colItems
Select Case lcase(objItem.State)
Case "running"
state=1
Case "stopped"
state=0
Case else
state=2
End Select
Next
End Function
'--------------------------------------------------------------------
</script>
</job> |
Partager