Do-Until循环询问两次值

时间:2018-09-24 13:16:16

标签: powershell switch-statement do-while write-host

问题出在DoDotil循环上。 脚本必须使用URL,而不是询问如何处理他,并在用户选择数字后给出结果(1-4) 然后输出,再次给菜单,用户可以选择另一个数字,如果他想要或完成脚本输入“ X”。

但是此脚本的工作方式不同,在我选择号码之后,它询问URL,然后再询问URL和号码,然后询问(例如,如果我选择相同的两次,数字),它将为我带来2个网址的值。

请提出建议,我在做错什么。

write-host ""
write-host "Past URL that you need to check"
write-host ""
write-host -nonewline "Type your URL and press Enter: "
$URLok = read-host
write-host ""

if ($URLok.Length -lt 1) {
    write-host "Invalid selection"
}

do {
    write-host "What do you want to do with this URL?"
    write-host ""
    write-host "DNS - Selection 1"
    write-host " MX - Selection 2"
    write-host " NS - Selection 3"
    write-host "TXT - Selection 4"
    write-host ""
    write-host "X - Exit"
    write-host ""
    write-host -nonewline "Type your choice and press Enter: "

    $choice = read-host

    $ok = $choice -match '^[1234x]+$'

    if ( -not $ok) {
        write-host "Invalid selection"
    }

    switch -Regex ( $choice ) {
        "1"
        {
            [System.Net.Dns]::resolve($URLok)
        }

        "2"
        {
            Resolve-DnsName -Name $URLok -Type MX -DnsOnly | select Name,Type,NameExchange
        }

        "3"
        {
            Resolve-DnsName -Name '888ladies.com' -Type NS -DnsOnly | select Name,Type,NameHost,PrimaryServer

        }

        "4"
        {
            Resolve-DnsName -Name $URLok -Type TXT | select Name,Type,Strings
        }

        "5"
        {
            write-host ""
            write-host "Past URL that you need to check"
            write-host ""
            write-host -nonewline "Type your URL and press Enter: "
            $URLok = read-host
            write-host ""
        }
    }
} while($choice -ne "X")

P.s。还有一点要说的是,在2-3-4选项中我有'Resolve-DNSName',但是在循环doUntil完成之前,该脚本不会在控制台上显示。因此,只有选择“ X”并完成循环,我才能看到结果

1 个答案:

答案 0 :(得分:0)

答案是Format-Table点,而且有效!

例如:

Resolve-DnsName -Name $URLok -Type NS -DnsOnly | select Name,Type,NameHost,PrimaryServer| Format-Table