PowerShell Arrays and XML operations

Source: Internet
Author: User
Tags powershell array

PowerShell Basics

PowerShell Array Operations

Operations to split a string into data

cls# Raw String $Str ="Abc,def,ghi,mon" #数据定义 # $StrArray=New-Object Collections.Generic.List[string]$StrArray=@ () #拆分 $StrArray=$Str. Split (",") $Len =$StrArray.CountWrite-The total number of elements in the Host data is: "$Len#遍历数据元素foreach ($Str inch$StrArray) {    $Str}

The result of the execution is:

  

  

There are two ways to define an array, one is New-object collections.generic.list[string], one is @ ()

$StrArray = New-object Collections.generic.list[string]
$StrArray = @ ()

Even data groups defined as type int: $StrArray = New-object Collections.generic.list[int], assigning a value to a string and not an error?

Execution results as follows

Read the information in the text file as an array

cls$content=Get-Content "D:1. txt "$CONTENTTMP="" foreach ($lineinch$content) {$CONTENTTMP=$contentTmp+$line} $StrArray=New-Object Collections.Generic.List[string]#拆分 $StrArray=$contentTmp. ToString (). Split (",") $Len =$StrArray.CountWrite-The total number of elements in the Host data is: "$Len#遍历数据元素foreach ($Str inch$StrArray) {    $Str}

The original text file looks like this.

Convert to data read-out results

PowerShell XML Data manipulation

Reading XML Data and node information

cls# getting XML informationfunctionGetaltercounter ($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 ($objinch$xmlList) {Write-Host "*********************the Write-Host $obj. ID Write-Host $obj. toWrite-Host "*********************"}

The original XML file looks like this.

Here read the ID and the information of the same two nodes, read out the result:

  

PowerShell Arrays and XML operations

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.