Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

As an example, I would like the following commands to put a dir listing with all subdirectories to StdOut

@'
>> -R
>> '@ > .\test
>>
dir `test`

So, after the first four lines, there's a file in the current directory called test containing -R (recurse). Line four doesn't work, of course, but I would like the result to be the same as

dir -R

. Perhaps a better for line 3 is

echo `test`

where I hope the result is

/s
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Try:

"Get-Childitem $(Get-Content test)" | Invoke-Expression

or

"dir $(type test)" | iex

Invoke-Expression is roughly equivalent to Bash's eval. It's a good idea to be cautious of any security implications of its use.

share|improve this answer
    
Echo Dennis' comment regarding security. Could the input to Invoke-Expression be manipulated to gain an advantage? –  Simon Catlin Oct 24 '10 at 9:19
    
@Simon: Yes, the contents of the file (named "test" in this example) could include ;del * which would be executed by Invoke-Expression causing files to be deleted, for example. –  Dennis Williamson Oct 24 '10 at 15:02
    
@Joey: But in the case of the OP's question, that would treat the file contents as the name of a file to do the gci on instead of as an option to gci. –  Dennis Williamson Oct 25 '10 at 5:42
    
Ah, sorry; skimmed the question a bit too shallowly. Agreed, then it's a quite dangerous thing to do. You could perhaps read the file, tokenize the contents and build a hashmap from it. After that you can use the splat operator (provided you're on PS2) to explicitly only use them as arguments to gci, negating the option to inject other commands (at least as far as I can see now). –  Joey Oct 25 '10 at 13:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.