0

I want to write a little Tool like a little CLI where I can get Informations out of our AD-Domain. I have that function (listed below) which works but I want it not to ask me the Parameter in the second place. The Goal is to let the Tool run in a Loop where I can type the Function-Shortcuts with given Parameter like "Username"

ListUserInformation -Username "Firstname, Lastname" (Something like that.) I tryed double and single quotes but it keeps asking in the next line about a Username. Can some1 help me with that? Here is the Function:

    function ListUserInformation {
        [CmdletBinding()]
    param (
    [Parameter(Mandatory=$true)][String]$Username
    )
        
        $target = $Username
        $x = Get-ADUser -Filter "CN -like '$target'"  -SearchBase $SearchBase -Properties CN,Title,SamAccountName,emailaddress,officephone | Out-String
            Select-Object CN,Title,SamAccountName,emailaddress,officephone
        write-host $x }
3
  • It works fine on my end. Are you sure you reloaded your function in the correct console after modifying it? Commented Sep 10, 2020 at 8:48
  • Thanks for the Answer Thomas, could you tell me what exactly you typed in? Imagine there is a Loop waiting for Input like lui (for ListUserInformation). and I want to give it the Username straight away like: lui -Username "Firstname, Lastname" I am kinda new so could you tell me what you mean with "reloaded"? I saved the Testscript and started it from a new opened PS Console. Commented Sep 10, 2020 at 8:52
  • Remove | Out-String and also make a second parameter for the $SearchBase variable. Commented Sep 10, 2020 at 8:53

2 Answers 2

1

As commented, remove Out-String. Also, your function could be more versatile if you add a second (optional) parameter $SearchBase.

Write-Host just outputs to console and is not a return value of the function. Either use the return keyword or leave it out alltogether.

Something like this:

function ListUserInformation {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [String]$Username,

        [Parameter(Mandatory = $false)]
        [String]$SearchBase = 'CN=SomeWhere,CN=Users,DC=Company,DC=com'  # insert your predefined OU searchbase here
    )
    $Properties = 'CN','Title','SamAccountName','EmailAddress','OfficePhone'
    if ([string]::IsNullOrWhiteSpace($SearchBase)) {
        Get-ADUser -Filter "CN -like '$Username'" -Properties $Properties |
        Select-Object $Properties
    }
    else {
        Get-ADUser -Filter "CN -like '$Username'" -SearchBase $SearchBase -Properties $Properties |
        Select-Object $Properties
    }
}

Usage:

$x = ListUserInformation -Username 'Pa1n4ndt3rr0r'
Sign up to request clarification or add additional context in comments.

1 Comment

The Searchbase is set but not shown here as Its confidential. Please check out my Comment on Pba´s Answer as I described my Main Issue better there. Thanks alot for your response!
0

You only need to change one minor thing. Write-Host doesn't give you an output that you can use in follow up commands.

Return on the other hands does.

function ListUserInformation {
    [CmdletBinding()]
param (
[Parameter(Mandatory=$true)][String]$Username
)
    
    $target = $Username
    $x = Get-ADUser -Filter "CN -like '$target'"  -SearchBase $SearchBase -Properties CN,Title,SamAccountName,emailaddress,officephone | Out-String
        Select-Object CN,Title,SamAccountName,emailaddress,officephone
return $x }

That should solve your problem.

3 Comments

That works, yes. But my Main Problem is not solved, maybe I did not describe it well enough. I want a consistant Loop waiting for my Input. This Function (ListUserInformation) is only one of many which I want to run. I want to Start the script and it should run in that Loop which waits for the Input and if the Input is as following: ListUserInformation -Username "Firstname, Lastname" it should recognize that it has to run the ListUserInformation function with -Username "Firstname, Lastname" as its parameter for the Get-ADUser function to work.I want all in one line Sorry for bad English ^^
so you want the above script to loop and wait for your input after every loop?
Basically yes. It should always wait for my Input and should recognize which Function and Parameters it gets within the same one Lane as I described above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.