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

Scripts/Batch Discussion :

Programme permettant de déplacer des ordinateurs de l'OU Computer dans une autre OU


Sujet :

Scripts/Batch

  1. #1
    Candidat au Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Février 2017
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Février 2017
    Messages : 5
    Points : 3
    Points
    3
    Par défaut Programme permettant de déplacer des ordinateurs de l'OU Computer dans une autre OU
    Bonjour à toutes et à tous,

    Je me permet de vous solliciter car je suis bloqué sur un programme que je développe en Powershell.

    Pour ma société, je développe un petit programme qui permet de déplacer un ordinateur (sélectionné depuis un combobox) qui se situe dans l'OU Computers vers une autre OU (que l'on a sélectionné dans une ComboBox), on lui rajoute ces groupes ainsi qu'une description.

    Je ne me suis pas encore attaqué aux groupes pour le moment par contre j'arrive à leur mettre une description depuis le programme mais le gros problème réside que je n'arrive pas à faire déplace l'ordinateur vers l'ou sélectionné.

    voici mon programme

    Code PowerShell : 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
    #Importation du module Active Directory.
    import-module activedirectory
     
    #Ouvre une fenêtre.
    [reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
    $form1 = New-Object Windows.Forms.Form
    $form1.text = "Gestion Ordinateurs"             
    $form1.Size = New-Object System.Drawing.Size(400,400)
     
    # Création du bouton Cancel
    $ButtonCancel = New-Object System.Windows.Forms.Button
    $ButtonCancel.Location = New-Object System.Drawing.Point(250,320)
    $ButtonCancel.Size = New-Object System.Drawing.Size(75,23)
    $ButtonCancel.Text = "Annuler"
    $ButtonCancel.DialogResult = [system.Windows.Forms.DialogResult]::Cancel
     
    # Création d'un label
    $FormLabel1 = New-Object System.Windows.Forms.Label
    $FormLabel1.Location = New-Object System.Drawing.Point(10,10)
    $FormLabel1.Size = New-Object System.Drawing.Size(120,23)
    $FormLabel1.Text = " Nom de l'ordinateur : "
     
    $FormLabel2 = New-Object System.Windows.Forms.Label
    $FormLabel2.Location = New-Object System.Drawing.Point(10,70)
    $FormLabel2.Size = New-Object System.Drawing.Size(120,23)
    $FormLabel2.Text = " Ranger dans l'OU : "
     
    $FormLabel3 = New-Object System.Windows.Forms.Label
    $FormLabel3.Location = New-Object System.Drawing.Point(10,130)
    $FormLabel3.Size = New-Object System.Drawing.Size(150,23)
    $FormLabel3.Text = " Description de l'ordinateur : "
     
    $FormLabel4 = New-Object System.Windows.Forms.Label
    $FormLabel4.Location = New-Object System.Drawing.Point(10,190)
    $FormLabel4.Size = New-Object System.Drawing.Size(205,23)
    $FormLabel4.Text = " Groupe d'impression pour l'ordinateur : "
     
    $FormLabel5 = New-Object System.Windows.Forms.Label
    $FormLabel5.Location = New-Object System.Drawing.Point(10,250)
    $FormLabel5.Size = New-Object System.Drawing.Size(205,23)
    $FormLabel5.Text = " Autre groupe pour l'ordinateur : "
     
    $Computer = Get-ADComputer -LDAPFilter '(name=*)' -SearchBase "CN=Computers,DC="domaine",DC=fr"
    $Computer = $Computer.Name
     
    #Création de la Textbox
    $FormTextBox1 = New-Object System.Windows.Forms.ComboBox
    $FormTextBox1.Location = New-Object System.Drawing.Point(15,35)
    $FormTextBox1.Size = New-Object System.Drawing.Size(100,10)
    $FormTextBox1.Height = 20
     
    #Pour afficher par ordre alphabétique
    $FormTextBox1.Sorted = $true
     
    #La combobox sera en menu déroulant descendant
    $FormTextBox1.DropDownStyle = "DropDownList"
     
    #Retourne les valeurs récupérées dans la variable $OU dans la combobox
    foreach($ComputerList in $Computer)
    {
      $FormTextBox1.Items.add($ComputerList)
    }
     
    $FormTextBox2 = New-Object System.Windows.Forms.TextBox
    $FormTextBox2.Location = New-Object System.Drawing.Point(15,155)
    $FormTextBox2.Size = New-Object System.Drawing.Size(200,10)
    $FormTextBox2.Height = 20
     
    # Création du bouton Ok
    $ButtonOK = New-Object System.Windows.Forms.Button
    $ButtonOK.Location = New-Object System.Drawing.Point(50,320)
    $ButtonOK.Size = New-Object System.Drawing.Size(75,23)
    $ButtonOK.Text = "Ok"
    $ButtonOK.DialogResult = [system.Windows.Forms.DialogResult]::Ok
    #Action lors du clic de bouton Ok
    $ButtonOK.Add_Click({ordi})
     
    #Récupération des OU de notre AD
    $OU = Get-ADOrganizationalUnit -LDAPFilter '(name=*)' -SearchBase 'OU=Postes Clients,OU=...,DC="domaine",DC=fr'
    $OU = $OU.Name
     
    #Liste deroulante (ComboBox)
    $FormComboBox1 = New-Object System.Windows.Forms.Combobox
    $FormComboBox1.Location = New-Object Drawing.Point (15,95)
    $FormComboBox1.Size = New-Object System.Drawing.Size(200,10)
     
    #Pour afficher par ordre alphabétique
    $FormComboBox1.Sorted = $true
     
    #La combobox sera en menu déroulant descendant
    $FormComboBox1.DropDownStyle = "DropDownList"
     
    #Retourne les valeurs récupérées dans la combobox
    foreach($OUList in $OU)
    {
      $FormComboBox1.Items.add($OUList)
     
    }
     
    $GroupeImpression = Get-ADGroup -Filter "*" -SearchBase 'OU=Groupes_Impression,OU=...,DC="domaine",DC=fr'
    $GroupeImpression = $GroupeImpression.name
     
    #Liste deroulante (ComboBox)
    $FormComboBox2 = New-Object System.Windows.Forms.Combobox
    $FormComboBox2.Location = New-Object Drawing.Point (15,215)
    $FormComboBox2.Size = New-Object System.Drawing.Size(200,10)
     
    #Pour afficher par ordre alphabétique
    $FormComboBox2.Sorted = $true
     
    $FormComboBox2.DropDownStyle = "DropDownList"
     
    #Retourne les valeurs récupérées dans la combobox
    foreach($GroupeImpressionList in $GroupeImpression)
    {
      $FormComboBox2.Items.add($GroupeImpressionList)
    }
     
    $GroupeOther = Get-ADGroup -Filter "*" -SearchBase 'CN=G_724Poste,OU=...,DC="domaine",DC=fr'
    $GroupeOther = $GroupeOther.Name
     
    #Liste deroulante (ComboBox)
    $FormComboBox3 = New-Object System.Windows.Forms.Combobox
    $FormComboBox3.Location = New-Object Drawing.Point (15,275)
    $FormComboBox3.Size = New-Object System.Drawing.Size(200,10)
     
    #Pour afficher par ordre alphabétique
    $FormComboBox3.Sorted = $true
     
    $FormComboBox3.DropDownStyle = "DropDownList"
     
    #Retourne les valeurs récupérées dans la combobox
    foreach($GroupeOtherList in $GroupeOther)
    {
      $FormComboBox3.Items.add($GroupeOtherList)
    }
     
    function ordi {
    Get-ADObject -Identity $FormTextBox1.SelectedItem | %{Move-ADObject -targetpath $FormComboBox1.SelectedItem}
     
    Set-ADComputer $FormTextBox1.SelectedItem -Description $FormTextBox2.Text
     
    Write-Host "Le poste "$FormTextBox1.SelectedItem" a été mis dans l'OU "$FormComboBox1.SelectedItem," a pour description " $FormTextBox2.Text
    }
     
    $form1.controls.add($ButtonCancel)
    $form1.controls.add($ButtonOK)
    $form1.controls.add($FormLabel1)
    $form1.controls.add($FormLabel2)
    $form1.controls.add($FormLabel3)
    $form1.controls.add($FormLabel4)
    $form1.controls.add($FormLabel5)
    $form1.controls.add($FormTextBox1)
    $form1.controls.add($FormTextBox2)
    $form1.controls.add($FormComboBox1)
    $form1.controls.add($FormComboBox2)
    $form1.controls.add($FormComboBox3)
     
    #Affiche le tout.
    $form1.ShowDialog()
    #Fin.

    Voici l'erreur que je rencontre
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Get-ADObject : Impossible de trouver un objet avec l’identité «TESTJG» sous: «
    DC="domaine",DC=fr».
    Au caractère C:\Users\jgrudzien\Documents\SAPIEN\Scripts\test.ps1:139 : 1
    + Get-ADObject -Identity $FormTextBox1.SelectedItem | %{Move-ADObject - ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (TESTJG:ADObject) [Get-ADObject], ADIdentit 
       yNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management. 
       ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADObject
    TESTJG est le nom du pc que je récupérer dans la FormTextBox (attention je n'ai pas modifier mais il s'agit d'un combobox en réalité) et ce poste existe bien dans notre Active directory.

    Le soucis est que je n'arrive pas à régler le problème, j'ai cherché un peu partout sur le net, tester divers ligne de commande rien.

    Merci d'avance de l'aide que vous m'apporterez.

  2. #2
    Membre actif
    Avatar de troxsa
    Inscrit en
    Novembre 2004
    Messages
    386
    Détails du profil
    Informations personnelles :
    Âge : 50

    Informations forums :
    Inscription : Novembre 2004
    Messages : 386
    Points : 264
    Points
    264
    Par défaut
    Bonjour,

    Moi j'utilise [adsi] car je suis en powershell version 2.0 (Windows 7 "Hé oui il y en a encore beaucoup")

    Il est possible que le problème viens de "import-module activedirectory"



  3. #3
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 183
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 45
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Responsable déploiement (SCCM, InTune, GPO)
    Secteur : Transports

    Informations forums :
    Inscription : Juillet 2014
    Messages : 3 183
    Points : 5 754
    Points
    5 754
    Par défaut
    Ton module semble correctement chargé.

    Pourquoi ne pas utiliser la cmdlet Get-ADComputer appropriée ?

    Sinon un truc comme ça avec Get-ADObject :
    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    Get-ADObject -Filter {(objectClass -eq "Computer") -and (Name -eq $($FormTextBox1.SelectedItem))}

  4. #4
    Candidat au Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Février 2017
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Février 2017
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    J'ai trouvé ma solution =

    Code PowerShell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    $ButtonOK.Add_Click({
    $computerPC = $($FormTextBox1.SelectedItem)
    $Service = $($FormComboBox1.SelectedItem)
    $OUCible = "OU=$Service,OU=Postes Clients,OU=...,DC=...,DC=fr"
     
    Get-ADComputer $computerPC | Move-ADObject -targetpath $OUCible
    Set-ADComputer $FormTextBox1.SelectedItem -Description $FormTextBox2.Text
     
    Write-Host "Le poste "$FormTextBox1.SelectedItem" a été mis dans l'OU "$FormComboBox1.SelectedItem," a pour description " $FormTextBox2.Text

    J'ai enlevé la fonction ordi (je me mélangeais les pinceaux tout seul à force).

    Maintenant il ne me reste plus qu'à trouver comment on rajoute le pc sélectionné dans le bon groupe AD et j'aurais terminé. Dès que j'ai trouvé je reviens vers vous.

  5. #5
    Candidat au Club
    Homme Profil pro
    Technicien maintenance
    Inscrit en
    Février 2017
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Technicien maintenance

    Informations forums :
    Inscription : Février 2017
    Messages : 5
    Points : 3
    Points
    3
    Par défaut
    Bon trouvé

    Code PowerShell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    $computerPC = $($FormTextBox1.SelectedItem)
    $ordinateur = "CN=$computerPC,OU=Postes Clients,OU=...,DC=...,DC=fr"
     
    Get-ADGroup $GroupeImp | Add-ADGroupMember -Members $ordinateur

    Par contre, mon collègue me signale que j'ai fais une erreur quand on prépare un poste chez nous, le poste descend direct dans une OU qui contient toutes les OU de nos services et donc tous les ordinateurs de l'entreprise. J'ai modifié pour qu'il ne pointe plus dans OU Computers mais Postes Clients.
    Mais du coup il m'affiche TOUS les ordinateurs. Est-il possible de ne cibler que les ordinateurs rangés que dans l'OU parent (ici Postes Clients).

    En espérant m'être fait comprendre.

Discussions similaires

  1. [XL-2010] [Programme permettant de supprimer des lignes en fonction du contenu d'une cellule]
    Par Etrof dans le forum Macros et VBA Excel
    Réponses: 5
    Dernier message: 08/07/2016, 21h14
  2. copier des cellules A1:AZ1 et coller dans une autre feuille en E1:E42
    Par zergo dans le forum Macros et VBA Excel
    Réponses: 0
    Dernier message: 06/02/2010, 07h40
  3. Réponses: 3
    Dernier message: 05/08/2009, 00h34
  4. Réponses: 1
    Dernier message: 06/05/2009, 15h12
  5. Réponses: 1
    Dernier message: 20/03/2007, 08h58

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