Function 函数参数总是空的为什么?

Function 函数参数总是空的为什么?,function,powershell,Function,Powershell,有人能告诉我,为什么这个函数调用不起作用,为什么参数总是空的 function check([string]$input){ Write-Host $input #empty line $count = $input.Length #always 0 $test = ([ADSI]::Exists('WinNT://./'+$input)) #exception (empty str

有人能告诉我,为什么这个函数调用不起作用,为什么参数总是空的

function check([string]$input){
  Write-Host $input                             #empty line
  $count = $input.Length                        #always 0
  $test = ([ADSI]::Exists('WinNT://./'+$input)) #exception (empty string) 
  return $test
}

check 'test'
正在尝试获取用户或用户组存在时的信息


致以最诚挚的问候

也许可以使用
param
块作为参数

更新:如果不使用
$input
作为参数名,问题似乎已得到解决,拥有合适的变量名可能不是一件坏事;)

此外,Powershell没有
return
关键字,您只需将对象本身作为语句推送,这将由函数返回:

函数Get ADObjectExists
{
param(
[参数(必需=$true,ValueFromPipeline=$true)]
[字符串]
$ObjectName
)
#仅通过调用对象返回结果(powershell中没有返回语句)
([ADSI]::存在('WinNT://./'+$ObjectName))
}
获取ADObjectExists-ObjectName'test'

$input
是一个自动变量

$Input

包含枚举器,枚举传递给函数的所有输入。
$input
变量仅对函数和脚本块(未命名函数)可用。在函数的进程块中,
$input
变量枚举当前在管道中的对象。当进程块完成时,管道中没有对象,因此
$input
变量枚举一个空集合。如果函数没有进程块,则在结束块中,
$input
变量枚举函数所有输入的集合


我认为,
$input
是一个不应该使用的系统变量,更新后的samplePerfect。将输入更改为其他内容,它就起作用了。非常感谢,事实上PowerShell确实有一个
return
关键字,在某些情况下使用它非常方便:@ManuelBatsching-hah!比我快一秒钟:)哦,乔伊,所以我在
param
块上的
return
关键字是错误的,甚至花了我一段时间才发现
$input
是一个自动变量。。。很糟糕。。。至少我也从中吸取了教训!