The function parameter is always empty, why?
Can someone tell me why this function call is not working and why the argument is always empty?
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'
An attempt was made to retrieve information if a user or user group exists.
Regards
source to share
Maybe use parameter param
for parameters.
Update : the problem seems to be fixed, if you don't use $input
as parameter name it might not be bad to have correct variable names;)
Also Powershell doesn't have a keyword return
, you just push the object as operator yourself, this will be returned by the function:
function Get-ADObjectExists { param( [Parameter(Mandatory=$true, ValueFromPipeline=$true)] [string] $ObjectName ) #return result by just calling the object (no return statement in powershell) ([ADSI]::Exists('WinNT://./'+$ObjectName)) } Get-ADObjectExists -ObjectName'test'
source to share
$input
is an automatic variable.
https://technet.microsoft.com/ru-ru/library/hh847768.aspx
$Input
Contains an enumerator that lists all the input that is passed to the function. The variable
$input
is only available for functions and script blocks (which are unnamed functions). In the Process block of the function, the variable$input
lists the object that is currently in the pipeline. When the Process block finishes, there are no objects left in the pipeline, so the variable$input
enumerates an empty collection. If the function does not have a Process block, then in the End block the variable$input
lists the collection of all inputs to the function.
source to share