Powershell 时区是真是假

Powershell 时区是真是假,powershell,if-statement,Powershell,If Statement,我想提示用户输入时区。然后,如果服务器设置为该时区,则返回true(如果不是false)。我不明白为什么我的代码总是返回false 提前谢谢 cls $InTimeZone = Read-Host -Promt "What Time Zone should this server be in?" $HostName = $env:Computername $ServerInfo = Get-WmiObject -Class win32_timezone -ComputerName $Hos

我想提示用户输入时区。然后,如果服务器设置为该时区,则返回true(如果不是false)。我不明白为什么我的代码总是返回false

提前谢谢

cls

$InTimeZone = Read-Host -Promt "What Time Zone should this server be in?"

$HostName = $env:Computername

$ServerInfo = Get-WmiObject -Class win32_timezone -ComputerName $HostName

$TimeZone = $ServerInfo.Caption 

If ($InTimeZone -like $TimeZone ){$ResTimeZone = "Validation Passed: Server time zone is $TimeZone"}
Else {$ResTimeZone = "Nope."}

$TimeZone

$ResTimeZone

这里有两件事是不正确的

  • 这种比较是倒退的
  • 您使用的比较不正确
  • 更好的解决办法是:

    If ($TimeZone -match $InTimeZone) {
        $ResTimeZone = "Validation Passed: Server time zone is $TimeZone"
    } Else {
        $ResTimeZone = "Nope."
    }
    

    谢谢大师!这让我快发疯了。现在我看它,它似乎很简单。