简体   繁体   中英

Powershell Parameter passing issue

I have a strange one I have searched the existing Q&A and haven't found a match. I have written my functions using parameter validation using the basic format

function FunctioName
{
    [CmdletBinding()]
    Param(
    [parameter(Mandatory)]
    [String]$VariableName
    )

When I set the parameter to Mandatory as above I get a parameter binding exception indicating a null value was passed. Running the script in debug I can see the function parameter being passed is not null and is a valid string. When I run the script in the exact same way without the mandatory flag the string is passed into the function and it executes correctly. Has anyone got any ideas, what could be the issue. This problem is affecting a number of functions in my application interestingly it appears that the affected functions all have only a single parameter functions with multiple parameters do not appear to be affected.

Ok thanks guys for your feedback its much appreciated. BTW i am using powershell 5 . Further to the issue, looking into it further I found that the variable was being passed to the function as an array of strings, however an empty string value was being appended into the array which I believe was the cause for the issue. This is where it starts to get interesting, I will need to give a bit more background.

The script I am running queries active directory for user attributes meeting specific conditions, those that match I create an array of strings with each value a delimited value of the user,hostname and other attribute properties. To ensure that I am getting the latest values I use the ASDI GetInfo method,which seems to trigger the odd behavior.

At a high level the functions are

Function GetuserAttr
{
   $inscopeusers = New-Object System.Collections.ArrayList
   $accountlist = (Get-ADUser -Filter { attribute1 -eq "value"} -Properties attribute1).SamAccountName

 foreach ($user in $accountlist)
{
    $DN = getDN($user)   # basically a funtion I wrote to create ASDI object for user account.
    $DN.GetInfo()  # this method call appears to cause issues
    $attr1 = $DN.Get("Attribute1")
    $attr2 = $DN.Get("Attribute2")
    $hoststring = "$($user)|$($attr1)|$($attr2)"
    $inscopeusers.Add($hoststring) > null
}
return $inscopeusers
}

The string array returned in this function is fed into a number of other functions, one of which is the one that was giving the error that I originally brought up. The thing is when I use the GetInfo method the array returned by this function contains several null values in the array, when I remove the command the array has no null strings. Even more strange when I am operating on the array in other functions it appears that the array looses some of its properties when the GetInfo method is used. So for instance I am able to use the foreach loop to iterate through array values but I cannot access an array value by index such as $array[1]. By simply commenting out the GetInfo method call in the function the array returned seems to function normally and you can access array values by index.

I have another function that also uses GetInfo and returns a hash table, when I try to operate on the returned hashtable I cannot access values using a key value such as $hashtable['key'], but I can access them using $hashtable.key. I know this is really weird and can't really think what it could be

Has any one else experienced a similar problem.

You're missing an argument.

Function Test
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [String]
        $Variable
    )

    Write "$Variable"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM