Bonjour à tous,

Help me please

L’objectif de ce script est de réinitialiser des mots de passe de compte locaux utilisés pour une application métier sur un serveur distant.

J'ai déjà un script fonctionnel, mais sans interface graphique, donc j'ai décidé dans faire une.

Mon problème : Le script lors de la commande envoyée au serveur distant (
Code : Sélectionner tout - Visualiser dans une fenêtre à part
Invoke-Command –ComputerName $Server...
) ne trouve pas mes variables de mot de passe inscrites dans l'interface graphique et donc il ne réinitialise pas le compte.

Voici le code source du script :

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
# Chargement des assemblies :

Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName system.windows.forms
Add-Type -AssemblyName system.drawing

[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

# Création de l'interface graphique WPF :

[xml] $xaml = @'
<Window
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="QIS change password :" Height="300" Width="500">
    <Grid x:Name="background" Background="#FF1D3245" HorizontalAlignment="Left" Width="500">
        <Button x:Name="reset" Content="Reset" HorizontalAlignment="Left" Margin="208,214,0,0" VerticalAlignment="Top" Width="75"/>
        <PasswordBox x:Name="password" HorizontalAlignment="Left" Margin="270,81,0,0" VerticalAlignment="Top" Width="134"/>
        <PasswordBox x:Name="confirm" HorizontalAlignment="Left" Margin="270,142,0,0" VerticalAlignment="Top" Width="134"/>
        <TextBox x:Name="user" HorizontalAlignment="Left" Height="20" Margin="62,114,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="134" IsReadOnly="False"/>
        <Label Content="Please enter your new password :" HorizontalAlignment="Left" Margin="252,50,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Label Content="Confirm new password :" HorizontalAlignment="Left" Margin="267,113,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Label Content="Username :" HorizontalAlignment="Left" Margin="62,88,0,0" VerticalAlignment="Top" Foreground="White"/>
    </Grid>
</Window>
'@


#Déclarations des objets de la forme :

$reader = New-Object system.xml.xmlnodereader $xaml
$Form = [windows.markup.xamlreader]::Load($reader)

# Déclarations des variables de l'interface graphique :

$reset = $form.Findname('reset')
$pass = $form.Findname('password') 
$confirm = $form.Findname('confirm')
$user = $form.Findname('user')


################ Déclaration variables ##################

[string]$Server =  "server"
[string]$pass.Password
[string]$confirm.Password 
[string]$user.set_text("user")

Write-Host "Current connection : $Server" -ForegroundColor Green

# Autre méthode de fonctionnement de connexion automatique :

$login = "login"
$password = 'motdepasse' | Convertto-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.Pscredential -Argumentlist $login,$password

# Autre méthode de connexion au serveur :

$Server =  "server"
$Session = New-PSSession –ComputerName $Server -Credential $credential

 $reset.Add_Click({

Invoke-Command –ComputerName $Server -Credential $credential -ScriptBlock {# Demande de création du nouveau mot de passe :

Write-Host $pass.Password 
Write-Host $confirm.Password 
Write-Host $user.get_text()

function ConvertTo-String {

  param(

    [System.Security.SecureString] $secureString

  )
  $marshal = [System.Runtime.InteropServices.Marshal]
  try {

    $intPtr = $marshal::SecureStringToBSTR($secureString)
    $string = $marshal::PtrToStringAuto($intPtr)
  }

  finally {
    if ( $intPtr ) {
      $marshal::ZeroFreeBSTR($intPtr)

    }

  }

  $string
}

# Demande nouveau mot de passe plus confirmation :

do {
$server = $env:computername
$user = "user"
Write-host "change password"
$pwd = $pass.password
$pwdconf = $confirm.Password
$ok = (ConvertTo-String $pwd) -ceq (ConvertTo-String $pwdconf)

if (-not $ok ) {

    write-host "The passwords do not match"

  }

}

until ($ok)

$pwd = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd))
$pwdconf = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwdconf))

# changement du mot de passe sur server :

try {

$creer = [ADSI]"WinNT://$server/$user,user"
$creer.SetPassword($pwd)
$creer.ADsPath

# Message de confirmation du reset mot de passe :

Write-Host "Your password has been reset" -ForegroundColor Green

  }

catch { 

# Message d'erreur si reset non effectué :

Write-host "Error - Please contact your Administrator" -ForegroundColor Red

 Start-Sleep 7

 }}
 })


# Affichage de la Windows
$Form.ShowDialog() | Out-Null
Peut être un problème de portée ?

Si quelqu'un a une idée ?

Merci d'avance,

Bonne journée a tous,