none
Multiple arguments for Function

    Question

  • Hello All,

    Greeting.
    I'm working on script to bulk update any attribute of the AD User. Created the script, but have a problem while passing a value to function
                    - Now doing the testing with updating the attribute "streetaddress"
                    - Need to pass three arguments to function
                    - Input file will have dn value, attribute to update and the value to be updated
                    - Trying to update one user

    Script is like this:


    param([string]$Whatif)
    $CURRENTPATH = Split-Path $MyInvocation.MyCommand.Path
    $SCRIPTNAME = $MyInvocation.MyCommand.Name
    $DATE = [System.DateTime]::Now.ToString('yyyyMMdd-Hmm')

    Function Set-attribute($oUser, $attribute, $ovalue)
    {
        $omail = $oUser.mail
        Write-Host "# Inside the Function #"
        Write-Host "Function: User Mail is "$omail""
        Write-Host "Function: User Mail is $oUser.mail"
        Write-Host "Function: Attribute Passed is $attribute"
        Write-Host "Function: Value is $oValue"
        Write-Host "# Inside the Function #"
                    $oldvalue = $oUser.($attribute)
                    $newvalue = $ovalue
        if (($newvalue -eq $null) -or ($newvalue -eq ""))
                    {
                                    $oUser.$attribute.Clear()
                    }
                    else
                    {
                                    $oUser.$attribute = $newvalue
                    }              
    }


    ##Main
    .{
        $DATE = [System.DateTime]::Now.ToString('yyyyMMdd-Hmm')
        $INPUTFILE = $CURRENTPATH + "\" + "Inputtest.csv"
        $OUTPUTFILE = $CURRENTPATH + "\" + "OutputResult.csv"
        $domaincontroller = "ADSTest01.test.com"
        
        $inp = Import-csv $INPUTFILE
                    $output = @()
        
        foreach ($user in $inp)
                    {
            $dn = $user.dn
                                    $fetchcode = -1
                                    $mailid = $user.mail
            $attr = $user.attribute
            $value = $user.value
            $result = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$DOMAINCONTROLLER/$dn")
            Write-Host "# Value of Variable Result #"
            $result
            Write-Host "# Values before passing to function #"
            Write-Host "User Mail is $($result.mail)"
            Write-Host "Attribute Passed is $attr"
            Write-Host "Value is $Value"
            
            Set-Attribute "$result" "$attr" "$value"
            if($Whatif -eq "False")
                   {
                   Write-Host "Commiting the changes for user - "
                   $result.CommitChanges()
                   }
          }
    }

    Friday, April 28, 2017 10:41 AM

