Multiple input methods for powershell-arrays

Source: Internet
Author: User


PS: This requirement was launched by a small camel. Thank Microsoft Cloud Solution Exchange 236804566 for this high-end group of user contributions.


Let's start by looking at how a standard array is entered.

PS d:\> $arr = "ADF", "Asdfer", "Sredsaf" PS d:\> $arradfasdfersredsaf

It is important to note that the double quotation marks and the comma are separated. I have to say, this is the simplest way to enter, but the input method is not very friendly, users need to change the PowerShell script


To facilitate user interaction, I wrote the following example

$changdu  = read-host ("Enter data length in array") $s 1=  "@{" for ($x =0;  $x  -le  $changdu -1;  $x + +) {if ($x  -ne 0)         {           $servers  = read-host (' Enter the computer name to add ')           $s 1 =  $s 1+ $x + ' = "' + $servers + '";          $s 1         }if ($ x -eq 0)          {          $servers  = read-host (' Enter first computer name ')          $s 1  =  $s 1+ $x + ' = "' + $servers + '";          $s 1    }} $s 1 =  ' $arr = ' + $s 1+ "}" $ s1echo xx | out-file d:\3456.ps1$files =  (GET-CHILDITEM D:\3456.PS1). pspathclear-content $ filesadd-content  $files  -value  "$s 1" 

If you do, the effect is like this

Please enter the data length in the array:  4 Enter the first computer name:  asdf@{0= "ASDF"; Enter the computer name you want to add:  ert@{0= "asdf"; 1= "ert"; " Enter the computer name you want to add:  2345@{0= "asdf"; 1= "ert"; 2 = "2345"; " Enter the computer name to be added:  gadf@{0= "asdf"; 1= "ert"; 2= "2345"; 3= "GADF"; [ Email protected]{0= "Asdf", 1= "ert"; 2= "2345"; 3= "GADF";} Ps d:\> [email protected]{0= "Asdf", 1= "ert"; 2= "2345"; 3= "GADF";} ps d:\>  $arrName                             Value                                                                                                                                        ----                             -----                                   3                               gadf             2                                2345            1                                ert             0                               asdf

The final output is a sentence, but this sentence is a string, not an array, so you want to output it to other files, and then run it alone.

[Email protected] {0= "asdf", 1= "ert"; 2= "2345"; 3= "GADF";}

Run the following command, and of course this sentence can be added to the script above.

PowerShell D:\3456.ps1

=================================================

The above method can solve the demand, the problem is that it needs to create a new file, from the efficiency, is low, but the use of the loop, the user experience is still very good, so change, we try the split method.

$arr = (Read-host ("Enter the name of the virtual machine you want to create, separate the virtual machines with commas in the English state)"). Split (', ')

Here is the split method, with good separation of data, they become an array, about the split method, the following link said Crisp is very detailed, I'm not a nonsense here

Http://www.pstips.net/string-object-methods.html

Then I can modify the above script, with the split method, so that the benefits of the current script can be output, and the user does not need to enter double quotes and

$changdu = Read-host ("Enter the data length in the array") $s 1= "" for ($x = 0; $x-le $changdu-1;        $x + +) {if ($x-ne 0) {$servers = Read-host (' Enter the computer name to be incremented ') $s 1 = $s 1+ $servers + '; '        $s 1}if ($x-eq 0) {$servers = Read-host (' Enter first computer name ') $s 1 = $s 1+ $servers + '; ' $s 1}} $s 2 = ($s 1). Split (';') Echo $s 2

=============

In addition, there is a way to use the. Net method to deal with, the visualization is poor, but the effect is very good

$BB 2= new-object system.collections.arraylist$bb2. ADD ("First") $BB 2. ADD ("second")

The result of the output is this, you can see that this method is more simple, modified, we can also be made interactive input mode.

PS d:\> $BB 2 The first of the second

To summarize, there should be at least 4 ways to solve our problem.

1. Output string to text, run with PowerShell Xxx.ps1

2, using the Split method to process the string , cut into arrays

3, direct non-interactive word processing

4.. net method Processing

This article is from the "Nine uncle-Microsoft Private Cloud" blog, please make sure to keep this source http://jiushu.blog.51cto.com/972756/1671226

Multiple input methods for powershell-arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.