Question GUI marche sur certains ordinateurs/utilisateurs mais pas d'autres

Plus d'informations
il y a 2 ans 1 mois - il y a 2 ans 1 mois #31780 par xGreed
Bonjour tout le monde,

Voici un script que j'ai écrit, comme dis dans le sujet il marche pour certains ordinateurs mais pas pour d'autres et je n'arrive pas à trouver pourquoi.

L'interface graphique ne se lance pas sur les ordinateurs concernés, j'ai bien mon read host et mon get credential qui marchent et même si la première partie avec Find-USERS venait à ne fonctionner, cela ne devrait pas impacter la deuxieme. Donc soit les deux ne fonctionnent pas sur les pc concernés soit seule la deuxième partie est en cause, le problème et que je ne trouve pas pour quelle raison.

Merci pour votre aide !

#############################FIRST PART##########################################
$ErrorActionPreference = 'silentlycontinue'
function Find-USERS {
Param ( [Parameter(Mandatory = $true)]
$Authentification,
[Parameter(Mandatory = $true)]
$Wanted
)

if ($Authentification -eq $null) {
Write-Host "Veuillez vous authentifier"
Pause
exit
}
$test = Get-ADUser -Identity "testuser" -Credential $Authentification
if (-not $test) {
Write-Host "Ce compte n'a pas les droits nécessaires."
} else {
$wanted = $wanted.Split(' ')
$aduser = @()
foreach ($info in $wanted) {
if (($info.Length -eq 6) -and ($info -match '^\d+$')) {
$aduser += Get-ADUser -Properties SamAccountName, GivenName, Surname, Description -Filter "Description -like '$info'" -Credential $Authentification
}
$aduser += Get-ADUser -Properties SamAccountName, GivenName, Surname, Description -Filter "Surname -like '*$info*'" -Credential $Authentification
$aduser += Get-ADUser -Properties SamAccountName, GivenName, Surname, Description -Filter "GivenName -like '*$info*'" -Credential $Authentification
$aduser += Get-ADUser -Properties SamAccountName, GivenName, Surname, Description -Filter "SamAccountName -like '*$info*'" -Credential $Authentification
}
$aduser = Add-Type -AssemblyName System.DirectoryServicesaduser | Select-Object -Unique
$aduser = $aduser | Select-Object -Property SamAccountName, GivenName, Surname, Description
}
return $aduser
}

########################################SECOND PART####################################
function Find-USERSGUI {
Param (
[Parameter(Mandatory = $true)]
$users
)

#region GUI_ListBox
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$ListForm = New-Object System.Windows.Forms.Form
$ListForm.Text = "Chercher un utilisateur."
$ListForm.Size = New-Object System.Drawing.Size(300, 200)
$ListForm.StartPosition = "CenterScreen"
$ListForm.MinimizeBox = $true
$ListForm.MaximizeBox = $false
$ListForm.WindowState = 'Maximized'

$ButtonOk = New-Object System.Windows.Forms.Button
$ButtonOk.Location = New-Object System.Drawing.Point(980, 500)
$ButtonOk.Size = New-Object System.Drawing.Size(75, 23)
$ButtonOk.Text = "OK"
$ButtonOk.DialogResult = [System.Windows.Forms.DialogResult]::OK

$ButtonCancel = New-Object System.Windows.Forms.Button
$ButtonCancel.Location = New-Object System.Drawing.Point(900, 500)
$ButtonCancel.Size = New-Object System.Drawing.Size(75, 23)
$ButtonCancel.Text = "Annuler"
$ButtonCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

$FormLabel = New-Object System.Windows.Forms.Label
$FormLabel.Location = New-Object System.Drawing.Point(820, 400)
$FormLabel.Size = New-Object System.Drawing.Size(260, 20)
$FormLabel.Text = "Voici les utilisateurs correspondant trouvé : "

$ListBox = New-Object System.Windows.Forms.ListBox
$ListBox.Location = New-Object System.Drawing.Size(820, 420)
$ListBox.Size = New-Object System.Drawing.Size(260, 40)
$ListBox.Height = 200
$ListForm.KeyPreview = $True
$ListForm.Add_KeyDown( { if ($_.KeyCode -eq "Enter") { $global:x = $ListBox.SelectedItem; $Listform.Close(); } })
$ListForm.Add_KeyDown( { if ($_.KeyCode -eq "Escape") { $global:x = $null $Listform.Close() } })
$ButtonOk.Add_Click({ $global:x = $ListBox.SelectedItem; $Listform.Close() })
$ButtonCancel.Add_Click({ $global:x = $null; $Listform.Close() })

$ListForm.Controls.Add($ButtonOk)
$ListForm.Controls.Add($ButtonCancel)
$ListForm.Controls.Add($FormLabel)
$ListForm.Controls.Add($ListBox)
$ListForm.TopMost = $True
#endregion GUI_ListBox

if ($users.Count) {
for ($k = 0; $k -lt $users.Count; $k++) {
[void]$ListBox.Items.Add("$($users[$k].GivenName), $($users[$k].Surname), $($users[$k].SamAccountName), $($users[$k].Description)")
}
} else {
[void]$ListBox.Items.Add("$($users.GivenName), $($users.Surname), $($users.SamAccountName), $($users.Description)")
}

$ListForm.add_Shown({ $ListForm.Activate() })
$Result = $ListForm.ShowDialog()

}

