Multiple input methods of PowerShell array _powershell

Source: Internet
Author: User
Tags powershell array

The demand was initiated by the small camel. Thanks to Microsoft Cloud Solution Exchange 236804566 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:\> $arr
ADF
Asdfer
Sredsaf

Notice here that the double quotes are separated from the comma. I have to say, this is the simplest way to enter, but the input method is not very friendly, require users to change the PowerShell script

To facilitate user interaction, I wrote one of the following examples

$changdu = Read-host ("Please 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 be added ')
  $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+ '} '
$s 1
echo xx | Out-file d:\3456.ps1
$files = (Get-childitem d:\3456.ps1). Pspath
clear-content $files
add-content $ Files-value "$s 1"

Execution, the effect is this

Enter the data length in the array: 4
Enter 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"; $arr =@{0= "asdf"; 1= "ert"; 2= "2345"; 3= "GADF";}
 
PS d:\> $arr =@{0= "asdf"; 1= "ert"; 2= "2345"; 3= "GADF";}
 
PS d:\> $arr
 
Name       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.

$arr =@{0= "asdf"; 1= "ert"; 2= "2345"; 3= "GADF";}

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

PowerShell D:\3456.ps1

The above method can solve the demand, the problem is that it needs to create a new file, in terms of efficiency, is lower, but the use of the cycle, 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, and multiple virtual machines are separated by commas in the English state"). Split (', ')

Here we use the Split method, which separates the data, turns them into arrays, and about the split method, the following link says it's crisp and detailed, and I don't have any nonsense here.

Http://www.jb51.net/article/69147.htm

So the above script I can also modify, with the split method to deal with, the advantage is that the current script can be output, and users do not need to enter double quotes and

$changdu = Read-host ("Please 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 be added ')
  $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

It should also be said that there is a way to use the. Net method to deal with, the visualization is poor, but the effect is very good

Copy Code code as follows:

$BB 2= New-object System.Collections.ArrayList
$BB 2. ADD ("First")
$BB 2. ADD ("second")

The result of the output is this, can see this method is simpler, revise, we can also make interactive input way.

Copy Code code as follows:

PS d:\> $BB 2
First one
A second

To sum up, there should be at least 4 ways to solve our problems.

1. Output string to text, run with PowerShell Xxx.ps1
2, with the split method of processing strings, cutting the group
3, directly without interactive word processing
4. Net method handles

This article comes from "Nine uncle-Microsoft Private Cloud" blog

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.