2017-09-27 3 views
0

저는 powershell을 처음 사용하기 때문에 스크립트에 대한 도움이 필요합니다.powershell에서 변수가 비어있는 동안 텍스트 상자를 수행하십시오.

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

나는 GUI 버전을 사용하려고하지만 루프가 멈추지 않는다 :

[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) {} 

나는 사용자가 이름을 입력하지 않는 동안 루프 간단한 코드를

나는 뭔가를 놓치고 있다고 생각하지만, 나는 무엇을 모르겠다. ... 내 가난한 영어로 죄송합니다, 미리 감사드립니다.

답변

0

귀하의 텍스트 상자는 호출되는 중일 때 부모에게 전송되지 않습니다. 따라서 반환 값을 지정하고 거기에서 작동해야합니다.

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

[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 
} 
관련 문제