使用文件内容的读取主机验证
Read-Host verification using a files contents
我正在尝试执行 read-host
命令,该命令只接受与文本文件中列出的主机名匹配的输入。下面是我正在使用的代码,有人可以帮忙吗?
它试图匹配整个文本文件,而不仅仅是其中的任何一行,根本不需要区分大小写:
$Input = Get-Content -Path "C:\Users\username\Computer-Results.txt"
Do {
Try {
$HostnameOK = $True
$HostnameEntry = Read-Host "Enter something"}
Catch { $HostnameOK = $False
Write-Host "This is the catch."}
}
Until (($HostnameEntry -contains $Input))
$Input
你的条件倒退了。
Until (($HostnameEntry -contains $Input))
应该是
Until ($fileInput -contains $HostnameEntry)
如果您至少有 PowerShell 3.0,-in
运算符会更直观一些
Until ($HostnameEntry -in $fileInput)
正如其他答案和评论指出的那样,您正在使用 automatic variable。更改变量名称。
$Input
:
Contains an enumerator that enumerates all input that is passed to a
function. The $input variable is available only to functions and script
blocks (which are unnamed functions). In the Process block of a
function, the $input variable enumerates the object that is currently
in the pipeline. When the Process block completes, there are no objects
left in the pipeline, so the $input variable enumerates an empty
collection. If the function does not have a Process block, then in the
End block, the $input variable enumerates the collection of all input to
the function.
发生这种情况是因为 $Input
是默认的 PowerShell 变量。您应该使用另一个变量来添加文件的内容。类似于:
$fileinput = Get-Content -Path "C:\Users\username\Computer-Results.txt"
do {
$HostnameEntry = Read-Host "Enter Something"
} until ($fileinput -contains $HostnameEntry)
尝试这样的事情。
$Computers = Get-Content -Path 'C:\Users\username\Computer-Results.txt'
do {
$HostnameEntry = Read-Host 'Enter something'
if ($Computers -notcontains $HostnameEntry)
{
Write-Host "Sorry, [$($HostnameEntry)] isn't acceptable"
pause
}
} while ($Computers -contains $HostnameEntry)
我正在尝试执行 read-host
命令,该命令只接受与文本文件中列出的主机名匹配的输入。下面是我正在使用的代码,有人可以帮忙吗?
它试图匹配整个文本文件,而不仅仅是其中的任何一行,根本不需要区分大小写:
$Input = Get-Content -Path "C:\Users\username\Computer-Results.txt"
Do {
Try {
$HostnameOK = $True
$HostnameEntry = Read-Host "Enter something"}
Catch { $HostnameOK = $False
Write-Host "This is the catch."}
}
Until (($HostnameEntry -contains $Input))
$Input
你的条件倒退了。
Until (($HostnameEntry -contains $Input))
应该是
Until ($fileInput -contains $HostnameEntry)
如果您至少有 PowerShell 3.0,-in
运算符会更直观一些
Until ($HostnameEntry -in $fileInput)
正如其他答案和评论指出的那样,您正在使用 automatic variable。更改变量名称。
$Input
: Contains an enumerator that enumerates all input that is passed to a function. The $input variable is available only to functions and script blocks (which are unnamed functions). In the Process block of a function, the $input variable enumerates the object that is currently in the pipeline. When the Process block completes, there are no objects left in the pipeline, so the $input variable enumerates an empty collection. If the function does not have a Process block, then in the End block, the $input variable enumerates the collection of all input to the function.
发生这种情况是因为 $Input
是默认的 PowerShell 变量。您应该使用另一个变量来添加文件的内容。类似于:
$fileinput = Get-Content -Path "C:\Users\username\Computer-Results.txt"
do {
$HostnameEntry = Read-Host "Enter Something"
} until ($fileinput -contains $HostnameEntry)
尝试这样的事情。
$Computers = Get-Content -Path 'C:\Users\username\Computer-Results.txt'
do {
$HostnameEntry = Read-Host 'Enter something'
if ($Computers -notcontains $HostnameEntry)
{
Write-Host "Sorry, [$($HostnameEntry)] isn't acceptable"
pause
}
} while ($Computers -contains $HostnameEntry)