Je cherche a executer un script VBS qui permet de passer les postes de travail de mon reseau en DHCP et dns auto.
Le probleme est que les utilisateurs n'ont pas les droits administrateurs pour excuter le script ci dessous
Global Const $userA = "administrateur"
Global Const $pwdA = "MotDePasseAdmin"
FileInstall ( "......\script_1.exe", @Tempdir & "\script_1.exe" ,1 ) ; insertion du script à exécuter en admin et copie dans le temp
If IsAdmin() Then ; si user logué est admin
RunWait(@TempDIR& "\Script_1.exe")
Else ; si user logué pas admin lancer avec l'admin local du pc
RunAsWait($userA, @ComputerName, $pwdA, 0, @TempDIR & "\Script_1.exe")
EndIf
FileDelete(@Tempdir & "\script_1.exe")
Etant donné que les fonction RunAs ne fonctionnent pas avec du VBS sauf erreur, il est peut-être utile de faire appel à la fenêtre de commande windows et lui indiqué le VBS à lancer, du style
On Error Resume Next
Dim DummyText, WshShell, WshNetwork, WshUserEnv, ValueData, PCName, fso, TextFile, PCName_Found
Dim TextLine, CopiedText, EnvPath
Const ForReading = 1, ForWriting = 2
Const FULL_DNS_REGISTRATION = True
Const DOMAIN_DNS_REGISTRATION = False
Const DEFAULT = 0
Const ENABLED = 1
Const DISABLED = 2
Const EnvName = "USER"
Const EnvVar = "DHCP"
Const LogFile = "C:\temp\dhcp_log.txt"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter In colNetAdapters
objNetAdapter.setDNSServersearchOrder()
objNetAdapter.EnableDHCP()
objNetAdapter.SetDynamicDNSRegistration _
FULL_DNS_REGISTRATION, DOMAIN_DNS_REGISTRATION
objNetAdapter.SetTcpipNetbios (ENABLED)
Set objNetworkSettings = objWMIService.Get("Win32_NetworkAdapterConfiguration")
objNetworkSettings.RenewDHCPLeaseAll()
' Get the computer name
Set WshNetwork = WScript.CreateObject("WScript.Network")
PCName = WshNetwork.ComputerName
' Search if the PC has not already been tested.
' =============================================
Set fso = CreateObject("Scripting.FileSystemObject")
if (NOT fso.FileExists(LogFile)) then
Set TextFile = fso.CreateTextFile(LogFile, ForWriting)
' Title line
TextFile.WriteLine("Nom PC; DHCP; Date; Heure")
TextFile.Close
end if
' Open the log file and search for PC Name
Set TextFile = fso.OpenTextFile(LogFile, ForReading)
' Read title line
CopiedText = TextFile.ReadLine
' Test if the PC name is already in the log file
PCName_Found = False
Do Until (TextFile.AtEndOfStream OR PCName_Found)
TextLine = TextFile.ReadLine
CopiedText = CopiedText & vbCrLf & TextLine
PCName_Found = (instr(TextLine, PCName) <> 0)
Loop
TextFile.Close
' Search for Value Data only if the PC has not already been tested.
' =================================================================
if (NOT PCName_Found) then
Set TextFile = fso.OpenTextFile(LogFile, ForWriting)
' Add previous lines first
if (CopiedText <> "") then
TextFile.WriteLine(CopiedText)
end if
' Add PC name and NLS_LANG status at the end of the log file
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshUserEnv = WshShell.Environment(EnvName)
' It seems impossible to declare the type of the variable able to receive the Value Data!
if (WshUserEnv(EnvVar) = "") then
' Nonexistent value data
TextFile.WriteLine( PCName & ";DHCP;" & Date() & ";" & Time() )
else
' The value data exists
TextFile.WriteLine( PCName & ";" & WshUserEnv(EnvVar) & ";" & Date() & ";" & Time() )
' WshShell.RegDelete("HKEY_CURRENT_USER\Environment\" & EnvVar)
end if
TextFile.Close
end if
ludogayff a écrit :pourriez vous me traduire ce sript vbs en autoit
Je me suis fait aidé par ce script : http://www.autoitscript.com/forum/index ... st&p=83650, mais il y a encore des choses à modifier, la conversion n'est pas parfaite. Et j'ai l'impression que le script VBS n'est pas complet : il y a une boucle "for" qui commence mais pas de "next" à la fin.
#include <date.au3>
Global Const $ForReading = 1, $ForWriting = 2
Global Const $FULL_DNS_REGISTRATION = 1
Global Const $DOMAIN_DNS_REGISTRATION = 0
Global Const $DEFAULT = 0
Global Const $ENABLED = 1
Global Const $DISABLED = 2
Global Const $EnvName = "USER"
Global Const $EnvVar = "DHCP"
Global Const $LogDir = "C:\temp\"
Global Const $LogFile = $LogDir & "\dhcp_log.txt"
Global $WshUserEnv, $ValueData, $PCName, $TextFile, $PCName_Found
Global $TextLine, $CopiedText, $EnvPath
Global $strComputer = "."
Global $objWMIService = ObjGet("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & $strComputer & "\root\cimv2")
Global $colNetAdapters = $objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For $objNetAdapter In $colNetAdapters
$objNetAdapter.setDNSServersearchOrder()
$objNetAdapter.EnableDHCP()
$objNetAdapter.SetDynamicDNSRegistration($FULL_DNS_REGISTRATION, $DOMAIN_DNS_REGISTRATION)
$objNetAdapter.SetTcpipNetbios($ENABLED)
$objNetworkSettings = $objWMIService.Get("Win32_NetworkAdapterConfiguration")
$objNetworkSettings.RenewDHCPLeaseAll()
; Get the computer name
$PCName = @ComputerName
; Search if the PC has not already been tested.
; =============================================
If Not FileExists($LogFile) Then
DirCreate($LogDir)
$TextFile = FileOpen($LogFile, 1)
; Check if file opened for reading OK
If $TextFile = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
; Title line
FileWriteLine($TextFile, "Nom PC; DHCP; Date; Heure")
FileClose($TextFile)
EndIf
; Open the log file and search for PC Name
$TextFile = FileOpen($LogFile, 0)
; Check if file opened for reading OK
If $TextFile = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
; Read title line
FileReadLine($TextFile)
; Test if the PC name is already in the log file
$PCName_Found = 0
Do
$TextLine = FileReadLine($TextFile)
If @error = -1 Then ExitLoop
$CopiedText = $CopiedText & @CRLF & $TextLine
$PCName_Found = StringInStr($TextLine, $PCName)
Until $PCName_Found
FileClose($TextFile)
; Search for Value Data only if the PC has not already been tested.
; =================================================================
If Not $PCName_Found Then
$TextFile = FileOpen($LogFile, 1)
; Check if file opened for reading OK
If $TextFile = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
; Add previous lines first
If $CopiedText <> "" Then
FileWriteLine($TextFile, $CopiedText)
EndIf
;~ ; Add PC name and NLS_LANG status at the end of the log file
;~ $WshUserEnv = EnvGet($EnvName)
;~ ; It seems impossible to declare the type of the variable able to receive the Value Data!
;~ If EnvGet($EnvVar) = "" Then
;~ ; Nonexistent value data
FileWriteLine($TextFile, $PCName & ";DHCP;" & _NowDate() & ";" & _NowTime())
;~ Else
;~ ; The value data exists
;~ FileWriteLine($TextFile, $PCName & ";" & EnvGet($EnvVar) & ";" & _NowDate() & ";" & _NowTime())
;~ ; RegDelete("HKEY_CURRENT_USER\Environment\" & $EnvVar)
;~ EndIf
FileClose($TextFile)
EndIf
Next
Edité : mise à jour du script.
Il reste la dernière partie à revoir (avec le "WshShell.Environment(EnvName)").
De petits détails peuvent faire toute la différence. — Quand la boule de neige commence à rouler… poussez-la. (Columbo)