Exchange a PowerShell array and hash table

Source: Internet
Author: User
Tags powershell array

Example:

You can use a variable to hold an array, assign multiple values to the variable through the array, and between the values, the values need to be separated by a separator number, creating an example:

$servers = "EX1", "EX2", "EX3"

to create an empty hash table, you can use the following syntax:

$hashtable = @{}

After the hash table is created, we can assign the value to it:

$hashtable ["server1"] = 1

$hashtable ["server2"] = 2

$hashtable ["server3"] = 3

We can also use one of the following commands to create a hash table with the following syntax:

$hashtable = @{server1 = 1; server2 = 2; server3 = 3}

in this command above, we use semicolons to separate multiple hashes, or you can use the following syntax:

$hashtable = @{

Server1 = 1

Server2 = 2

Server3 = 3

}

Let's take a look at the results of the data running in Pewershell :

650) this.width=650; "title=" 01.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/03/ Wkiol1tff2oc5vffaadhr66ujng244.jpg "alt=" Wkiol1tff2oc5vffaadhr66ujng244.jpg "/>

The result above is the output, followed by a re-assignment $server [2] the value from EX3 modified to EX4

Let's continue to see a command, we add value to the array

650) this.width=650; "title=" 02.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/03/ Wkiol1tff2pgsrjgaabbvw-2nhg662.jpg "alt=" Wkiol1tff2pgsrjgaabbvw-2nhg662.jpg "/>

Use the following command to view the number of values in the current array:

650) this.width=650; "title=" 03.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/06/ Wkiom1tffoezoadoaaako6etzaa842.jpg "alt=" Wkiom1tffoezoadoaaako6etzaa842.jpg "/>

We also use sequential statements to display the values in the data as strings, with the following syntax:

$servers | foreach-object {"Server Name: $_"}

650) this.width=650; "title=" 04.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M00/59/03/ Wkiol1tff2ojl5wuaact_v5nbpk951.jpg "alt=" Wkiol1tff2ojl5wuaact_v5nbpk951.jpg "/>

you can also use parameters -contains or –notcontains to see if the specified value exists in the array

PS c:\> $servers-contains "EX1"

True

Here's how to use the following hash table:

650) this.width=650; "title=" 05.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M00/59/06/ Wkiom1tffoeguy9jaady_yjk8k8988.jpg "alt=" Wkiom1tffoeguy9jaady_yjk8k8988.jpg "/>

If you want to sort the hash table, you can use the GetEnumerator () method, and then use the value to sort

650) this.width=650; "title=" 06.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/03/ Wkiol1tff2sw587laab2vhqdyla907.jpg "alt=" Wkiol1tff2sw587laab2vhqdyla907.jpg "/>

We can also use a hash table when creating custom objects, or provide a set of parameter names or parameterized values. Instead of requiring a command to specify the name of the parameter, you can use the corresponding value of the hash table to match the name of the parameter and its associated key value to be automatically entered, following an example:

$parameters = @{

Title = "Manager"

Department = " operation and Maintenance Engineering Department "

Office = " Headquarters "

}

Set-user testuser @parameters

650) this.width=650; "title=" 07.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/06/ Wkiom1tffoizbvkgaacpw3apmss174.jpg "alt=" Wkiom1tffoizbvkgaacpw3apmss174.jpg "/>

to the AD To View the user information:

650) this.width=650; "title=" 08.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/03/ Wkiol1tff2tcgpgnaahkpge99uu669.jpg "alt=" Wkiol1tff2tcgpgnaahkpge99uu669.jpg "/>

650) this.width=650; "title=" 09.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/06/ Wkiom1tffperzxz2aaggolzgdaa618.jpg "alt=" Wkiom1tffperzxz2aaggolzgdaa618.jpg "/>

in the above command, we can see that the user attribute in the Title , Department and the Office when using Set-user is assigned a value

For more usage of the hash table, you can use the Help system to allow the following commands:

Get-help About_hash_tables

For more data and hash table usage examples:

You can use the output results to create an array, for example, we use the Get-mailbox command to get a set of mailboxes

650) this.width=650; "title=" 10.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M00/59/03/ Wkiol1tff3sr9j6paadciftuogg960.jpg "alt=" Wkiol1tff3sr9j6paadciftuogg960.jpg "/>

To manipulate the array you just created:

650) this.width=650; "title=" 11.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M00/59/06/ Wkiom1tffpjz-z65aabcimoimu0871.jpg "alt=" Wkiom1tffpjz-z65aabcimoimu0871.jpg "/>

Note: When our command simply returns an item, the Exchange is not accessible in the form of data, but is not available in Exchange SP1 This issue is fixed in the following example:

650) this.width=650; "title=" 12.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/03/ Wkiol1tff3tdjrqeaab-vakqz98408.jpg "alt=" Wkiol1tff3tdjrqeaab-vakqz98408.jpg "/>

if it is Exchange2013 version, we can use the following command to avoid errors in the output:

650) this.width=650; "title=" 13.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M01/59/06/ Wkiom1tffpibxmyuaacehoarr7e403.jpg "alt=" Wkiom1tffpibxmyuaacehoarr7e403.jpg "/>

we can add and remove multiple attribute values to see the following example: we have Testuser This user adds a Email Address

$mailbox = Get-mailbox TestUser

$mailbox. EmailAddresses + = "[Email protected]"

Set-mailbox testuser-emailaddresses $mailbox. EmailAddresses

650) this.width=650; "title=" 14.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/03/ Wkiol1tff3xtojljaab3auhd-so421.jpg "alt=" Wkiol1tff3xtojljaab3auhd-so421.jpg "/>

Look at the user's email address information:

650) this.width=650; "title=" 15.png "style=" Float:none; "src=" http://s3.51cto.com/wyfs02/M02/59/06/ Wkiom1tffpmh4gr0aaebtvlxc1y059.jpg "alt=" Wkiom1tffpmh4gr0aaebtvlxc1y059.jpg "/>

You can also perform a delete operation with the following command:

$mailbox. EmailAddresses-= "[Email protected]"

Set-mailbox testuser-emailaddresses $mailbox. EmailAddresses

650) this.width=650; "title=" 16.png "style=" Float:none; "src=" Http://s3.51cto.com/wyfs02/M00/59/04/wKioL1TFF3Ww_ Kguaad-ob44-os947.jpg "alt=" Wkiol1tff3ww_kguaad-ob44-os947.jpg "/>

For help with more arrays, we use the get-help about_arrays View

This article from "Robin's Home" blog, declined reprint!

Exchange a PowerShell array and hash table

Related Article

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.