Function to retrieve Ad user account details using read-host

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #6245

    by someguy at 2013-01-02 15:49:55

    It seems that for some reason the variables are not retaining the data from the host and im a little stuck as to why.

    Function Getuserdetails ([string]$givenname, [string]$Surname)
    {

    $Givenname = Read-Host ' Please enter Firstname '

    $Surname = Read-Host ' Please enter Lastname '

    Get-ADUser -properties * -Filter {(givenname -eq "$Givenname") -and (surname -eq "$Surname")} | Select-Object telephonenumber,office

    }

    by Klaas at 2013-01-03 00:11:16

    In the function declaration you specify $givenname and $surname as input parameters. That means you want them to be supplied with the function call:
    Getuserdetails John Smith
    But then in the function body you want to fill those variables again?

    Maybe you can make things more clear by showing a bit more of your code.

    And a little optimization: it's better to use -properties telephonenumber,office instead of * if you only want to return those 2 properties.

    by RichardSiddaway at 2013-01-03 10:32:01

    Please don't do this

    You have a function that has the names as parameters but then you use Read-Host to get the names?

    if you are running the function from the command line make the parameters mandatory and then you will prompted for them if you forget. If this function is part of a bigger script you should already have the names.

    I would recommend changing your function to this
    function Getuserdetails {
    [CmdletBinding()]
    param (
    [parameter(Mandatory=$true)]
    [string]$Givenname,

    [parameter(Mandatory=$true)]
    [string]$Surname
    )

    Get-ADUser -properties telephonenumber,office -Filter {(GivenName -eq $Givenname) -and (Surname -eq $Surname)}

    }

    The variables don't need to be in quotes - you would have been getting the object rather then the value

    by someguy at 2013-01-03 12:51:17

    Thank you guys for responding i will take your advice.

    Thanks again.

You must be logged in to reply to this topic.