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: