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