Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

Reading a table from a text file

$
0
0
Hello,There got to be a easier way.I'm writing a script that reads the Summary.txt file when we patch SQL.If the server has multiple instances, I want to read those instances from the Summary.txt file(yes... there are other better ways, but I want to know what instances were patched... so stay with me.This is the snippet of the code that I run[code="other"]$Location = "C:\Program Files\Microsoft SQL Server\"$SummaryFile=Get-ChildItem -Recurse -Path $Location -Filter Summary.txt | select-String -Pattern "patch" | group path$SummaryFile = Get-Content $SummaryFile.Name$a = $SummaryFile | Select-String -pattern "Updated product edition:"$b = $SummaryFile | Select-String -Pattern "User Input Settings:"$line = (($SummaryFile)[($a.LineNumber)..($b.LineNumber-3)])[/code]Which yields something like this:[code="plain"] Instance Edition MSSQLSERVER ENTERPRISE SPDSTAGE ENTERPRISE[/code]Anyway I can use some CSV thing where I can simply say "select instance" ?I mean... I am able to do it, but it's god ugly.My code:[code="other"]$Location = "C:\Program Files\Microsoft SQL Server\"$SummaryFile=Get-ChildItem -Recurse -Path $Location -Filter Summary.txt | select-String -Pattern "patch" | group path$SummaryFile = Get-Content $SummaryFile.Name$a = $SummaryFile | Select-String -pattern "Updated product edition:"$b = $SummaryFile | Select-String -Pattern "User Input Settings:"$line = (($SummaryFile)[($a.LineNumber+1)..($b.LineNumber-3)]) $data = $line.split(" ",[System.StringSplitOptions]::RemoveEmptyEntries)# $Line gets the data between lines $a.LineNumber+1) and ($b.LineNumber-3# $Line retrieves the instances and its licenses, somthing like this:# MSSQLSERVER ENTERPRISE# SQLINST1 STANDARD# $Results = @{} $j=1 For ($i=0;$i -lt ($b.LineNumber-3 - $a.LineNumber+1);$i=$i+2) { $Results += @{"Instance $j"= $data[$i] } $j=$j+1 }$Results[/code]Am I missing something?Help! There has to be an easier wayThank youMiguel

Viewing all articles
Browse latest Browse all 467

Trending Articles