因此,我正在尝试编写一个将DNS转发器设置为2个预设IP的脚本,但是如果用户想选择其他IP,则他只需要在提示中给予它们。
Write-Host " "
Write-Host "DNS Forwarders are set on -192.168.20.3 & 168.192.24.3- want to choose these?"
$Antw = Read-Host -Prompt 'y/n'
If ($Antw.ToLower() = "n")
{
$ip1 = Read-Host -Prompt 'DNS Forwarder 1: '
$ip2 = Read-Host -Prompt 'DNS Forwarder 2: '
C:WindowsSystem32dnscmd.exe $hostname /resetforwarders $ip1, $ip2
}
Elseif ($Antw.ToLower() = "y")
{
C:WindowsSystem32dnscmd.exe $hostname /resetforwarders 192.168.20.3, 168.192.24.3
}
#Write-Host $Antw
我的if/elseif似乎不起作用,但是,如果我按" y",它仍然要求2个IP?我的代码怎么了?
谢谢
这是那些对PowerShell完全不满意的人的常见错误。PowerShell中的比较不是与经典操作员符号进行的;您必须使用" fortran风格"操作员:
Write-Host " "
Write-Host "DNS Forwarders are set on -192.168.20.3 & 168.192.24.3- want to choose these?"
$Antw = Read-Host -Prompt 'y/n'
If ($Antw.ToLower() -eq "n")
{
$ip1 = Read-Host -Prompt 'DNS Forwarder 1: '
$ip2 = Read-Host -Prompt 'DNS Forwarder 2: '
C:WindowsSystem32dnscmd.exe $hostname /resetforwarders $ip1, $ip2
}
Elseif ($Antw.ToLower() -eq "y")
{
C:WindowsSystem32dnscmd.exe $hostname /resetforwarders 192.168.20.3, 168.192.24.3
}
#Write-Host $Antw
比较操作员
-eq Equal
-ne Not equal
-ge Greater than or equal
-gt Greater than
-lt Less than
-le Less than or equal
-like Wildcard comparison
-notlike Wildcard comparison
-match Regular expression comparison
-notmatch Regular expression comparison
-replace Replace operator
-contains Containment operator
-notcontains Containment operator
-shl Shift bits left (PowerShell 3.0)
-shr Shift bits right – preserves sign for signed values. (PowerShell 3.0)
-in Like –contains, but with the operands reversed.(PowerShell 3.0)
-notin Like –notcontains, but with the operands reversed.(PowerShell 3.0)