IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

VBScript Discussion :

Erreur dans l’exécution du script renommage et changement IP


Sujet :

VBScript

  1. #1
    Futur Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Octobre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Octobre 2016
    Messages : 4
    Points : 5
    Points
    5
    Par défaut Erreur dans l’exécution du script renommage et changement IP
    Bonjour

    Je viens de faire des recherches mais ne trouve aucun sujet qui pourrais m'aider.
    Je tiens a préciser que je suis un peu novice dans la création de script et mes connaissances sont très sommaire.

    Je souhaite créer un script qui me permettrait de renommer un poste dans un premier temps et d'ensuite modifier l'adresse réseau.
    Après de longue recherche sur le Web pour trouver des équivalents, je me retrouve bloquer et je ne vois pas d’où viens le problème.

    Je vous sollicite donc pour m'éclairer et me permettre de mettre en place un script fonctionnel.
    Je vous colle le script tel qu'il est actuellement.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    ' ***************************************************************************************************************
    ' Changer le nom de l'ordinateur
    ' ***************************************************************************************************************
    strComputer = "."
     
    ' Paramètres à saisir au clavier  ************************
    strNouveauNom = Inputbox ("Entrer le nouveau nom du PC : ")
     
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     
    Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
     
    For Each objComputer in colComputers
        err = objComputer.Rename(strNouveauNom)
    Next
    ' ***************************************************************************************************************
    ' Changement adresse IP
    ' ***************************************************************************************************************
    strComputer = "."
     
    ' Paramètres à saisir au clavier  **************************
    strIPAddress = Inputbox ("Entrer l'adresse IP : ")
    strSubnetMask = Inputbox ("Entrer le masque de sous reseau : ")
    strGateway = Inputbox ("Entrer la passerelle : ")
    strDNSServers = Inputbox ("Entrer le DNS : ")
    strGatewayMetric = Array(1)
     
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     
    Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration whereIPEnabled=TRUE")
     
    For Each objNetAdapter in colNetAdapters
    errEnable = objNetAdapter.EnableStatic(strIPAddress,strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway,strGatewaymetric)
    errDOM = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)
     
    If errEnable = 0 Then
    WScript.Echo "L'addresse IP a été changée correctement."
    Else
    WScript.Echo "L'addresse n'a pas été changée il y a un Pb."
    End If
    Next
    Merci par avance

  2. #2
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut

    Pour la partie changement d'adresse ip :
    Edit : Ajout d'une fonction pour vérifier la validité de l'adresse ip saisi par l'utilisateur
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    Titre = "Changement adresse IP"
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
     
    Set colNetAdapters = objWMIService.ExecQuery _ 
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
    Do    
        strIPAddress =Inputbox ("Entrer l'adresse IP : ",Titre,"192.168.1.150")
        If strIPAddress = "" Then Wscript.Quit()
        If IP_Valide(strIPAddress) = False Then 
            MsgBox "l'adresse ip " & strIPAddress & " est invalide !",vbCritical,Titre
        End If
    Loop until IP_Valide(strIPAddress) = True
     
    Do
        strSubnetMask = Inputbox ("Entrer le masque de sous réseau : ",Titre,"255.255.255.0")
        If strSubnetMask = "" Then Wscript.Quit()
        If IP_Valide(strSubnetMask) = False Then 
            MsgBox "l'adresse ip " & strSubnetMask & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strSubnetMask) = True
     
    Do
        strGateway = Inputbox ("Entrer la passerelle : ",Titre,"192.168.1.1")
        If strGateway = "" Then Wscript.Quit()
        If IP_Valide(strGateway) = False Then 
            MsgBox "l'adresse ip " & strGateway & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strSubnetMask) = True
     
    Do
        strDNSServers = Inputbox ("Entrer le DNS : ",Titre,"192.168.1.1")
        If strDNSServers = "" Then Wscript.Quit()
        If IP_Valide(strDNSServers) = False Then 
            MsgBox "l'adresse ip " & strDNSServers & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strDNSServers) = True
     
    strIPAddress = Array(strIPAddress) 
    strSubnetMask = Array(strSubnetMask) 
    strGateway = Array(strGateway) 
    strDNSServers = Array(strDNSServers)
    strGatewayMetric = Array(1) 
     
    For Each objNetAdapter in colNetAdapters 
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway,strGatewaymetric)
    errDOM = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)
     
        If errEnable = 0 Then 
            MsgBox "The IP address has been changed.",vbInformation,Titre 
        Else 
            Msgbox "The IP address could not be changed.",vbCritical,Titre 
        End If 
    Next 
    '*************************************************************************************
    Function IP_Valide(ip)
        Set RegularExpressionObject = New RegExp
        With RegularExpressionObject
            .Pattern = "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"
            .IgnoreCase = False
            If .Test(ip)= True then
                IP_Valide = True
            end if
        End With
    End Function
    '*************************************************************************************

  3. #3
    Futur Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Octobre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Octobre 2016
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Bonjour Hackoofr

    Merci pour ta réponse, je viens de l'incrémenter dans mon script et tout passe bien.
    La seule problèmatique est que quand le poste a 2 cartes réseaux, par exemple une carte filaire et sans fil, comment puis-je faire pour lui demander de n'attaquer que la carte ethernet ?

    Je recherche mais ne trouve pas le code qui correspond à cette fonction.

    Je recherche également pour en plus de changer le nom du poste insérer un descriptif.

    Merci encore

  4. #4
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut

    Nouveau script pour renommer le nom du PC avec description et changement d'adresse ip :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    'Run as Admin
    If Not WScript.Arguments.Named.Exists("elevate") Then
       CreateObject("Shell.Application").ShellExecute DblQuote(WScript.FullName) _
       , DblQuote(WScript.ScriptFullName) & " /elevate", "", "runas", 1
        WScript.Quit
    End If
     
    Titre = "Renommage du PC et Changement d'adresse IP"
    'Lire les informations a propos du nom du PC et les adresses ip 
    Msgbox IP_Info,VbInformation,"Configuration réseau de l'ordinateur"
    '*******************************************************************************
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    ' ********************  Paramأ¨tres أ  saisir au clavier  ************************
    strNouveauNom = Inputbox ("Entrer le nouveau nom du PC : ",Titre,"Hackoo-PC")
    If strNouveauNom = "" Then Wscript.Quit()
    ' ***************************Changer le nom du PC ******************************
    Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
    For Each objComputer in colComputers
        err = objComputer.Rename(strNouveauNom)
    Next
    '*************************** Changer la description **********************************
    strDescription = Inputbox("Enter Description : ",Titre,"Ordinateur de bureau balablalaaaa")
    If strDescription = "" Then Wscript.Quit()
    Set Obj= GetObject("winmgmts:\\" & strComputer).InstancesOf("Win32_OperatingSystem")
    For Each x In Obj 
       x.Description = strDescription
       x.Put_
    Next
    '***************************** Changement adresse  IP **************************
    Set colNetAdapters = objWMIService.ExecQuery _ 
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
    Do    
        strIPAddress =Inputbox ("Entrer l'adresse IP : ",Titre,"192.168.1.150")
        If strIPAddress = "" Then Wscript.Quit()
        If IP_Valide(strIPAddress) = False Then 
            MsgBox "l'adresse ip " & strIPAddress & " est invalide !",vbCritical,Titre
        End If
    Loop until IP_Valide(strIPAddress) = True
     
    Do
        strSubnetMask = Inputbox ("Entrer le masque de sous réseau : ",Titre,"255.255.255.0")
        If strSubnetMask = "" Then Wscript.Quit()
        If IP_Valide(strSubnetMask) = False Then 
            MsgBox "l'adresse ip " & strSubnetMask & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strSubnetMask) = True
     
    Do
        strGateway = Inputbox ("Entrer la passerelle : ",Titre,"192.168.1.1")
        If strGateway = "" Then Wscript.Quit()
        If IP_Valide(strGateway) = False Then 
            MsgBox "l'adresse ip " & strGateway & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strSubnetMask) = True
     
    Do
        strDNSServers = Inputbox ("Entrer le DNS : ",Titre,"192.168.1.1")
        If strDNSServers = "" Then Wscript.Quit()
        If IP_Valide(strDNSServers) = False Then 
            MsgBox "l'adresse ip " & strDNSServers & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strDNSServers) = True
     
    strIPAddress = Array(strIPAddress) 
    strSubnetMask = Array(strSubnetMask) 
    strGateway = Array(strGateway) 
    strDNSServers = Array(strDNSServers)
    strGatewayMetric = Array(1) 
     
    For Each objNetAdapter in colNetAdapters 
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway,strGatewaymetric)
    errDOM = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)
     
        If errEnable = 0 Then 
            MsgBox "The IP address has been changed.",vbInformation,Titre
            'Lire les informations a propos du nom du PC et les adresses ip attribués
            Msgbox IP_Info,VbInformation,"Configuration réseau de l'ordinateur"
            Call Ask4Reboot()
        Else 
            Msgbox "The IP address could not be changed.",vbCritical,Titre
            Msgbox IP_Info,VbInformation,"Configuration réseau de l'ordinateur"
        End If 
    Next 
    '*************************************************************************************
    Function IP_Valide(ip)
        Set RegularExpressionObject = New RegExp
        With RegularExpressionObject
            .Pattern = "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"
            .IgnoreCase = False
            If .Test(ip)= True then
                IP_Valide = True
            end if
        End With
    End Function
    '*************************************************************************************
    Function IP_Info()
        Dim ComputerName,IPConfigSet,IPConfig,BailObtenu,BailExpirant
        ComputerName="."
        On error resume next 
        set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & ComputerName).ExecQuery _ 
        ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE") 
        If Err.Number <> 0 Then 
            msgbox " - non accessible -" 
        Else 
            for each IPConfig in IPConfigSet 
                BailObtenu = IPConfig.DHCPLeaseObtained
                BailExpirant = IPConfig.DHCPLeaseExpires
    '---- Convertion des date et heure d'obtention et d'expiration des baux DHCP en un format lisible par l'utilisateur. ----
                BailObtenu = mid(BailObtenu, 7, 2) & "/" & mid(BailObtenu, 5, 2) & "/" & mid(BailObtenu, 1, 4) & " - " & mid(BailObtenu, 9, 2)& ":" & mid(BailObtenu, 11, 2)& ":" & mid(BailObtenu, 13, 2)
                BailExpirant = mid(BailExpirant, 7, 2) & "/" & mid(BailExpirant, 5, 2) & "/" & mid(BailExpirant, 1, 4) & " - " & mid(BailExpirant, 9, 2)& ":" & mid(BailExpirant, 11, 2)& ":" & mid(BailExpirant, 13, 2)
     
                Msg = "Configuration réseau de l'ordinateur : " & IPConfig.DNSHostName & vbcrlf & vbcrlf & _ 
                "Nom Machine " & vbtab & " : " & vbtab & IPConfig.DNSHostName & vbcrlf & vbcrlf & _ 
                "Carte active" & vbtab & " : " & vbtab & IPConfig.Description & vbcrlf & vbcrlf & _ 
                "Adresse MAC " & vbtab & " : " & vbtab & IPConfig.MACAddress & vbcrlf & _ 
                "DHCP Activé" & vbtab & " : " & vbtab & IPConfig.DHCPEnabled & vbcrlf & vbcrlf & _
                "Adresse IP " & vbtab & " : " & vbtab & IPConfig.IPAddress(0) & vbcrlf & _
                "Masque " & vbtab & vbtab & " : " & vbtab & IPConfig.IPSubnet(0) & vbcrlf & _
                "Passerelle     " & vbtab & " : " & vbtab & IPConfig.DefaultIPGateway(0) & vbcrlf & _   
                "Serveur DHCP " & vbtab & " : " & vbtab & IPConfig.DHCPServer & vbcrlf & vbcrlf & _
                "Serveur DNS " & vbtab & " : " & vbtab & IPConfig.DNSServerSearchOrder(0) & vbcrlf & _
                "             " & vbtab & vbtab & " : " & vbtab & IPConfig.DNSServerSearchOrder(1) & vbcrlf & _
                vbcrlf & _
                " Bail obtenu " & vbtab & " : " & vbtab & BailObtenu & vbcrlf & _
                " Bail expirant " & vbtab & " : " & vbtab & BailExpirant                  
                '"Serveur WINS " & vbtab & " : " & IPConfig.WINSPrimaryServer(0) & vbcrlf & _
                '"             " & vbtab & vbtab & " : " & IPConfig.WINSSecondaryServer(0) 
                'MsgBox Msg,VbInformation,"Configuration réseau de l'ordinateur " 
            Next 
            IP_Info = Msg
        End If
    End Function
    '****************************************************************************
    Function SimpleQuote(Str)
        SimpleQuote = ChrW(39) & Str & ChrW(39)
    End Function
    '****************************************************************************
    Sub Ask4Reboot()
    Question = MsgBox("Le nom du PC changera en " & DblQuote(strNouveauNom) & " après le redémarrage de cet ordinateur" & vbCrLf &_
    "Oui pour redémarrer" & vbCrLF &_
    "Non pour annuler le redémarrage" & vbtab & "?",VbYesNo+VbQuestion,"Renommage du PC")
    If Question = VbYes then 
        Reboot()
    Else
        wscript.Quit(1)
    End If
    End Sub
    '***************************************************************************
    Function DblQuote(Str)
        DblQuote = chr(34) & Str & chr(34)
    End function
    '***************************************************************************
    Sub Reboot()
    Dim ws,Command,Result
    Set ws = CreateObject("Wscript.Shell")
    Command = "Shutdown.exe /r /t 20 /c "& DblQuote("Enregistrer vos documents - Redemarrage du PC dans 20 secondes")
    Result = ws.run(Command,0,True)
    End Sub
    '**************************************

  5. #5
    Futur Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Octobre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Octobre 2016
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Qu'est ce que j'aime ce site

    J'allais justement mettre mon nouveau script avec toutes les fonctions mais qui ne me change pas la description du poste.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    ' ***************************************************************************************************************
    ' Changer le nom de l'ordinateur
    ' ***************************************************************************************************************
    strComputer = "."
     
    ' Paramètres à saisir au clavier  ************************
    strNouveauNom = Inputbox ("Entrer le nouveau nom du PC : ")
     
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     
    Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
     
    For Each objComputer in colComputers
        err = objComputer.Rename(strNouveauNom)
    Next
    '****************************************************************************************************************
    'Changement description poste
    '****************************************************************************************************************
    Set WshNetwork = WScript.CreateObject("WScript.Network")
     
    strComputer = "."
    strDescription = Inputbox("Entrer la Description : ")
     
    Set Obj= GetObject("winmgmts:\\" & strComputer).InstancesOf("Win32_OperatingSystem")
    For Each x In Obj 
       x.Description = strDescription
    Next
    '****************************************************************************************************************
    'Changement adresse IP
    '****************************************************************************************************************
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
     
    Set colNetAdapters = objWMIService.ExecQuery _ 
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
    Do    
        strIPAddress =Inputbox ("Entrer l'adresse IP : ",Titre,"192.168.1.150")
        If strIPAddress = "" Then Wscript.Quit()
        If IP_Valide(strIPAddress) = False Then 
            MsgBox "l'adresse ip " & strIPAddress & " est invalide !",vbCritical,Titre
        End If
    Loop until IP_Valide(strIPAddress) = True
     
    Do
        strSubnetMask = Inputbox ("Entrer le masque de sous reseau : ",Titre,"255.255.255.0")
        If strSubnetMask = "" Then Wscript.Quit()
        If IP_Valide(strSubnetMask) = False Then 
            MsgBox "l'adresse ip " & strSubnetMask & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strSubnetMask) = True
     
    Do
        strGateway = Inputbox ("Entrer la passerelle : ",Titre,"192.168.1.1")
        If strGateway = "" Then Wscript.Quit()
        If IP_Valide(strGateway) = False Then 
            MsgBox "l'adresse ip " & strGateway & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strSubnetMask) = True
     
    Do
        strDNSServers = Inputbox ("Entrer le DNS : ",Titre,"192.168.1.1")
        If strDNSServers = "" Then Wscript.Quit()
        If IP_Valide(strDNSServers) = False Then 
            MsgBox "l'adresse ip " & strDNSServers & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strDNSServers) = True
     
    strIPAddress = Array(strIPAddress) 
    strSubnetMask = Array(strSubnetMask) 
    strGateway = Array(strGateway) 
    strDNSServers = Array(strDNSServers)
    strGatewayMetric = Array(1) 
     
    For Each objNetAdapter in colNetAdapters 
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway,strGatewaymetric)
    errDOM = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)
     
        If errEnable = 0 Then 
            MsgBox "L'adresse IP est changée.",vbInformation,Titre 
        Else 
            Msgbox "L'adresse IP n'est pas changée.",vbCritical,Titre 
        End If 
    Next 
    '*************************************************************************************
    Function IP_Valide(ip)
        Set RegularExpressionObject = New RegExp
        With RegularExpressionObject
            .Pattern = "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"
            .IgnoreCase = False
            If .Test(ip)= True then
                IP_Valide = True
            end if
        End With
    End Function
    Je vais tester de ce pas ton nouveau script pour voir si il correspond bien à mon besoin.
    Merci encore et je te tiens au courant.

  6. #6
    Futur Membre du Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Octobre 2016
    Messages
    4
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Octobre 2016
    Messages : 4
    Points : 5
    Points
    5
    Par défaut
    Bon aprés plusieurs tests je suis ravi.
    J'ai juste apporté de petite modification car la MsgBox pour les infos du PC n'apparaissait pas.
    C'est juste parfait, merci beaucoup.
    Je joins le script final pour ceux que ça peut interresser :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    'Run as Admin
    If Not WScript.Arguments.Named.Exists("elevate") Then
       CreateObject("Shell.Application").ShellExecute DblQuote(WScript.FullName) _
       , DblQuote(WScript.ScriptFullName) & " /elevate", "", "runas", 1
        WScript.Quit
    End If
     
    Titre = "Renommage du PC et Changement d'adresse IP"
    'Lire les informations a propos du nom du PC et les adresses ip 
    Msgbox IP_Info,VbInformation,Titre
    '*******************************************************************************
    strComputer = "." 
    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    ' ********************  Paramأ¨tres أ  saisir au clavier  ************************
    strNouveauNom = Inputbox ("Entrer le nouveau nom du PC : ",Titre,"Salle-Poste")
    If strNouveauNom = "" Then Wscript.Quit()
    ' ***************************Changer le nom du PC ******************************
    Set colComputers = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
    For Each objComputer in colComputers
        err = objComputer.Rename(strNouveauNom)
    Next
    '*************************** Changer la description **********************************
    strDescription = Inputbox("Entrer la Description : ",Titre,"RNE-P-Asset Tag")
    If strDescription = "" Then Wscript.Quit()
    Set Obj= GetObject("winmgmts:\\" & strComputer).InstancesOf("Win32_OperatingSystem")
    For Each x In Obj 
       x.Description = strDescription
       x.Put_
    Next
    '***************************** Changement adresse  IP **************************
    Set colNetAdapters = objWMIService.ExecQuery _ 
        ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
    Do    
        strIPAddress =Inputbox ("Entrer l'adresse IP : ",Titre,"-.-.-.-")
        If strIPAddress = "" Then Wscript.Quit()
        If IP_Valide(strIPAddress) = False Then 
            MsgBox "l'adresse ip " & strIPAddress & " est invalide !",vbCritical,Titre
        End If
    Loop until IP_Valide(strIPAddress) = True
     
    Do
        strSubnetMask = Inputbox ("Entrer le masque de sous reseau : ",Titre,"-.-.-.-")
        If strSubnetMask = "" Then Wscript.Quit()
        If IP_Valide(strSubnetMask) = False Then 
            MsgBox "l'adresse ip " & strSubnetMask & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strSubnetMask) = True
     
    Do
        strGateway = Inputbox ("Entrer la passerelle : ",Titre,"-.-.-.-")
        If strGateway = "" Then Wscript.Quit()
        If IP_Valide(strGateway) = False Then 
            MsgBox "l'adresse ip " & strGateway & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strSubnetMask) = True
     
    Do
        strDNSServers = Inputbox ("Entrer le DNS : ",Titre,"-.-.-.-")
        If strDNSServers = "" Then Wscript.Quit()
        If IP_Valide(strDNSServers) = False Then 
            MsgBox "l'adresse ip " & strDNSServers & " est invalide !",vbCritical,Titre
        End If
    Loop Until IP_Valide(strDNSServers) = True
     
    strIPAddress = Array(strIPAddress) 
    strSubnetMask = Array(strSubnetMask) 
    strGateway = Array(strGateway) 
    strDNSServers = Array(strDNSServers)
    strGatewayMetric = Array(1) 
     
    For Each objNetAdapter in colNetAdapters 
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway,strGatewaymetric)
    errDOM = objNetAdapter.SetDNSServerSearchOrder(strDNSServers)
     
        If errEnable = 0 Then 
            MsgBox "L'adresse IP a bien etait change.",vbInformation,Titre
            'Lire les informations a propos du nom du PC et les adresses ip attribués
            Msgbox IP_Info,VbInformation,Titre
            Call Ask4Reboot()
        Else 
            Msgbox "L'adresse IP n'a pas etait change.",vbCritical,Titre
            Msgbox IP_Info,VbInformation,"Configuration reseau de l'ordinateur"
        End If 
    Next 
    '*************************************************************************************
    Function IP_Valide(ip)
        Set RegularExpressionObject = New RegExp
        With RegularExpressionObject
            .Pattern = "^((25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(25[0-5]|2[0-4]\d|1?\d?\d)$"
            .IgnoreCase = False
            If .Test(ip)= True then
                IP_Valide = True
            end if
        End With
    End Function
    '*************************************************************************************
    Function IP_Info()
        Dim ComputerName,IPConfigSet,IPConfig,BailObtenu,BailExpirant
        ComputerName="."
        On error resume next 
        set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}!//" & ComputerName).ExecQuery _ 
        ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE") 
        If Err.Number <> 0 Then 
            msgbox " - non accessible -" 
        Else 
            for each IPConfig in IPConfigSet 
                BailObtenu = IPConfig.DHCPLeaseObtained
                BailExpirant = IPConfig.DHCPLeaseExpires
    '---- Convertion des date et heure d'obtention et d'expiration des baux DHCP en un format lisible par l'utilisateur. ----
                BailObtenu = mid(BailObtenu, 7, 2) & "/" & mid(BailObtenu, 5, 2) & "/" & mid(BailObtenu, 1, 4) & " - " & mid(BailObtenu, 9, 2)& ":" & mid(BailObtenu, 11, 2)& ":" & mid(BailObtenu, 13, 2)
                BailExpirant = mid(BailExpirant, 7, 2) & "/" & mid(BailExpirant, 5, 2) & "/" & mid(BailExpirant, 1, 4) & " - " & mid(BailExpirant, 9, 2)& ":" & mid(BailExpirant, 11, 2)& ":" & mid(BailExpirant, 13, 2)
     
                Msg = "Configuration reseau de l'ordinateur : " & IPConfig.DNSHostName & vbcrlf & vbcrlf & _ 
                "Nom Machine " & vbtab & " : " & vbtab & IPConfig.DNSHostName & vbcrlf & vbcrlf & _ 
                "Carte active" & vbtab & " : " & vbtab & IPConfig.Description & vbcrlf & vbcrlf & _ 
                "Adresse MAC " & vbtab & " : " & vbtab & IPConfig.MACAddress & vbcrlf & _ 
                "DHCP Active" & vbtab & " : " & vbtab & IPConfig.DHCPEnabled & vbcrlf & vbcrlf & _
                "Adresse IP " & vbtab & " : " & vbtab & IPConfig.IPAddress(0) & vbcrlf & _
                "Masque " & vbtab & vbtab & " : " & vbtab & IPConfig.IPSubnet(0) & vbcrlf & _
                "Passerelle     " & vbtab & " : " & vbtab & IPConfig.DefaultIPGateway(0) & vbcrlf & _   
                "Serveur DHCP " & vbtab & " : " & vbtab & IPConfig.DHCPServer & vbcrlf & vbcrlf & _
                "Serveur DNS " & vbtab & " : " & vbtab & IPConfig.DNSServerSearchOrder(0) & vbcrlf & _
                "             " & vbtab & vbtab & " : " & vbtab & IPConfig.DNSServerSearchOrder(1) & vbcrlf & _
                vbcrlf & _
                " Bail obtenu " & vbtab & " : " & vbtab & BailObtenu & vbcrlf & _
                " Bail expirant " & vbtab & " : " & vbtab & BailExpirant                  
                '"Serveur WINS " & vbtab & " : " & IPConfig.WINSPrimaryServer(0) & vbcrlf & _
                '"             " & vbtab & vbtab & " : " & IPConfig.WINSSecondaryServer(0) 
                'MsgBox Msg,VbInformation,"Configuration reseau de l'ordinateur " 
            Next 
            IP_Info = Msg
        End If
    End Function
    '****************************************************************************
    Function SimpleQuote(Str)
        SimpleQuote = ChrW(39) & Str & ChrW(39)
    End Function
    '****************************************************************************
    Sub Ask4Reboot()
    Question = MsgBox("Le nom du PC changera en " & DblQuote(strNouveauNom) & " apres le redemarrage de cet ordinateur" & vbCrLf &_
    "Oui pour redemarrer" & vbCrLF &_
    "Non pour annuler le redemarrage" & vbtab & "?",VbYesNo+VbQuestion,"Renommage du PC")
    If Question = VbYes then 
        Reboot()
    Else
        wscript.Quit(1)
    End If
    End Sub
    '***************************************************************************
    Function DblQuote(Str)
        DblQuote = chr(34) & Str & chr(34)
    End function
    '***************************************************************************
    Sub Reboot()
    Dim ws,Command,Result
    Set ws = CreateObject("Wscript.Shell")
    Command = "Shutdown.exe /r /t 5 /c "& DblQuote("Enregistrer vos documents - Redemarrage du PC dans 5 secondes")
    Result = ws.run(Command,0,True)
    End Sub
    '**************************************
    Je passe le sujet en résolu.

  7. #7
    Expert éminent
    Avatar de hackoofr
    Homme Profil pro
    Enseignant
    Inscrit en
    Juin 2009
    Messages
    3 839
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Enseignant

    Informations forums :
    Inscription : Juin 2009
    Messages : 3 839
    Points : 9 222
    Points
    9 222
    Par défaut

    Tu dois me payer avec cette devise +1

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. erreur dans un petit script
    Par isma92 dans le forum Shell et commandes GNU
    Réponses: 2
    Dernier message: 01/03/2011, 13h48
  2. erreur dans un script
    Par rib dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 22/12/2005, 16h42
  3. erreur dans mon script
    Par Swata dans le forum Langage
    Réponses: 4
    Dernier message: 21/09/2005, 00h02
  4. [langage]erreur dans mon script
    Par Fabouney dans le forum Langage
    Réponses: 11
    Dernier message: 30/06/2005, 14h58
  5. [Apache] Erreur dans mes scripts CGI
    Par GLDavid dans le forum Apache
    Réponses: 2
    Dernier message: 28/08/2004, 06h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo