Function 按下“取消”按钮时读取主机返回的值
据此,当按下Function 按下“取消”按钮时读取主机返回的值,function,powershell,return,exit,Function,Powershell,Return,Exit,据此,当按下取消按钮时,读取主机返回的值为$NULL PS C:\Windows\System32\WindowsPowerShell\v1.0> $a = read-host "please cancel me" ____________________________________________________________________________ PS C:\Windows\System32\WindowsPowerShell\v1.0> if ($A -eq
取消
按钮时,读取主机返回的值为$NULL
PS C:\Windows\System32\WindowsPowerShell\v1.0> $a = read-host "please cancel me"
____________________________________________________________________________
PS C:\Windows\System32\WindowsPowerShell\v1.0> if ($A -eq $null) {'null'}
null
我有一个功能,提示用户输入多对密码,如果用户按下CANCEL
,该功能将退出
if (($password -eq $NULL) -or ($confirmpassword -eq $NULL)){
return
}
不幸的是,脚本退出(因为从未打印过“hello world”)
有什么我遗漏的吗?
读取主机的新字符串对象。Powershell不允许您创建空变量,因为$null
为空。要演示这一点,请在控制台中键入设置变量$a
。您将返回一个错误,该错误基本上告诉您不能将变量绑定为null
您的变量实际上是一个空字符串对象。通过以下方式进行测试:
If($a -eq $null){Write-Host "a equals null"}
If($a -eq ""){Write-Host "a is not null";$a|get-member;$a.GetType()}
第二行代码将生成大量输出。特别值得注意的是:
$a.GetType() | select Name, BaseType
请尝试使用系统字符串类IsNullOrEmpty静态方法,而不是使用$null检查
$a = $null
$b = ""
[String]::IsNullOrEmpty($a)
True
[String]::IsNullOrEmpty($b)
True
$a = $null
$b = ""
[String]::IsNullOrEmpty($a)
True
[String]::IsNullOrEmpty($b)
True