none
need to supply multiline input from a copy/paste

    Question

  • Hello,

    I would like to write a script that prompts for input (like using read-host), but I need to be able to copy/paste that input from the clipboard, and that clipboard data will have line breaks in it, like copy/pasting the contents of a text file.

    The script is a simple script that will process the lines of input, but for ease of use for this particular scenario, I don't want to have to save the text to a text file and then read in that file, I really need to just copy/paste it directly into the input prompt of the script.

    can I do this? something with HERE strings and read-host? or do I need to hit up something in the [console] class like readline or readlines etc..???

    I'm about to start experimenting but as always I appreciate the experienced input from the community here.

    Thanks.


    • Edited by c0pe Tuesday, August 21, 2012 7:15 PM
    Tuesday, August 21, 2012 7:14 PM

Answers

  • I think you can pry most of what you need out of this:

    http://mjolinor.wordpress.com/2011/12/31/clip-toarray-for-ps-v3-ise/


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by c0pe Friday, August 24, 2012 2:24 PM
    Tuesday, August 21, 2012 7:28 PM
  • I'm not sure why you are attempting to read character by character. I wrote a powershell function that reads usernames in a loop using read-host in order to produce a short report of the value of a dozen or so AD attributes. After a while I got tired of copying and pasting them individually from a spreadsheet and found I could copy the entire column and paste it into the console at the read-host prompt.

    When it finishes processing all the names I posted in, I just hit the ENTER button, and the function knows it is done.

    You should be able to do much the same, however your loop might need a different method than mine in order to determine when there is no more to read, as your intput might actually contain blank lines.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by c0pe Friday, August 24, 2012 2:24 PM
    Tuesday, August 21, 2012 9:46 PM

All replies

  • I think you can pry most of what you need out of this:

    http://mjolinor.wordpress.com/2011/12/31/clip-toarray-for-ps-v3-ise/


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by c0pe Friday, August 24, 2012 2:24 PM
    Tuesday, August 21, 2012 7:28 PM
  • Hi, thanks mjolinor. I hadn't thought of snagging directly from the clipboard. I'll play with that.

    I'm curious as to why the following seems to not include carriage returns though, any ideas? This looks like it should do what I want, but if you view the value of  $FirewallConfig after running this (type in test[ENTER]test[ENTER]test), is shows "testtesttest", enter keys removed??

    $FirewallConfig = [string]::Empty
    
    while ($true)
    {	
    	$k = [Console]::ReadKey($true)
    	if ($k.Key -eq [ConsoleKey]::Escape) {break}
    	$FirewallConfig += $k.KeyChar
    }

    would I need to check for the cariage return key and replace with something else?... then replace that placeholder again with a carriage return? that doesn't sound right at all.

    I'll play with the direct clipboard options as well, but since I've started down the path above already I want to understand what's wrong with it.

    Tuesday, August 21, 2012 9:00 PM
  • I'm not sure why you are attempting to read character by character. I wrote a powershell function that reads usernames in a loop using read-host in order to produce a short report of the value of a dozen or so AD attributes. After a while I got tired of copying and pasting them individually from a spreadsheet and found I could copy the entire column and paste it into the console at the read-host prompt.

    When it finishes processing all the names I posted in, I just hit the ENTER button, and the function knows it is done.

    You should be able to do much the same, however your loop might need a different method than mine in order to determine when there is no more to read, as your intput might actually contain blank lines.


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by c0pe Friday, August 24, 2012 2:24 PM
    Tuesday, August 21, 2012 9:46 PM
  • Thanks mjolinor and Al, I marked both your responses as helpful.

    I wound up using the idea from mjolinor's post and I'm grabbing from the clipboard directly, although in a slightly different style.

    I'm doing the following to grab the data I need:

    $FirewallConfig = & {powershell –sta {add-type –a system.windows.forms; [windows.forms.clipboard]::GetText()}}

    thanks for the input, I appreciate it.

    Friday, August 24, 2012 2:22 PM