唐’;t在PowerShell脚本结果中显示取消
我有以下PowerShell脚本,它显示文件对话框以选择txt文件。如果用户取消对话框,则提供一个多行文本框唐’;t在PowerShell脚本结果中显示取消,powershell,Powershell,我有以下PowerShell脚本,它显示文件对话框以选择txt文件。如果用户取消对话框,则提供一个多行文本框 function GetDetails() { Add-Type -AssemblyName System.Windows.Forms; $browser = New-Object System.Windows.Forms.OpenFileDialog; $browser.Filter = "txt (*.txt)|*.txt"; $browser.InitialDire
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;
我有两个问题:
写入主机$desc
中,如果用户选择取消对话框,也会在此处打印Cancel。如何避免这种情况ShowDialog()
,我希望它的行为像模态对话框一样。这是正常的还是有解决办法您需要在
GetFromForm
中禁止输出$form.ShowDialog()
:
$form.ShowDialog()|out-null
Powershell将向返回值添加在函数/commandlet中输出到主机的所有内容
关于你的第二个问题-见此
请不要在行尾使用分号。这不是C#,会让您误以为行到此结束,但它不是完全正确的。您需要在
GetFromForm
中禁止输出$form.ShowDialog()
:
$form.ShowDialog()|out-null
Powershell将向返回值添加在函数/commandlet中输出到主机的所有内容
关于你的第二个问题-见此
请不要在行尾使用分号。这不是C#,会让你误以为行到此结束,但这不是真的。好的,我对效率和功能做了一些更改。阅读脚本中的注释以获得解释
# Just add types once. There is no need to add the types in each function.
Add-Type -AssemblyName System.Windows.Forms;
Add-Type -AssemblyName System.Drawing
function GetDetails() {
$browser = New-Object System.Windows.Forms.OpenFileDialog;
$browser.Filter = "txt (*.txt)|*.txt";
$browser.InitialDirectory = "E:\";
$browser.Title = "select txt file";
$browserResult = $browser.ShowDialog();
# Combined the if statements
if($browserResult -eq [System.Windows.Forms.DialogResult]::OK -and [string]::IsNullOrWhiteSpace($txtFile) -ne $true) {
$nfoFile = $browser.FileName;
$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;
# The Raw flag should return a string
$result = Get-Content $txtFile -Raw;
$browser.Dispose();
return $result;
}
# No need for else since the if statement returns
return GetFromForm;
}
function GetFromForm(){
$form = New-Object System.Windows.Forms.Form;
$form.Text = 'Adding Arguments'
$form.Size = New-Object System.Drawing.Size(816,600)
$form.StartPosition = 'CenterScreen'
# Added a button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(585,523)
$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)
$txtBox = New-Object System.Windows.Forms.TextBox;
$txtBox.Multiline = $true;
$txtBox.AcceptsReturn = $true;
$txtBox.AcceptsTab = $true;
$txtBox.Visible = $true;
$txtBox.Name = "txtName";
$txtBox.Size = New-Object System.Drawing.Size(660,500)
$form.Controls.Add($txtBox);
# Needed to force it to show on top
$form.TopMost = $true
# Select the textbox and activate the form to make it show with focus
$form.Add_Shown({$txtBox.Select(), $form.Activate()})
# Finally show the form and assign the ShowDialog method to a variable (this keeps it from printing out Cancel)
$result = $form.ShowDialog();
# If the user hit the OK button return the text in the textbox
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
return $txtBox.Text
}
}
$desc = GetDetails;
cls;
Write-Host $desc;
您可以在这里看到参考资料:好的,我对效率和功能做了一些更改。阅读脚本中的注释以获得解释
# Just add types once. There is no need to add the types in each function.
Add-Type -AssemblyName System.Windows.Forms;
Add-Type -AssemblyName System.Drawing
function GetDetails() {
$browser = New-Object System.Windows.Forms.OpenFileDialog;
$browser.Filter = "txt (*.txt)|*.txt";
$browser.InitialDirectory = "E:\";
$browser.Title = "select txt file";
$browserResult = $browser.ShowDialog();
# Combined the if statements
if($browserResult -eq [System.Windows.Forms.DialogResult]::OK -and [string]::IsNullOrWhiteSpace($txtFile) -ne $true) {
$nfoFile = $browser.FileName;
$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;
# The Raw flag should return a string
$result = Get-Content $txtFile -Raw;
$browser.Dispose();
return $result;
}
# No need for else since the if statement returns
return GetFromForm;
}
function GetFromForm(){
$form = New-Object System.Windows.Forms.Form;
$form.Text = 'Adding Arguments'
$form.Size = New-Object System.Drawing.Size(816,600)
$form.StartPosition = 'CenterScreen'
# Added a button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(585,523)
$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)
$txtBox = New-Object System.Windows.Forms.TextBox;
$txtBox.Multiline = $true;
$txtBox.AcceptsReturn = $true;
$txtBox.AcceptsTab = $true;
$txtBox.Visible = $true;
$txtBox.Name = "txtName";
$txtBox.Size = New-Object System.Drawing.Size(660,500)
$form.Controls.Add($txtBox);
# Needed to force it to show on top
$form.TopMost = $true
# Select the textbox and activate the form to make it show with focus
$form.Add_Shown({$txtBox.Select(), $form.Activate()})
# Finally show the form and assign the ShowDialog method to a variable (this keeps it from printing out Cancel)
$result = $form.ShowDialog();
# If the user hit the OK button return the text in the textbox
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
return $txtBox.Text
}
}
$desc = GetDetails;
cls;
Write-Host $desc;
您可以在此处看到参考资料:如果用户在对话框中选择了
取消
按钮,仍然显示取消
,您是否准确复制了我的脚本?当我运行它时,它不会显示cancel。我的答案中应该包含所有内容,以使它按您希望的方式工作。所以你应该可以复制并粘贴它。我也在脚本中添加了注释。所以,当您复制并粘贴到PowerShell中时,它将非常容易阅读。我忘了告诉您,我使用PowerShell 4时,如果用户选择对话框中的取消
按钮,仍然会显示取消
,您是否准确复制了我的脚本?当我运行它时,它不会显示cancel。我的答案中应该包含所有内容,以使它按您希望的方式工作。所以你应该可以复制并粘贴它。我也在脚本中添加了注释。所以,当您复制并粘贴到PowerShell中时,它将非常容易阅读。我忘了告诉您我使用PowerShell4@SnakeEyes在您的示例中,根本不使用此值。我说的是GetFromForm
函数。如果需要,请将其分配给一个变量,不要让ShowDialog
的调用保持未分配状态,否则它将转到标准输出。@SnakeEyes您看到的Cancel
不是因为取消了OpenFile
对话框,而是因为用文本框关闭了表单。@SnakeEyes在您的示例中,您根本不使用这个值。我说的是GetFromForm
函数。如果需要,请将其分配给一个变量,不要让ShowDialog
的调用保持未分配状态,否则它将转到标准输出。@SnakeEyes您看到的Cancel
不是因为取消了OpenFile
对话框,而是因为用文本框关闭了表单。