Вы можете преобразовать имеющийся у вас текстовый файл в соответствующий файл CSV и без особых усилий импортировать его в Excel.
# read the file as a single string including newlines
$content = Get-Content -Path 'TheInputFile' -Raw
# create a regex to parse the file
$regex = [regex] '(?i)Node:\s*(?<node>[^\r\n]*)\r?\nName:\s*(?<name>[^\r\n]*)\r?\nCluster:\s*(?<cluster>[^\r\n]*)'
$match = $regex.Match($content)
# collect the resulting PSObjects in variable $result
$result = while ($match.Success) {
[PsCustomObject]@{
'Node' = $match.Groups['node'].Value
'Name' = $match.Groups['name'].Value
'Cluster' = $match.Groups['cluster'].Value
}
$match = $match.NextMatch()
}
# output on screen
$result
# output to CSV file (if you like)
# $result | Export-Csv -Path 'TheOutputFile' -UseCulture -NoTypeInformation
Результат на экране:
Node Name Cluster
---- ---- -------
ABC PC HS1
XZZ CC HS2
XYZ DD HS3
Обновление Этот массив $result
можно использовать для вставки данных в файл Excel. Примерно так:
# first use the above code to parse the text file into an array of PSObjects.
# next, insert data from the $result array in the Excel
$WorkbookPath = "Destination Path"
$sheetName = "Sheet1"
$objExcel = New-Object -ComObject Excel.Application
$WorkBook = $objExcel.Workbooks.Open($WorkbookPath)
$WorkSheet = $WorkBook.sheets.item($sheetName)
$usedRange = $WorkSheet.usedRange
$lastCell = $usedRange.SpecialCells(11)
# determine the row and column positions
$row = $lastCell.row + 1
$colNode = ($Worksheet.Columns.Find("Node"))
$colName = ($Worksheet.Columns.Find("Name"))
$colCluster = ($Worksheet.Columns.Find("Cluster"))
# now loop through the parsed $result array
$result | ForEach-Object {
$WorkSheet.cells.item($row, $colNode.Column).value = $_.Node
$WorkSheet.cells.item($row, $colName.Column).value = $_.Name
$WorkSheet.cells.item($row, $colCluster.Column).value = $_.Cluster
$row++
}
$WorkBook.Save()
$objExcel.Quit()
# release COM objects from memory
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($WorkBook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel) | Out-Null
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Regex Подробности:
(?i) Make the Match work case-insensitive
Node: Match the characters “Node:” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?<node> Match the regular expression below and capture its match into backreference with name “node”
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\r Match a carriage return character
? Between zero and one times, as many times as possible, giving back as needed (greedy)
\n Match a line feed character
Name: Match the characters “Name:” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?<name> Match the regular expression below and capture its match into backreference with name “name”
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\r Match a carriage return character
? Between zero and one times, as many times as possible, giving back as needed (greedy)
\n Match a line feed character
Cluster: Match the characters “Cluster:” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(?<cluster> Match the regular expression below and capture its match into backreference with name “cluster”
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)