$wanted = Read-Host -Prompt "Entrez des information (Identifiant, prénom, nom ou matricule). "
$Authentification = Get-Credential

$test = Find-USERS -Authentification $Authentification -Wanted $wanted
Find-USERSGUI -users $test
Dernière édition: il y a 2 ans 1 mois par xGreed.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 ans 1 mois #31781 par ericlm128
Premièrement, les postes ont ils bien RSAT d'installé ?

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 ans 1 mois - il y a 2 ans 1 mois #31782 par ericlm128
Ca c'est étrange
$aduser = Add-Type -AssemblyName System.DirectoryServicesaduser | Select-Object -Unique

La il manque un ;
$ListForm.Add_KeyDown( { if ($_.KeyCode -eq "Escape") { $global:x = $null $Listform.Close() } })

Essaie ceci, j'ai modifié (ajouté ? ) l'indentation pour essayer de comprendre le code
#############################FIRST PART##########################################
$ErrorActionPreference = 'silentlycontinue'
function Find-USERS {
    Param (
        [Parameter(Mandatory = $true)]
        $Authentification,
        [Parameter(Mandatory = $true)]
        $Wanted
    )

    if ($Authentification -eq $null) {
        Write-Host "Veuillez vous authentifier"
        Pause
        exit
    }

    $test = Get-ADUser -Identity "testuser" -Credential $Authentification
    if (-not $test)
    {
        Write-Host "Ce compte n'a pas les droits nécessaires."
    }
    else
    {
        $wanted = $wanted.Split(' ')
        $aduser = @()
        foreach ($info in $wanted)
        {
            if ($info.Length -eq 6 -and $info -match '^\d+$')
            {
                $aduser += Get-ADUser -Properties SamAccountName, GivenName, Surname, Description -Filter "Description -like '$info'" -Credential $Authentification
            }
            $aduser += Get-ADUser -Properties SamAccountName, GivenName, Surname, Description -Filter "Surname -like '*$info*'" -Credential $Authentification
            $aduser += Get-ADUser -Properties SamAccountName, GivenName, Surname, Description -Filter "GivenName -like '*$info*'" -Credential $Authentification
            $aduser += Get-ADUser -Properties SamAccountName, GivenName, Surname, Description -Filter "SamAccountName -like '*$info*'" -Credential $Authentification
        }
        #$aduser = Add-Type -AssemblyName System.DirectoryServicesaduser | Select-Object -Unique
        $aduser = $aduser | Select-Object -Property SamAccountName, GivenName, Surname, Description
    }
    return @($aduser)
}

