在PowerShell脚本结果中不显示“取消”

时间:2019-04-22 13:15:14

标签: powershell

我有以下PowerShell脚本,该脚本显示文件对话框以选择txt文件。如果用户取消对话框,则提供一个多行文本框

function GetDetails() {
  Add-Type -AssemblyName System.Windows.Forms;
  $browser = New-Object System.Windows.Forms.OpenFileDialog;
  $browser.Filter = "txt (*.txt)|*.txt";
  $browser.InitialDirectory = "E:\";
  $browser.Title = "select txt file";
  $browserResult = $browser.ShowDialog();

  if($browserResult -eq [System.Windows.Forms.DialogResult]::OK) {
    $nfoFile = $browser.FileName;

    if([string]::IsNullOrWhiteSpace($txtFile)) {
      return GetFromForm;
    }

    $txtFile = [System.IO.Path]::ChangeExtension($nfoFile, ".dac");
    $txtFile = $temp + [System.IO.Path]::GetFileName($txtFile);
    $exeArgs = "-f -S `"$txtFile`" -O `"$txtFile`"";

    Start-Process $anExe -ArgumentList $exeArgs -Wait;
    $result = Get-Content $txtFile | Out-String;

    $browser.Dispose();
    return $result;
  } else {
    return GetFromForm;
  }
}


function GetFromForm(){
  Add-Type -AssemblyName System.Windows.Forms;
  $form = New-Object System.Windows.Forms.Form;
  $form.Width = 800;
  $form.Height = 600;

  $txtBox = New-Object System.Windows.Forms.TextBox;
  $txtBox.Multiline = $true;
  $txtBox.AcceptsReturn = $true;
  $txtBox.AcceptsTab = $true;
  $txtBox.Visible = $true;
  $txtBox.Name = "txtName";
  $txtBox.Width = 760;
  $txtBox.Height = 660;

  $form.Controls.Add($txtBox);
  $form.ShowDialog();
 
  $form.Dispose();

  return $txtBox.Text;
}

$desc = GetDetails;
cls;
Write-Host $desc;

这里有两个问题:

  1. Write-Host $desc中,如果用户选择取消对话框,还会打印取消此字符串字符串。如何避免这种情况?

  2. 如果我在ISE中运行脚本,则即使我调用ShowDialog(),生成的表单(在第二个函数中)也将始终位于ISE后面,我希望它充当模式对话框。这是正常的还是有解决办法?

2 个答案:

答案 0 :(得分:0)

您需要在$form.ShowDialog()中禁止输出GetFromForm

$form.ShowDialog()|out-null

Powershell会将在函数/命令行开关中输出到主机的所有内容添加到返回值中。

关于第二个问题-参见此answer

请不要在行尾使用分号。这不是C#,并且会使您误以为该行到此结束,但事实并非如此。

答案 1 :(得分:0)

好,我为提高效率做了一些更改,为功能做了一些更改。阅读脚本中的注释以获取解释。

    <div id="formMain">
    <form id="form_id_1" class="formClass">
        <div id="fullname">
            <p>Full Name</p>
            <input type="text" class="inputClass" name="name" value="Joe">
            <br/>
            <input type="text" class="inputClass" name="name2" value="Doe">
        </div>
        <div id="Address">
            <p>Address</p>
            <input type="text" class="inputClass" name="address" value="1st Maint Street">
        </div>
    </form>
    <form id="form_id_2" class="formClass">
        <div id="fullname">
            <p>Full Name</p>
            <input type="text" class="inputClass" name="name" id="name1" value="Mary">
            <br/>
            <input type="text" class="inputClass" name="name2" id="name2" value="Doe">
        </div>
        <div id="Address">
            <p>Address</p>
            <input type="text" class="inputClass" name="address" id="addressId" value="2nd Maint Street">
        </div>
    </form>
</div>

您可以在此处查看参考资料:https://docs.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-6