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 :

Script d'un menu graphique qui ne donne pas le bon résultat [PowerShell]


Sujet :

Scripts/Batch

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Août 2019
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Transports

    Informations forums :
    Inscription : Août 2019
    Messages : 3
    Par défaut Script d'un menu graphique qui ne donne pas le bon résultat
    Bonjour à tous,

    J'a récupéré un morceau de script graphique que j'ai modifié et qui doit en fonction du choix exécuter un ensemble de code (lire un fichier, changer une ligne d'adresse IP puis le sauvegarder).
    Mais malheureusement, le résultat est toujours le même.
    Le script envoi sur le premier choix malgré les divers choix sélectionnés, impossible de renvoyer sur les autres choix.

    J'ai eu beau chercher un peu partout, je ne trouve aucune réponse sur cela.

    Si vous pouviez me dire qu'est ce qui cloche se serait TOP !!

    Merci d'avance et bonne journée.

    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
    Add-Type -assembly System.Windows.Forms 
     
    <#01#>
    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "APP WIN x64"
    $Form.Width = 340
    $Form.Height = 220
    $Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
     
    <#Icon#>
    $objIcon = New-Object system.drawing.icon ("C:\APP.ICO")
    $Form.Icon = $objIcon
     
    <#02#>
    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Point(90,05)
    $Label1.Size = New-Object System.Drawing.Size(150,15)
    $Label1.Text = '--- SYSTEM ---'
    $Label1.Font = New-Object System.Drawing.Font("Arial",10,[System.Drawing.FontStyle]::Bold)
    $Form.Controls.Add($Label1)
     
    <#Date#>
    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Location = New-Object System.Drawing.point(260,11) 
    $Label2.Size = New-Object System.Drawing.Size(280,15) 
    $Label2.Text = Get-Date -UFormat "%d-%m-%Y"
    $Label2.Font = New-Object System.Drawing.Font("Arial",07,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label2) 
     
    <#Time#>
    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Location = New-Object System.Drawing.point(284,24) 
    $Label3.Size = New-Object System.Drawing.Size(280,15) 
    $Label3.Text = Get-Date -UFormat "%R"
    $Label3.Font = New-Object System.Drawing.Font("Arial",07,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label3)
     
    <#03#>
    $Label4 = New-Object System.Windows.Forms.Label
    $Label4.Location = New-Object System.Drawing.Point(10,30)
    $Label4.Size = New-Object System.Drawing.Size(280,15)
    $Label4.Text = 'Sélecte APP :'
    $Label4.Font = New-Object System.Drawing.Font("Arial",08,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label4)
     
    <#04 L300xH90#>
    $ListBox = New-Object System.Windows.Forms.ListBox 
    $ListBox.Location = New-Object System.Drawing.Point(10,45) 
    $ListBox.Size = New-Object System.Drawing.Size(300,90) 
    $ListBox.Height = 90
     
    <#05#>
    [void] $ListBox.Items.Add("ACTION 01")
    [void] $ListBox.Items.Add("ACTION 02")
    [void] $ListBox.Items.Add("ACTION 03")
    [void] $ListBox.Items.Add("ACTION 04")
    [void] $ListBox.Items.Add("ACTION 05")
    [void] $ListBox.Items.Add("ACTION 06")
     
    <#OKbutton#>
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,135)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$CSP=$ListBox.SelectedItem;$Form.Close()})
    $Form.Controls.Add($OKButton)
     
    <#OKButon action Valide#>
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$CSP=$ListBox.SelectedItem;$Form.Close()}})
     
    <#Button Cancel#>
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(170,135)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $Form.CancelButton = $CancelButton
    $Form.Controls.Add($CancelButton)
     
    <#Text#>
    $Label5 = New-Object System.Windows.Forms.Label
    $Label5.Location = New-Object System.Drawing.point(10,164) 
    $Label5.Size = New-Object System.Drawing.Size(60,15) 
    $Label5.Text = "Sign"
    $Label5.Font = New-Object System.Drawing.Font("Arial",08,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label5) 
     
    <#Text Adresse Sign#>
    $Label6 = New-Object System.Windows.Forms.Label
    $Label6.Location = New-Object System.Drawing.point(110,167) 
    $Label6.Size = New-Object System.Drawing.Size(260,15) 
    $Label6.Text = "000.000.00"
    $Label6.Font = New-Object System.Drawing.Font("Arial",06,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label6) 
     
    $Form.Controls.Add($ListBox) 
    $Form.Topmost = $True
    $Form.Add_Shown({$Form.Activate()})
    $Form.ShowDialog()
     
    <#Résultat#>
    If ($CSP = "ACTION 01")  
    {
    'ACTION 01'
    (Get-Content -Path "C:\File.txt") |
    ForEach-Object {$_ -replace "Host.+", ("Host =" + " 192.168.0.30)")} |
    Set-Content -Path ("C:\File.txt") -Force;
    }
     
    ElseIf ($CSP = "ACTION 02")  
    {
    'ACTION 02'
    (Get-Content  -Path "C:\File.txt") |
    Foreach-Object {$_ -replace "Host.+", ("Host =" + " 192.168.0.31)")} |
    Set-Content -Path ("C:\File.txt") -Force;
    }
     
    ElseIf ($CSP = "ACTION 03")  
    {
    'ACTION 03'
    (Get-Content  -Path "C:\File.txt") |
    Foreach-Object {$_ -replace "Host.+", ("Host =" + " 192.168.0.32)")} |
    Set-Content -Path ("C:\File.txt") -Force;
    }
     
    ElseIf ($CSP = "ACTION 04")  
    {
    'ACTION 04'
    (Get-Content  -Path "C:\File.txt") |
    Foreach-Object {$_ -replace "Host.+", ("Host =" + " 192.168.0.33)")} |
    Set-Content -Path ("C:\File.txt") -Force;
    }
     
    ElseIf ($CSP = "ACTION 05")  
    {
    'ACTION 05'
    (Get-Content  -Path "C:\File.txt") |
    Foreach-Object {$_ -replace "Host.+", ("Host =" + " 192.168.0.34)")} |
    Set-Content -Path ("C:\File.txt") -Force;
    }
     
    ElseIf ($CSP = "ACTION 06")  
    {
    'ACTION 06'
    (Get-Content  -Path "C:\File.txt") |
    Foreach-Object {$_ -replace "Host.+", ("Host =" + " 192.168.0.35)")} |
    Set-Content -Path ("C:\File.txt") -Force
    }

  2. #2
    Membre chevronné
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Février 2012
    Messages
    281
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Lot et Garonne (Aquitaine)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Industrie

    Informations forums :
    Inscription : Février 2012
    Messages : 281
    Par défaut
    salut le corse

    remplace les "=" par des "-eq" dans tes if
    Code powershell : Sélectionner tout - Visualiser dans une fenêtre à part
    If ($CSP -eq "ACTION 01")

  3. #3
    Candidat au Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Août 2019
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Transports

    Informations forums :
    Inscription : Août 2019
    Messages : 3
    Par défaut
    Salut,

    D'abord, merci de ton aide.
    J'avais essayé avec "eq" comme le proposait ScryptAnalyzer mais pas -eq.

    J'ai donc essayé avec "-eq" mais cela ne lance plus rien, le fichier n'est pas modifié.
    Quand je lance ScryptAnalyzer, il me trouve des erreur avec le $CSP :

    -AVEC "=" :
    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
    RuleName                            Severity     ScriptName Line  Message
    --------                            --------     ---------- ----  -------
    PSUseDeclaredVarsMoreThanAssignment Warning      TEST.ps1   104   The variable 'CSP' is assigned but never used.
    s
    PSUseDeclaredVarsMoreThanAssignment Warning      TEST.ps1   65    The variable 'CSP' is assigned but never used.
    s
    PSUseDeclaredVarsMoreThanAssignment Warning      TEST.ps1   71    The variable 'CSP' is assigned but never used.
    s
    PSUseBOMForUnicodeEncodedFile       Warning      TEST.ps1         Missing BOM encoding for non-ASCII encoded file 'TEST.ps1'
    PSPossibleIncorrectUsageOfAssignmen Information  TEST.ps1   104   Did you mean to use the assignment operator '='? The
    tOperator                                                         equality operator in PowerShell is 'eq'.
    PSPossibleIncorrectUsageOfAssignmen Information  TEST.ps1   112   Did you mean to use the assignment operator '='? The
    tOperator                                                         equality operator in PowerShell is 'eq'.
    PSPossibleIncorrectUsageOfAssignmen Information  TEST.ps1   120   Did you mean to use the assignment operator '='? The
    tOperator                                                         equality operator in PowerShell is 'eq'.
    PSPossibleIncorrectUsageOfAssignmen Information  TEST.ps1   128   Did you mean to use the assignment operator '='? The
    tOperator                                                         equality operator in PowerShell is 'eq'.
    PSPossibleIncorrectUsageOfAssignmen Information  TEST.ps1   136   Did you mean to use the assignment operator '='? The
    tOperator                                                         equality operator in PowerShell is 'eq'.
    PSPossibleIncorrectUsageOfAssignmen Information  TEST.ps1   144   Did you mean to use the assignment operator '='? The
    tOperator                                                         equality operator in PowerShell is 'eq'.
    -AVEC "-eq" :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    RuleName                            Severity     ScriptName Line  Message
    --------                            --------     ---------- ----  -------
    PSUseDeclaredVarsMoreThanAssignment Warning      TEST.ps1   65    The variable 'CSP' is assigned but never used.
    s
    PSUseDeclaredVarsMoreThanAssignment Warning      TEST.ps1   71    The variable 'CSP' is assigned but never used.
    s
    PSUseBOMForUnicodeEncodedFile       Warning      TEST.ps1         Missing BOM encoding for non-ASCII encoded file 'TEST.ps1'
    J'en conclu que le "-eq" supprime les erreurs sur le calcul du résultat mais la variable "CSP" ne sert pas de ce script

    Il doit y avoir un soucis avec cette variable.

  4. #4
    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
    Tu a un problème de porté avec ta variable $CSP

    J'ai fait quelques adaptations en enlevant la modification du fichier host pour les tests

    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
    Add-Type -assembly System.Windows.Forms 
     
    <#01#>
    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "APP WIN x64"
    $Form.Width = 340
    $Form.Height = 220
    $Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
     
    <#Icon#>
    #$objIcon = New-Object system.drawing.icon ("C:\APP.ICO")
    #$Form.Icon = $objIcon
     
    <#02#>
    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Point(90,05)
    $Label1.Size = New-Object System.Drawing.Size(150,15)
    $Label1.Text = '--- SYSTEM ---'
    $Label1.Font = New-Object System.Drawing.Font("Arial",10,[System.Drawing.FontStyle]::Bold)
    $Form.Controls.Add($Label1)
     
    <#Date#>
    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Location = New-Object System.Drawing.point(260,11) 
    $Label2.Size = New-Object System.Drawing.Size(280,15) 
    $Label2.Text = Get-Date -UFormat "%d-%m-%Y"
    $Label2.Font = New-Object System.Drawing.Font("Arial",07,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label2) 
     
    <#Time#>
    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Location = New-Object System.Drawing.point(284,24) 
    $Label3.Size = New-Object System.Drawing.Size(280,15) 
    $Label3.Text = Get-Date -UFormat "%R"
    $Label3.Font = New-Object System.Drawing.Font("Arial",07,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label3)
     
    <#03#>
    $Label4 = New-Object System.Windows.Forms.Label
    $Label4.Location = New-Object System.Drawing.Point(10,30)
    $Label4.Size = New-Object System.Drawing.Size(280,15)
    $Label4.Text = 'Sélecte APP :'
    $Label4.Font = New-Object System.Drawing.Font("Arial",08,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label4)
     
    <#04 L300xH90#>
    $ListBox = New-Object System.Windows.Forms.ListBox 
    $ListBox.Location = New-Object System.Drawing.Point(10,45) 
    $ListBox.Size = New-Object System.Drawing.Size(300,90) 
    $ListBox.Height = 90
     
    <#05#>
    $ListBox.Items.AddRange(@("ACTION 01", "ACTION 02", "ACTION 03", "ACTION 04", "ACTION 05", "ACTION 06"))
     
    <#OKbutton#>
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,135)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $OKButton.Add_Click({
        $Form.Close()
    })
    $Form.Controls.Add($OKButton)
     
    <#OKButon action Valide#>
    $Form.KeyPreview = $True
    $Form.Add_KeyDown({
        if ($_.KeyCode -eq "Enter")
        {
            $Form.DialogResult = [System.Windows.Forms.DialogResult]::OK
            $Form.Close()
        }
    })
     
    <#Button Cancel#>
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(170,135)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $Form.CancelButton = $CancelButton
    $Form.Controls.Add($CancelButton)
     
    <#Text#>
    $Label5 = New-Object System.Windows.Forms.Label
    $Label5.Location = New-Object System.Drawing.point(10,164) 
    $Label5.Size = New-Object System.Drawing.Size(60,15) 
    $Label5.Text = "Sign"
    $Label5.Font = New-Object System.Drawing.Font("Arial",08,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label5) 
     
    <#Text Adresse Sign#>
    $Label6 = New-Object System.Windows.Forms.Label
    $Label6.Location = New-Object System.Drawing.point(110,167) 
    $Label6.Size = New-Object System.Drawing.Size(260,15) 
    $Label6.Text = "000.000.00"
    $Label6.Font = New-Object System.Drawing.Font("Arial",06,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label6) 
     
    $Form.Controls.Add($ListBox) 
    $Form.Topmost = $True
    $Form.Add_Shown({$Form.Activate()})
     
    $result = $Form.ShowDialog()
    $CSP = $ListBox.SelectedItem
     
    if ($result -ne [System.Windows.Forms.DialogResult]::OK -or $CSP -eq $null)
    {
        exit
    }
     
    <#Résultat#>
    If ($CSP -eq "ACTION 01")  
    {
    'ACTION 01'
    }
     
    ElseIf ($CSP -eq "ACTION 02")  
    {
    'ACTION 02'
    }
     
    ElseIf ($CSP -eq "ACTION 03")  
    {
    'ACTION 03'
    }
     
    ElseIf ($CSP -eq "ACTION 04")  
    {
    'ACTION 04'
    }
     
    ElseIf ($CSP -eq "ACTION 05")  
    {
    'ACTION 05'
    }
     
    ElseIf ($CSP -eq "ACTION 06")  
    {
    'ACTION 06'
    }

  5. #5
    Candidat au Club
    Homme Profil pro
    Administrateur systèmes et réseaux
    Inscrit en
    Août 2019
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Administrateur systèmes et réseaux
    Secteur : Transports

    Informations forums :
    Inscription : Août 2019
    Messages : 3
    Par défaut Merci
    Oh trop bien

    Merci à 6ratgus et ericlm128, ça fonctionne super bien.

    Bon week-end.

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

Discussions similaires

  1. [Excel 2007-2010]Requête SQL qui ne donne pas le bon résultat
    Par Davidw dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 24/10/2012, 11h48
  2. requete SQL qui ne donne pas le bon résultat?
    Par siho90 dans le forum Langage SQL
    Réponses: 14
    Dernier message: 24/01/2011, 18h01
  3. conversion de date qui ne donne pas le même résultat
    Par flamant dans le forum Collection et Stream
    Réponses: 3
    Dernier message: 06/12/2010, 04h49
  4. Réponses: 8
    Dernier message: 01/06/2010, 11h43
  5. [MySQL] requete sql qui ne donne pas le bon array
    Par easyjava dans le forum PHP & Base de données
    Réponses: 8
    Dernier message: 02/11/2006, 14h41

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