########################################SECOND PART####################################
function Find-USERSGUI
{
    Param
    (
        [Parameter(Mandatory = $true)]
        $users
    )

    #region GUI_ListBox
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    $ListForm = New-Object System.Windows.Forms.Form
    $ListForm.Text = "Chercher un utilisateur."
    $ListForm.Size = New-Object System.Drawing.Size(300, 200)
    $ListForm.StartPosition = "CenterScreen"
    $ListForm.MinimizeBox = $true
    $ListForm.MaximizeBox = $false
    $ListForm.WindowState = 'Maximized'

    $ButtonOk = New-Object System.Windows.Forms.Button
    $ButtonOk.Location = New-Object System.Drawing.Point(980, 500)
    $ButtonOk.Size = New-Object System.Drawing.Size(75, 23)
    $ButtonOk.Text = "OK"
    $ButtonOk.DialogResult = [System.Windows.Forms.DialogResult]::OK

    $ButtonCancel = New-Object System.Windows.Forms.Button
    $ButtonCancel.Location = New-Object System.Drawing.Point(900, 500)
    $ButtonCancel.Size = New-Object System.Drawing.Size(75, 23)
    $ButtonCancel.Text = "Annuler"
    $ButtonCancel.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

    $FormLabel = New-Object System.Windows.Forms.Label
    $FormLabel.Location = New-Object System.Drawing.Point(820, 400)
    $FormLabel.Size = New-Object System.Drawing.Size(260, 20)
    $FormLabel.Text = "Voici les utilisateurs correspondant trouvé : "

    $ListBox = New-Object System.Windows.Forms.ListBox
    $ListBox.Location = New-Object System.Drawing.Size(820, 420)
    $ListBox.Size = New-Object System.Drawing.Size(260, 40)
    $ListBox.Height = 200
    $ListForm.KeyPreview = $True
    $ListForm.Add_KeyDown( { if ($_.KeyCode -eq "Enter") { $global:x = $ListBox.SelectedItem; $Listform.Close() } })
    $ListForm.Add_KeyDown( { if ($_.KeyCode -eq "Escape") { $global:x = $null; $Listform.Close() } })
    $ButtonOk.Add_Click({ $global:x = $ListBox.SelectedItem; $Listform.Close() })
    $ButtonCancel.Add_Click({ $global:x = $null; $Listform.Close() })

    $ListForm.Controls.Add($ButtonOk)
    $ListForm.Controls.Add($ButtonCancel)
    $ListForm.Controls.Add($FormLabel)
    $ListForm.Controls.Add($ListBox)
    $ListForm.TopMost = $True
    #endregion GUI_ListBox

    $users | ForEach-Object {[void]$ListBox.Items.Add("$($_.GivenName), $($_.Surname), $($_.SamAccountName), $($_.Description)")}

    $ListForm.add_Shown({ $ListForm.Activate() })
    $Result = $ListForm.ShowDialog()

}

$wanted = Read-Host -Prompt "Entrez des information (Identifiant, prénom, nom ou matricule). "
$Authentification = Get-Credential

$test = Find-USERS -Authentification $Authentification -Wanted $wanted
Find-USERSGUI -users $test
Dernière édition: il y a 2 ans 1 mois par ericlm128.
Les utilisateur(s) suivant ont remercié: xGreed

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 ans 1 mois - il y a 2 ans 1 mois #31783 par ericlm128
Je n'ai pas d'AD sur ce poste, j'ai donc fait ceci et ca affiche quelque chose
...
#$wanted = Read-Host -Prompt "Entrez des information (Identifiant, prénom, nom ou matricule). "
#$Authentification = Get-Credential

#$test = Find-USERS -Authentification $Authentification -Wanted $wanted
$simulate = 1..10 | ForEach-Object {
    [PSCustomObject]@{
        GivenName = "GivenName $($_)"
        Surname       = "Surname $($_)"
        SamAccountName   = "SamAccountName $($_)"
        Description      = "Description $($_)"
    }
}

Find-USERSGUI -users $simulate
 
Dernière édition: il y a 2 ans 1 mois par ericlm128.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 ans 1 mois #31790 par xGreed
Je ne me rappelle plus pourquoi le $aduser = add-type pour être honnête, cela marche sans je l'ai donc enlevé. Sinon oui les outils RSAT sont bien installés sur tout les postes essayés. J'ai essayé ton code, encore une fois il ne marche pas sur les même postes que précédemment.

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 2 ans 1 mois #31791 par ericlm128
Même avec cette modification pour inhiber la fonction AD. Tu peux alors, aussi, la mettre en "remarque" ?
www.powershell-scripting.com/index.php/f...s-pas-d-autres#31783

Sinon il va falloir partir en debug sur les postes en questions pour découvrir pourquoi.

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.079 secondes
Propulsé par Kunena