2017-09-27 7 views
0

Je suis nouveau à powershell et j'ai besoin d'aide avec un script.Est-ce que la zone de texte alors que la variable est vide dans powershell

J'ai un code simple qui boucle pendant que l'utilisateur ne tapez pas un nom:

do {$name = Read-Host "Choose a name "} 
    while (!$name) {} 

J'essaie de l'utiliser pour la version GUI, mais la boucle ne s'arrête pas:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$box = { 

$Form = New-Object System.Windows.Forms.Form 
$Form.Text = "Hostname" 
$Form.Size = New-Object System.Drawing.Size(270,150) 
$Form.StartPosition = "CenterScreen" 

$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Size(165,75) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK 
$Form.AcceptButton = $OKButton 
$Form.Controls.Add($OKButton) 

$Label = New-Object System.Windows.Forms.Label 
$Label.Location = New-Object System.Drawing.Size(10,15) 
$Label.Size = New-Object System.Drawing.Size(280,20) 
$Label.Text = "Choose a name :" 
$Form.Controls.Add($Label) 

$TextBox = New-Object System.Windows.Forms.TextBox 
$TextBox.Location = New-Object System.Drawing.Size(10,40) 
$TextBox.Size = New-Object System.Drawing.Size(230,20) 
$Form.Controls.Add($TextBox) 

$Form.Topmost = $True 

$Form.Add_Shown({$TextBox.Select()}) 
$result = $Form.ShowDialog() 
return $TextBox.Text 

} 

do {&$box} 
    while (!$TextBox.Text) {} 

Je pense qu'il me manque quelque chose, mais je ne sais pas quoi ... Désolé pour mon mauvais anglais, merci d'avance.

Répondre

0

Votre zone de texte, appelée, n'est jamais transmise à son parent. Par conséquent, vous devez affecter votre valeur de retour et fonctionne à partir de là.

do {$value = &$box } 
while ([String]::IsNullOrWhiteSpace($value)) 
Write-Host $value -ForegroundColor Cyan 
0

Essayez-le.

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

# Null out name value in case you need to call the script multiple times in the same PS session. 
$name = $null 

$box = { 

$Form = New-Object System.Windows.Forms.Form 
$Form.Text = "Hostname" 
$Form.Size = New-Object System.Drawing.Size(270,150) 
$Form.StartPosition = "CenterScreen" 

$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Size(165,75) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK 
$Form.AcceptButton = $OKButton 
$Form.Controls.Add($OKButton) 

$Label = New-Object System.Windows.Forms.Label 
$Label.Location = New-Object System.Drawing.Size(10,15) 
$Label.Size = New-Object System.Drawing.Size(280,20) 
$Label.Text = "Choose a name :" 
$Form.Controls.Add($Label) 

$TextBox = New-Object System.Windows.Forms.TextBox 
$TextBox.Location = New-Object System.Drawing.Size(10,40) 
$TextBox.Size = New-Object System.Drawing.Size(230,20) 
$Form.Controls.Add($TextBox) 

$Form.Topmost = $True 

$Form.Add_Shown({$TextBox.Select()}) 
$result = $Form.ShowDialog() 

# Only return if the TextBox.Text is set to stop it from exiting immediately after rendering the form. 
if ($TextBox.Text) {return $TextBox.Text} 

} 

# While the name variable is null, show the form again. 
while (-not $name) { 
    $name = & $box 
}