如何在不必在Powershell中按Enter键的情况下,只允许在读取主机中输入Y/N?
我试图找到一种方法,让类似于如何在不必在Powershell中按Enter键的情况下,只允许在读取主机中输入Y/N?,powershell,Powershell,我试图找到一种方法,让类似于读取主机的东西询问用户是否要输出到列出的文件。我希望他们按y或n,然后代码继续,而不是先按y/n再按enter。目前,这一切都运作良好,但再次它不完全是我想要的 我尝试查看了Readkey和SendKeys(为用户按Enter键),但这两种方法都不起作用,因为它们似乎只有在用户在读取主机上按Enter键后才能执行。我对Powershell还很陌生,所以我不完全确定它是否真的可行,我花了太多时间在谷歌上搜索/测试,以找到一个有效的答案。如果要使用Write Host或其
读取主机的东西询问用户是否要输出到列出的文件。我希望他们按y
或n
,然后代码继续,而不是先按y/n再按enter。目前,这一切都运作良好,但再次它不完全是我想要的
我尝试查看了Readkey
和SendKeys
(为用户按Enter键),但这两种方法都不起作用,因为它们似乎只有在用户在读取主机上按Enter键后才能执行。我对Powershell还很陌生,所以我不完全确定它是否真的可行,我花了太多时间在谷歌上搜索/测试,以找到一个有效的答案。如果要使用Write Host
或其他方法来执行此操作,它需要不显示在日志中
我已经在下面包含了脚本的必要部分。它基本上会询问用户文件位置是否正确。如果是,则按y
并将其用于输出,否则,如果按n
则加载文件夹浏览器对话框
,供他们选择所需的文件夹
我还应该注意,这都在Tee对象中,因为这段代码决定了Tee对象输出的去向
$OutputYN = Read-Host "Do you want the output file generated to $startDirectory\FolderList.txt? (Y/N)"
If (“y”,”n” -notcontains $OutputYN) {
Do {
$OutputYN = Read-Host "Please input either a 'Y' for yes or a 'N' for no"
} While (“y”,”n” -notcontains $OutputYN)
}
if ($OutputYN -eq "Y") {
$OutputLoc = $startDirectory
}
elseif ($OutputYN -eq "N") {
$OutputLocDir = New-Object System.Windows.Forms.FolderBrowserDialog
$OutputLocDir.Description = "Select a folder for the output"
$OutputLocDir.SelectedPath = "$StartDirectory"
if ($OutputLocDir.ShowDialog() -eq "OK") {
$OutputLoc = $OutputLocDir.SelectedPath
$OutputLoc = $OutputLoc.TrimEnd('\')
}
}
编辑:
我应该更清楚一点。我已经尝试过消息框类型的东西,但我真的更喜欢如果有一种方式,用户键入的y或n。我对用户必须点击的弹出框并不感兴趣。如果不可能,那就这样吧。不确定在控制台中是否可以。但当我需要用户编写指定集合的一个答案时,我会使用do-until循环,如:
Do {
$a = Read-Host "Y / N"
} until ( 'y', 'n' - contains $a )
不确定这在控制台中是否可行。但当我需要用户编写指定集合的一个答案时,我会使用do-until循环,如:
Do {
$a = Read-Host "Y / N"
} until ( 'y', 'n' - contains $a )
Readkey是正确的方法
使用以下内容作为模板
:prompt while ($true) {
switch ([console]::ReadKey($true).Key) {
{ $_ -eq [System.ConsoleKey]::Y } { break prompt }
{ $_ -eq [System.ConsoleKey]::N } { return }
default { Write-Error "Only 'Y' or 'N' allowed!" }
}
}
write-host 'do it' -ForegroundColor Green
:prompt
为外部循环(while)提供一个名称,该名称可在switch语句中使用,通过break prompt
(而不是在switch语句中)直接完全中断
备选方案(适用于Windows):
使用MessageBox
Add-Type -AssemblyName PresentationFramework
$messageBoxResult = [System.Windows.MessageBox]::Show("Do you want the output file generated to $startDirectory\FolderList.txt?" , 'Question' , [System.Windows.MessageBoxButton]::YesNo , [System.Windows.MessageBoxImage]::Question)
switch ($messageBoxResult) {
{ $_ -eq [System.Windows.MessageBoxResult]::Yes } {
'do this'
break
}
{ $_ -eq [System.Windows.MessageBoxResult]::No } {
'do that'
break
}
default {
# stop
return # or EXIT
}
}
Readkey是正确的方法
使用以下内容作为模板
:prompt while ($true) {
switch ([console]::ReadKey($true).Key) {
{ $_ -eq [System.ConsoleKey]::Y } { break prompt }
{ $_ -eq [System.ConsoleKey]::N } { return }
default { Write-Error "Only 'Y' or 'N' allowed!" }
}
}
write-host 'do it' -ForegroundColor Green
:prompt
为外部循环(while)提供一个名称,该名称可在switch语句中使用,通过break prompt
(而不是在switch语句中)直接完全中断
备选方案(适用于Windows):
使用MessageBox
Add-Type -AssemblyName PresentationFramework
$messageBoxResult = [System.Windows.MessageBox]::Show("Do you want the output file generated to $startDirectory\FolderList.txt?" , 'Question' , [System.Windows.MessageBoxButton]::YesNo , [System.Windows.MessageBoxImage]::Question)
switch ($messageBoxResult) {
{ $_ -eq [System.Windows.MessageBoxResult]::Yes } {
'do this'
break
}
{ $_ -eq [System.Windows.MessageBoxResult]::No } {
'do that'
break
}
default {
# stop
return # or EXIT
}
}
试试这个:
$title = 'Question'
$question = 'Do you want the output file generated to $startDirectory\FolderList.txt?'
$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host 'Yes'
} else {
Write-Host 'No'
}
试试这个:
$title = 'Question'
$question = 'Do you want the output file generated to $startDirectory\FolderList.txt?'
$choices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&Yes'))
$choices.Add((New-Object Management.Automation.Host.ChoiceDescription -ArgumentList '&No'))
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host 'Yes'
} else {
Write-Host 'No'
}
如果您在Windows上,则可以执行以下操作:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$result = [System.Windows.Forms.MessageBox]::Show('Do you want the output file generated to $startDirectory\FolderList.txt?' , "Question" , [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question)
if ($result -eq 'Yes') {
"Yes"
}
else
{
"No"
}
如果您在Windows上,则可以执行以下操作:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
$result = [System.Windows.Forms.MessageBox]::Show('Do you want the output file generated to $startDirectory\FolderList.txt?' , "Question" , [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question)
if ($result -eq 'Yes') {
"Yes"
}
else
{
"No"
}
谢谢,但如果你看到我的代码中已经有了,除了我的和你的直到。谢谢,但是如果你看到我的代码中已经有了,除了我的和你的直到。对不起,我的无知,我不知道如何将它合并到我的代码中来工作。请放置你的代码对于“Y”,在“break-prompt”{$OutputLoc=$startDirectory break-prompt}
前面的“Y”的switch语句条件中,也相应地放置“N”的代码,但将“return”替换为“break-prompt”(类似于“Y”),我的意思是更多的问题应该放在哪里,以及:prompt在哪里?我在谷歌上找不到任何使用:prompt
的东西。啊,对不起。把问题放在我的代码前面<代码>:提示符
类似于批处理中的GoTo语句。你可以在这里找到更多信息:不要用“阅读主机”来回答你的问题。使用“Write Host”代替抱歉,我不知道如何将其合并到我的代码中才能工作?将“Y”的代码放在“break prompt”{$OutputLoc=$startDirectory break prompt}
前面的“Y”开关语句条件中,然后将“N”的代码也放在相应位置,但将“return”替换为“break prompt”(如“Y”)我的意思是更多的问题应该放在哪里,:prompt
放在哪里?我在谷歌上找不到任何使用:prompt
的东西。啊,对不起。把问题放在我的代码之前。:prompt
就像一个成批的GoTo语句。你可以在这里找到更多关于它的信息:不要使用”阅读“主机”回答你的问题。使用“写主机”代替。我忘了在我的OP中添加关于已经尝试过这类事情的内容(我现在已经更新了)。这不是我想要的,而是感谢你的回复。嗨,我忘了在我的OP中添加关于已经尝试过这类事情的内容(我现在已经更新了)。这不是我想要的,而是感谢你的回复。