2

i encountered a problem when trying to build a small read-host script that pipes the result into an AD script:

when doing an AD search, i can select objects like that:

get-aduser exampleuser | select-object name,enabled

though when i enter

name,enabled

into the read-host that stores the input as a variable which later gets used in the script, it turns into this:

name,enabled
------------
{}          

instead of this (when typing it manually)

get-adcomputer CTXTEST | select-object name,enabled

name    enabled
----    -------
CTXTEST   False

I assume that I overlook something rather simple, but I tried a lot of things and didnt find a solution through master Google.

1 Answer 1

3

Thats because the Select-Object cmdlet takes an array of strings, the Read-Host cmdlet only returns a singe string, even if the string contains a comma.

You can simply create an array by splitting the input on a comma. (I also use the regex to trim whitespaces):

$selectResult = (Read-Host "Which properties?") -split '\s*,\s*'
Sign up to request clarification or add additional context in comments.

4 Comments

Nice. I usually do -split ',' |% Trim to avoid excess whitespace
@MathiasR.Jessen wow, and I didn't know that you can invoke Trim like that :-). So I can invoke any member method like that?
i was about to add this as i just found out this works aswell - i split it up at the "," and it works just fine. it feels sloppy and messy, but i guess its fine if noone ever sees it
Sure, but if you enter name, enabled then your select will fail.

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.