Powershell 检查传递的参数是否为字符串和整数
我不熟悉powershell脚本。我应该检查传递的第一个参数是否为字符串,第二个参数是否为intPowershell 检查传递的参数是否为字符串和整数,powershell,parameters,Powershell,Parameters,我不熟悉powershell脚本。我应该检查传递的第一个参数是否为字符串,第二个参数是否为int function positions { param ( [string] $inputstring, [int] $num ) } $inputstring=read-host "Enter your name" if ( !$inputstring -eq "" ) { Write-Output "The first recieved param
function positions {
param (
[string] $inputstring,
[int] $num )
}
$inputstring=read-host "Enter your name"
if ( !$inputstring -eq "" ) {
Write-Output "The first recieved parameter is: " $inputstring "and its type is a string"}
else { Write-Output "The first received parameter is:" $inputstring "and its type is not a string" }
$num=read-Host "Enter a number"
if ( $num -eq int ) {
Write-Output "This second parameter is" $num "and its type is a integer"}
else { Write-Output "This second parameter is" $num "and its type is not a integer"}
我相信字符串的if语句是错误的,因为只有当我用“!”来否定它时,它才会给我正确的输入
另外,对于int,if语句,它在-eq之后没有读取'int'
我对此非常陌生,因此需要帮助。您可以使用GetType()
检查类型是否相等时,请在类型周围加上方括号([string]
):
来自Read Host
的值始终是字符串,因此验证$num
是否为int
的一个选项是将其转换为int
。但是,如果输入实际上不是整数,则会引发异常,因此可以使用try catch
块而不是if else
:
$num = read-Host "Enter a number"
try {
# cast to int
$num = [int]$num
# $num is type int (Int32)
}
catch
{
# $num is not an int
}
首先,当您使用
read host
从屏幕获取输入时,无论您输入什么,输入都将被读取并存储为字符串。您可以通过运行以下命令并输入数字来确认这一点:
($checkInt=read host).GetType().Name
这将输出字符串,无论您输入什么。等效方法是如下定义输入变量:
$checkInt=“10”
同样,如果您检查该变量的类型,它将返回字符串
您需要做的是将转换为int并检查值是否为null或检查值是否为数字:
## Checking if user input is alphanumeric
$stringIntVariable = Read-Host "Enter a number"
if (($stringIntVariable -as [int]) -ne $null) {
"StringIntVariable is numeric"
}
else {
"StringIntVariable is not numeric"
}
关于您的代码,以下内容将按照您的要求进行:
$inputstring = read-host "Enter your name"
if (($inputstring -as [int]) -eq $null) { ## Check if not castable to int
Write-Output "The first recieved parameter is: " $inputstring "and its type is a string"
}
else {
Write-Output "The first received parameter is:" $inputstring "and its type is not a string"
}
$num=read-Host "Enter a number"
## Checking if user input is numeric
if (($num -as [int]) -ne $null) {
Write-Output "This second parameter is" $num "and its type is a integer"
}
else {
Write-Output "This second parameter is" $num "and its type is not a integer"
}
正如所指出的那样,使用
($num-As[int])-ne$null
比使用正则表达式匹配更为宽容。难道$InputString-is[string]
&$num-is[int]
更有意义吗?它仍然会返回false,因为他从用户那里得到数字输入,所以它会返回字符串。他需要检查输入是否正确alphanumeric@Lee_Dailey-is
在我使用的powershell版本上不起作用@哦,是的,我没想过!它将是一个字符串,因此需要首先转换为int
,我猜如果使用该逻辑,您的If($num.GetType()-eq[int])
测试也会失败。@OwainEsau-arg!我完全错过了。。。谢谢你提醒我![grin]您还可以将第二项强制转换为int,并查看它是否为nullif(($num-as[int])-ne$null){“第二个参数是int”}否则{“第二个参数不是int”}
。如果PowerShell可以将输入解释为整数,则这会略显宽容,因为它会忽略一些空格,但仍然只返回$true
。请编辑答案。出于某种原因,如果第一个空格前的第一个字符是数字,则使用该正则表达式返回true。你的施法没有。谢谢大家。我理解读取主机总是将输入作为字符串接收的部分。我运行了代码,当read host要求输入一个名称时,我输入了一个整数,它会说这是一个字符串,而不是说这不是一个字符串。这也是因为它将以字符串形式读取输入。也许我还需要一个else-if语句。检查字符串变得有点困难,因为无论输入什么都是字符串。我想您可以将if语句更改为if(($inputstring-as[int])-eq$null){
,这只是检查输入是否不能强制转换为int类型(请参见编辑)
## Checking if user input is alphanumeric
$stringIntVariable = Read-Host "Enter a number"
if (($stringIntVariable -as [int]) -ne $null) {
"StringIntVariable is numeric"
}
else {
"StringIntVariable is not numeric"
}
$inputstring = read-host "Enter your name"
if (($inputstring -as [int]) -eq $null) { ## Check if not castable to int
Write-Output "The first recieved parameter is: " $inputstring "and its type is a string"
}
else {
Write-Output "The first received parameter is:" $inputstring "and its type is not a string"
}
$num=read-Host "Enter a number"
## Checking if user input is numeric
if (($num -as [int]) -ne $null) {
Write-Output "This second parameter is" $num "and its type is a integer"
}
else {
Write-Output "This second parameter is" $num "and its type is not a integer"
}