All replies

  • Hello All,

    Greeting.
    I'm working on script to bulk update any attribute of the AD User. Created the script, but have a problem while passing a value to function
                    - Now doing the testing with updating the attribute "streetaddress"
                    - Need to pass three arguments to function
                    - Input file will have dn value, attribute to update and the value to be updated
                    - Trying to update one user

    Script is like this:


    param([string]$Whatif)
    $CURRENTPATH = Split-Path $MyInvocation.MyCommand.Path
    $SCRIPTNAME = $MyInvocation.MyCommand.Name
    $DATE = [System.DateTime]::Now.ToString('yyyyMMdd-Hmm')

    Function Set-attribute($oUser, $attribute, $ovalue)
    {
        $omail = $oUser.mail
        Write-Host "# Inside the Function #"
        Write-Host "Function: User Mail is "$omail""
        Write-Host "Function: User Mail is $oUser.mail"
        Write-Host "Function: Attribute Passed is $attribute"
        Write-Host "Function: Value is $oValue"
        Write-Host "# Inside the Function #"
                    $oldvalue = $oUser.($attribute)
                    $newvalue = $ovalue
        if (($newvalue -eq $null) -or ($newvalue -eq ""))
                    {
                                    $oUser.$attribute.Clear()
                    }
                    else
                    {
                                    $oUser.$attribute = $newvalue
                    }              
    }


    ##Main
    .{
        $DATE = [System.DateTime]::Now.ToString('yyyyMMdd-Hmm')
        $INPUTFILE = $CURRENTPATH + "\" + "Inputtest.csv"
        $OUTPUTFILE = $CURRENTPATH + "\" + "OutputResult.csv"
        $domaincontroller = "ADSTest01.test.com"
        
        $inp = Import-csv $INPUTFILE
                    $output = @()
        
        foreach ($user in $inp)
                    {
            $dn = $user.dn
                                    $fetchcode = -1
                                    $mailid = $user.mail
            $attr = $user.attribute
            $value = $user.value
            $result = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$DOMAINCONTROLLER/$dn")
            Write-Host "# Value of Variable Result #"
            $result
            Write-Host "# Values before passing to function #"
            Write-Host "User Mail is $($result.mail)"
            Write-Host "Attribute Passed is $attr"
            Write-Host "Value is $Value"
            
            Set-Attribute "$result" "$attr" "$value"
            if($Whatif -eq "False")
                   {
                   Write-Host "Commiting the changes for user - "
                   $result.CommitChanges()
                   }
          }
    }


    RESULT:

    D:\Ranji\Test-Area\Set-Attribute-test.ps1
    # Value of Variable Result #


    distinguishedName : {CN=ranji,OU=Users,DC=test,DC=com}
    Path              : LDAP://ADSTest01.test.com/CN=ranji,OU=Users,DC=test,DC=com

    # Values before passing to function #
    User Mail is ranji@test.com
    Attribute Passed is streetAddress
    Value is Bangalore
    # Inside the Function #
    Function: User Mail is  
    Function: User Mail is System.DirectoryServices.DirectoryEntry.mail
    Function: Attribute Passed is streetAddress
    Function: Value is Bangalore
    # Inside the Function #
    Property 'streetAddress' cannot be found on this object; make sure it exists and is settable.
    At D:\Ranji\Test-Area\Set-Attribute-test.ps1:23 char:10
    +         $oUser. <<<< $attribute = $newvalue
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyAssignmentException


    Could you please let us why I'm not able to pass $result to $ouser in Function?
    I've tested like by calling the function Set-Attribute $result (removing other two arguments and by modifying the fuction to "Function Set-Attribute($ouser)"{}) - it works, but i'm unable to pass three values and get it back
    If you check the highlighted columns in the RESULT, the User Mail is having no value (one is empty and other one is "System.DirectoryServices.DirectoryEntry.mail")
    If I check the type of the attribute $result, it is (Name = DirectoryEntry and BaseType = "System.ComponentModel.Component")
    Could you please help to fix this? I need to work it out through calling Function and commit in main function after it.
    • Merged by jrv Friday, April 28, 2017 3:29 PM duplicate
    Friday, April 28, 2017 10:32 AM
  • Could you please let us why I'm not able to pass $result to $ouser in Function?
    I've tested like by calling the function Set-Attribute $result (removing other two arguments and by modifying the fuction to "Function Set-Attribute($ouser)"{}) - it works, but i'm unable to pass three values and get it back
    If you check the highlighted columns in the RESULT, the User Mail is having no value (one is empty and other one is "System.DirectoryServices.DirectoryEntry.mail")
    If I check the type of the attribute $result, it is (Name = DirectoryEntry and BaseType = "System.ComponentModel.Component")
    Could you please help to fix this? I need to work it out through calling Function and commit in main function after it.
    Friday, April 28, 2017 10:42 AM


  • RESULT:

    D:\Ranji\Test-Area\Set-Attribute-test.ps1
    # Value of Variable Result #


    distinguishedName : {CN=ranji,OU=Users,DC=test,DC=com}
    Path              : LDAP://ADSTest01.test.com/CN=ranji,OU=Users,DC=test,DC=com


    User Mail is ranji@test.com
    Attribute Passed is streetAddress
    Value is Bangalore

    Function: User Mail is  
    Function: User Mail is System.DirectoryServices.DirectoryEntry.mail
    Function: Attribute Passed is streetAddress
    Function: Value is Bangalore

    Property 'streetAddress' cannot be found on this object; make sure it exists and is settable.
    At D:\Ranji\Test-Area\Set-Attribute-test.ps1:23 char:10
    +         $oUser. <<<< $attribute = $newvalue
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyAssignmentException
    Friday, April 28, 2017 10:44 AM
  • This seems almost like a duplicate post???

    https://social.technet.microsoft.com/Forums/windowsserver/en-US/2bf24bbd-382d-44d2-aa44-671412985628/passing-argument-to-function?forum=winserverpowershell


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    Friday, April 28, 2017 2:57 PM
  • Please stop reposting the same question over and over.  If you continue we may have ot ban you.  YOU now have two accounts that are continuously posting the same question.

    Your question was answered in your first post.  If you cannot understand the answer then you will have to get someone to help you.


    \_(ツ)_/


    • Edited by jrv Friday, April 28, 2017 3:16 PM
    Friday, April 28, 2017 3:14 PM