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 :

Sélection dossiers via Powershell [PowerShell]


Sujet :

Scripts/Batch

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Technicien réseau
    Inscrit en
    Décembre 2018
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Technicien réseau
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Décembre 2018
    Messages : 49
    Par défaut Sélection dossiers via Powershell
    Bonjour à tous.
    Via un script Powershell, j'utilise la commande Robocopy.

    Au lieu de saisir la source et la destination, j'aimerais savoir si il est possible d'ouvrir l'explorateur de fichier afin de pouvoir sélectionner les bonnes infos qui seront ensuite intégrées à la commande Robocopy.

    J'avais un script .bat qui faisait cela, mais je galère avec PowerShell pour mettre cela en place.

    Merci d'avance pour votre aide

    En batch :
    Code batch : Sélectionner tout - Visualiser dans une fenêtre à part
    call:FolderSelection "%Source%", Source, "Sélectionnez le dossier Source"
    Code batch : Sélectionner tout - Visualiser dans une fenêtre à part
    call:FolderSelection "%Destination%", Destination, "Sélectionnez le dossier Destination"
    Code batch : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    :FolderSelection <SelectedPath> <folder> <Description>
    SetLocal & set "folder=%~1"
    set "dialog=powershell -sta "Add-Type -AssemblyName System.windows.forms^
    |Out-Null;$f=New-Object System.Windows.Forms.FolderBrowserDialog;$f.SelectedPath='%~1';$f.Description='%~3';^
    $f.ShowNewFolderButton=$true;$f.ShowDialog();$f.SelectedPath""
    for /F "delims=" %%I in ('%dialog%') do set "res=%%I"
    EndLocal & (if "%res%" EQU "" (set "%2=%folder%") else (set "%2=%res%"))
    exit/B 0

  2. #2
    Membre confirmé
    Homme Profil pro
    Technicien réseau
    Inscrit en
    Décembre 2018
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Technicien réseau
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Décembre 2018
    Messages : 49
    Par défaut
    Alors,

    J'ai déjà un début de réponse (du moins, il me semble...).

    Le code ouvre bien une fenêtre pour sélectionner un dossier.
    Maintenant, il faut que je trouve comment enregistrer la première valeur qui sera la source.
    Et certainement ajouter une boucle...

    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    Add-Type -AssemblyName System.Windows.Forms
    $browser = New-Object System.Windows.Forms.FolderBrowserDialog
    $null = $browser.ShowDialog()
    $path = $browser.SelectedPath

    Les idées sont les bienvenues.

  3. #3
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    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 218
    Par défaut
    Ton code est fonctionnel, quel est le problème ?

  4. #4
    Membre confirmé
    Homme Profil pro
    Technicien réseau
    Inscrit en
    Décembre 2018
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Technicien réseau
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Décembre 2018
    Messages : 49
    Par défaut
    Salut ericlm128.

    Pour la partie Powershell, effectivement, ça fonctionne.
    Mais je ne vois pas comment enregistrer la valeur d'une variable pour l'utiliser par la suite dans une commande robocopy.

    En revanche, j'ai travaillé sur le sujet et après avoir farfouiller et fait pas mal de tests, j'en suis là :

    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
     
    # Déclaration de la fonction des boutons
    Function Button_Click1()
    {
        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
     
        $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
        $foldername.Description = "Sélectionnez la Source"
        $foldername.rootfolder = "MyComputer"
        $foldername.SelectedPath = $initialDirectory
     
        if($foldername.ShowDialog() -eq "OK")
        {
            $folder1 += $foldername.SelectedPath
        }
        return $folder
    }
     
    Function Button_Click2()
    {
        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
     
        $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
        $foldername.Description = "Sélectionnez la Destination"
        $foldername.rootfolder = "MyComputer"
        $foldername.SelectedPath = $initialDirectory
     
        if($foldername.ShowDialog() -eq "OK")
        {
            $folder2 += $foldername.SelectedPath
        }
        return $folder
    }
     
    # Création de la forme
     
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
     
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 300
    $Form.height = 200
    $Form.Text = ”IT TOOLS - Robocopy”
    $Form.StartPosition = "CenterScreen"
    $Form.Topmost = $True
     
    # Ajout Premier Bouton
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(30,30)
    $Button.Size = New-Object System.Drawing.Size(100,40)
    $Button.Text = "Source"
     
    # Ajout Second Bouton
    $Button = New-Object System.Windows.Forms.Button
    $Button.Location = New-Object System.Drawing.Size(170,27)
    $Button.Size = New-Object System.Drawing.Size(100,40)
    $Button.Text = "Destination"
     
    #$DropDownFrom = New-Object System.Windows.Forms.ComboBox
    #$DropDownFrom.Location = New-Object System.Drawing.Size(140,10)
    #$DropDownFrom.Size = New-Object System.Drawing.Size(130,30)
     
    ForEach ($Item in $DropDownArray) {
    $Button.Items.Add($Item) | Out-Null
    }
     
    $Form.Controls.Add($Button)
     
    #$DropDownFromLabel = New-Object System.Windows.Forms.Label
    #$DropDownFromLabel.Location = New-Object System.Drawing.Size(10,10)
    #$DropDownFromLabel.Size = New-Object System.Drawing.Size(100,40)
    #$DropDownFromLabel.Text = "Sélectionnez la Source"
    #$Form.Controls.Add($DropDownFromLabel)
     
     
    #$DropDownTo = New-Object System.Windows.Forms.ComboBox
    #$DropDownTo.Location = New-Object System.Drawing.Size(140,50)
    #$DropDownTo.Size = New-Object System.Drawing.Size(130,30)
     
    ForEach ($Item in $DropDownArray) {
    $Button.Items.Add($Item) | Out-Null
    }
     
    $Form.Controls.Add($Button)
     
    #$DropDownToLabel = new-object System.Windows.Forms.Label
    #$DropDownToLabel.Location = new-object System.Drawing.Size(10,50)
    #$DropDownToLabel.size = new-object System.Drawing.Size(100,40)
    #$DropDownToLabel.Text = "Sélectionnez la Destination"
    #$Form.Controls.Add($DropDownToLabel)
     
     
    $OKButton = new-object System.Windows.Forms.Button
    $OKButton.Location = new-object System.Drawing.Size(100,90)
    $OKButton.Size = new-object System.Drawing.Size(100,20)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({
    $Form.DialogResult = "OK"
    $Form.close()
    })
    $form.Controls.Add($OKButton)
     
     
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(100,120)
    $CancelButton.Size = New-Object System.Drawing.Size(100,20)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({
    $Form.DialogResult = "Cancel"
    $Form.close()
    })
    $Form.Controls.Add($CancelButton)
     
     
    $Form.Add_Shown({$Form.Activate()})
     
    $result = $Form.ShowDialog()
    J'ai un bouton (Source) qui n'apparaît pas.

    Je n'ai pas encore progressé sur la partie Robocopy....

    *Si il faut déplacer le sujet => désolé pour le dérangement*

    Pour la faire courte :
    On lance le script .ps1, l'objet s'affiche avec deux boutons.
    Quand on clique sur "Source" ça ouvre l'explorateur de fichier afin de pouvoir sélectionner le bon dossier et idem pour la destination.

    Pour l'instant, seul le bouton destination s'affiche.
    Et comme dit plus haut, le bouton source ne s'affiche pas.


    Code fonctionnel pour appeler la sélection du dossier en source :
    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
     
    Function Button_Click1()
    {
        [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
     
        $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
        $foldername.Description = "Sélectionnez la Source"
        $foldername.rootfolder = "MyComputer"
        $foldername.SelectedPath = $initialDirectory
     
        if($foldername.ShowDialog() -eq "OK")
        {
            $folder += $foldername.SelectedPath
        }
        return $folder
    }
    Function Generate-Form {
     
        Add-Type -AssemblyName System.Windows.Forms    
        Add-Type -AssemblyName System.Drawing
     
        # Build Form
        $Form = New-Object System.Windows.Forms.Form
        $Form.Text = "My Form"
        $Form.Size = New-Object System.Drawing.Size(300,200)
        $Form.StartPosition = "CenterScreen"
        $Form.Topmost = $True
     
        # Add Button
        $Button = New-Object System.Windows.Forms.Button
        $Button.Location = New-Object System.Drawing.Size(35,35)
        $Button.Size = New-Object System.Drawing.Size(120,23)
        $Button.Text = "Source"
     
        $Form.Controls.Add($Button)
     
        #Add Button event 
        $Button.Add_Click({Button_Click1})
     
     
        #Show the Form 
        $form.ShowDialog()| Out-Null 
     
    } #End Function 
     
    #Call the Function 
    Generate-Form

    Si tu sais m'aider sur ce point, ça serait top.
    Merci d'avoir pris le temps de regarder

  5. #5
    Expert confirmé

    Homme Profil pro
    Responsable déploiement (SCCM, InTune, GPO)
    Inscrit en
    Juillet 2014
    Messages
    3 218
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    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 218
    Par défaut
    Il y a des choses que je n'ai pas compris, j'ai supprimé

    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
     # Déclaration de la fonction des boutons
    Function Button_Click1()
    {
        $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
        $foldername.Description = "Sélectionnez la Source"
        $foldername.RootFolder = [System.Environment+SpecialFolder]::MyComputer
     
        if($foldername.ShowDialog() -eq "OK")
        {
            return $foldername.SelectedPath
        }
        return ""
    }
     
    Function Button_Click2()
    {
        $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
        $foldername.Description = "Sélectionnez la Destination"
        $foldername.RootFolder = [System.Environment+SpecialFolder]::MyComputer
     
        if($foldername.ShowDialog() -eq "OK")
        {
            return $foldername.SelectedPath
        }
        return ""
    }
     
    # Création de la forme
     
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
     
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 300
    $Form.height = 200
    $Form.Text = "IT TOOLS - Robocopy"
    $Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
    $Form.Topmost = $True
     
    # Ajout Premier Bouton
    $Button1 = New-Object System.Windows.Forms.Button
    $Button1.Location = New-Object System.Drawing.Size(30,30)
    $Button1.Size = New-Object System.Drawing.Size(100,40)
    $Button1.Text = "Source"
    $Button1.Add_Click({
        $global:source = Button_Click1
    })
    $Form.Controls.Add($Button1)
     
    # Ajout Second Bouton
    $Button2 = New-Object System.Windows.Forms.Button
    $Button2.Location = New-Object System.Drawing.Size(170,27)
    $Button2.Size = New-Object System.Drawing.Size(100,40)
    $Button2.Text = "Destination"
    $Button2.Add_Click({
        $global:destination = Button_Click2
    })
    $Form.Controls.Add($Button2)
     
     
    $OKButton = new-object System.Windows.Forms.Button
    $OKButton.Location = new-object System.Drawing.Size(100,90)
    $OKButton.Size = new-object System.Drawing.Size(100,20)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({
        $Form.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $Form.Close()
    })
    $form.Controls.Add($OKButton)
     
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(100,120)
    $CancelButton.Size = New-Object System.Drawing.Size(100,20)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({
        $Form.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
        $Form.Close()
    })
    $Form.Controls.Add($CancelButton)
     
    $Form.Add_Shown({$Form.Activate()})
     
    $result = $Form.ShowDialog()
     
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        Write-Host "Source : $source"
        Write-Host "Destination : $destination"
    }

    Tu pourrais envisager de ne faire qu'une fonction paramétrable

  6. #6
    Membre confirmé
    Homme Profil pro
    Technicien réseau
    Inscrit en
    Décembre 2018
    Messages
    49
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Pas de Calais (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Technicien réseau
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Décembre 2018
    Messages : 49
    Par défaut
    Salut ericlm128.

    Je regarde en détail ce que tu as fait afin de trouver et comprendre mes erreurs.
    Merci pour ton aide.

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

Discussions similaires

  1. Récupérer un dossier via FTP
    Par Hoopsy dans le forum Langage
    Réponses: 2
    Dernier message: 28/01/2009, 13h12
  2. Sélection dossier ou fichier
    Par rambc dans le forum Tkinter
    Réponses: 2
    Dernier message: 26/12/2008, 23h21
  3. [Système] boîte de dialogue sélection dossier
    Par cuba1393 dans le forum Langage
    Réponses: 1
    Dernier message: 29/03/2007, 23h27
  4. Créer un dossier via une commande PHP
    Par budylove dans le forum Langage
    Réponses: 2
    Dernier message: 27/04/2006, 13h45
  5. Réponses: 2
    Dernier message: 14/03/2006, 17h25

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