PowerShell array and XML operations, powershell array xml

Source: Internet
Author: User
Tags powershell array

PowerShell array and XML operations, powershell array xml

 

PowerShell Basics

 

PowerShell Array Operations

 

Split string into data

Cls # original string $ str = "abc, def, ghi, mon" # data definition # $ StrArray = New-Object Collections. generic. list [string] $ StrArray = @ () # split $ StrArray = $ str. split (",") $ len = $ StrArray. countWrite-Host "Total number of elements in the data:" $ len # traverse the data element foreach ($ str in $ StrArray) {$ str}

The execution result is:

  

  

An array can be defined in two ways: New-Object Collections. Generic. List [string] And @()

$ StrArray = New-Object Collections. Generic. List [string]
$ StrArray = @()

Even if it is defined as an int type data group: $ StrArray = New-Object Collections. Generic. List [int], if it is assigned a value as a string, no error is returned?

The execution result is as follows:

 

Read the information in the text file in an array.

Cls $ content = Get-Content "d: 1.txt" $ contentTmp =" "foreach ($ line in $ content) {$ contentTmp = $ contentTmp + $ line} $ StrArray = New-Object Collections. generic. list [string] # split $ StrArray = $ contentTmp. toString (). split (",") $ len = $ StrArray. countWrite-Host "Total number of elements in the data:" $ len # traverse the data element foreach ($ str in $ StrArray) {$ str}

 

The original text file looks so long

Convert to the data read result

 

PowerShell XML data operations

Read XML data and node Information

Cls # function GetAlterCounter ($ path) {$ XML = [xml] (Get-Content $ path) $ list = $ xml. messageData. note $ list} $ xmlList = New-Object Collections. generic. list [string] $ xmlList = GetAlterCounter ("C: \ WORKSPACE \ PowerShellScript \ TestReadXml. xml ") foreach ($ obj in $ xmlList) {Write-Host "**********************" Write-Host $ obj. id Write-Host $ obj. to Write-Host "*********************"}

The original XML file looks like this

Here, we read the information of the IDS and the two nodes. The result is as follows:

  

 

 

 

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.