none
how can i search a text file for data using powershell? RRS feed

  • Question

  • I need to search a text file for specific information using powershell. I have tried using

    Select-String   c:\test.txt   -pattern  "Current IP Address"  

    But the output includes the path which i don't want.

    So is there a way to just get the information you want like "IP Address = 10.192.254.1"  ?

     

     


     
    • Edited by Heckter Saturday, September 3, 2011 8:09 PM
    Saturday, September 3, 2011 8:03 PM

Answers

  • (((gc c:\scripts\test2.txt) -split "=")[1]).trim()
    


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by Heckter Saturday, September 3, 2011 8:15 PM
    Saturday, September 3, 2011 8:11 PM

All replies

  • (((gc c:\scripts\test2.txt) -split "=")[1]).trim()
    


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Marked as answer by Heckter Saturday, September 3, 2011 8:15 PM
    Saturday, September 3, 2011 8:11 PM
  • Please post the contents of the test.txt file.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Saturday, September 3, 2011 8:12 PM
  • Test file

    IPv4 settings:
    Enabled                 = 1
    Current IP Address      = 10.192.254.1
    Current IP Gateway      = 10.10.20.1
    Current IP Netmask      = 255.255.255.0
    DHCP Enabled            = 0
    Current DNS Server 1    = 10.170.4.8
    Current DNS Server 2    = 10.175.3.21
    DNS Servers from DHCP   = 0

    So in the part of my script i ran it like this

    $MyReport += Get-HTMLDetail "Current IP Address"
    $MyReport += (((gc c:\Audit\DracNetworkDetails.txt) -split "=")[11]).trim()
    $MyReport += Get-HTMLDetail "Current IP Netmask"
    $MyReport += (((gc c:\Audit\DracNetworkDetails.txt) -split "=")[15]).trim()
    $MyReport += Get-HTMLDetail "Current IP Gateway"
    $MyReport += (((gc c:\Audit\DracNetworkDetails.txt) -split "=")[13]).trim()
    $MyReport += Get-HTMLDetail "Current DNS Server 1"
    $MyReport += (((gc c:\Audit\DracNetworkDetails.txt) -split "=")[19]).trim()
    $MyReport += Get-HTMLDetail "Current DNS Server 2"
    $MyReport += (((gc c:\Audit\DracNetworkDetails.txt) -split "=")[21]).trim()
    $MyReport += Get-HTMLDetail "DHCP Enabled "
    $MyReport += (((gc c:\Audit\DracNetworkDetails.txt) -split "=")[23]).trim()



    • Edited by Heckter Saturday, September 3, 2011 9:29 PM
    Saturday, September 3, 2011 9:09 PM
  • If that works, ok.  I thought it was a single line text file.  Let's see if there's a more elegant way of doing it.
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Saturday, September 3, 2011 9:18 PM
  • yeah well that might be good but thanks for your help.....
    Saturday, September 3, 2011 9:24 PM
  • If this is the sample file:

    IPv4 settings:
    Enabled = 1
    Current IP Address = 10.192.254.1
    Current IP Gateway = 10.10.20.1
    Current IP Netmask = 255.255.255.0
    DHCP Enabled = 0
    Current DNS Server 1 = 10.170.4.8
    Current DNS Server 2 = 10.175.3.21
    DNS Servers from DHCP = 0

    ...Please state what data you want.  Please give an example of the desired output?

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Saturday, September 3, 2011 9:30 PM
  • Well All of the information like below,

    Current IP Address = 10.192.254.1
    Current IP Gateway = 10.10.20.1
    Current IP Netmask = 255.255.255.0
    DHCP Enabled = 0
    Current DNS Server 1 = 10.170.4.8
    Current DNS Server 2 = 10.175.3.21
    DNS Servers from DHCP = 0

    but i have to put each under a heading because if i simply add the contents of the file by doing

    $MyReport += Get-HTMLDetail "Newtork Details"

    gc c:\Audit\DracNetworkDetails.txt  

    The format of text is not the same as the file....it just outputs in a long line......a html issue in my script :( 


    • Edited by Heckter Saturday, September 3, 2011 9:55 PM
    Saturday, September 3, 2011 9:51 PM
  • What does the contents of the c:\audit\DracNetworkDetails.txt look like.  Can you post a sample of this raw text file please?

    And what is 'Get-HTMLDetail'?


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"

    • Edited by Bigteddy Saturday, September 3, 2011 9:56 PM
    Saturday, September 3, 2011 9:55 PM
  • it's the same as the test, all i did for the post was change IP Addr
    Saturday, September 3, 2011 9:58 PM
  • The script is to big to post on the forum.....and i'll have to filter stuff out but will post a cut down version at a later date - Cheers Bigteddy
    Saturday, September 3, 2011 10:08 PM
  • I'm not sure if this helps, but here is another way of doing it that is more efficient, and uses Regular Expressions:

    $t = gc test12.txt
    $report = ($t -match  "^ Enabled\s+= \d") + "`n" + `
     ($t -match  "Current IP Address\s+= \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}") + "`n" + `
     ($t -match  "Current IP Gateway\s+= \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")+ "`n" + `
     ($t -match  "Current IP Netmask\s+= \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")+ "`n" + `
     ($t -match  "DHCP Enabled\s+= \d") + "`n" + `
     ($t -match  "Current DNS Server 1\s+= \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")+ "`n" + `
     ($t -match  "Current DNS Server 2\s+= \d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")+ "`n" + `
     ($t -match  "DNS Servers from DHCP\s+= \d+")
    
    $report
    
    
    


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Saturday, September 3, 2011 11:26 PM
  • I need to search a text file for specific information using powershell. I have tried using

    Select-String   c:\test.txt   -pattern  "Current IP Address"  

    But the output includes the path which i don't want.

    So is there a way to just get the information you want like "IP Address = 10.192.254.1"  ?

     

     


     


    To answer your original question, yes, like this:

     

    $t = Get-Content test12.txt 
    $t| Select-String "^ Enabled"
    $t| Select-String "Current IP Address"
    $t| Select-String "Current IP Gateway"
    $t| Select-String "Current IP Netmask"
    $t| Select-String "DHCP Enabled"
    $t| Select-String "Current DNS Server"
    $t| Select-String "DNS Servers from DHCP"
    

     


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    • Edited by Bigteddy Sunday, September 4, 2011 3:09 PM
    Sunday, September 4, 2011 3:07 PM
  • cat test.txt| Select-String -pattern "(\d.\d.\d.\d)"|convertfrom-stringdata

     


    jv
    Sunday, September 4, 2011 3:50 PM
  • That's nice, jv, and would be perfect if it was what the OP wants.  But he wants basically everything that's in the text file, minus the first line, by what I can make out.

    What you have done is neat, and a nice touch to convert it to hash table.  I think regex is not actually the answer to this probelm, whatever it may be.  I'm a little bemused, not quite sure what OP is trying to achieve.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Sunday, September 4, 2011 4:03 PM
  • Using all lesson learned for creating objects out of stuff in files.

    $txt=cat test.txt
    $hash=$txt[1..$txt.length]|Out-String|ConvertFrom-StringData
    $obj=New-Object PSObject -Property $hash
    $obj.'Current DNS Server 1'
    $obj.'Current IP Gateway'
    $obj.'Current IP Address'
    $obj.'Current DNS Server 2'
    $obj.'Current DNS Server 1'
    $obj.'Enabled'
    $obj.'Current IP Netmask'
    $obj.'DNS Servers from DHCP'
    $obj.'DHCP Enabled'
    

     


    jv
    Sunday, September 4, 2011 4:09 PM
  • That's very nice.  But what's the point of all the

    $obj.'Current DNS Server 1'  etc...

    It seems to work fine with just the first 3 lines.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Sunday, September 4, 2011 4:35 PM
  • That's nice, jv, and would be perfect if it was what the OP wants.  But he wants basically everything that's in the text file, minus the first line, by what I can make out.

    What you have done is neat, and a nice touch to convert it to hash table.  I think regex is not actually the answer to this probelm, whatever it may be.  I'm a little bemused, not quite sure what OP is trying to achieve.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"


    From the question it is impossible to tell what is required.  I have seen this thoing before in other forums. Same report , same method.  The whole thing is akludge dragged over from VBScript and lots of ugly batch files.

    The DRAC values can be taken from WMI.  The whole report that most want to do is usually a cuurent status on a nice HTML page posted to a share.  This can all be generated with two lines in PowerSHell and a style sheet.

    THe HTML detail in various versions either places a set <p> around teh line in which case your solution is correct, or it takes a name and value and formnays it into a table or onto a names control in a repexisting HTML form.

    None of this is neccessary in PowerShell as we can just use ConverTo-HTML and create all manner of nice tables with style sheets.

    Notice how Heckter is doing this:
    $MyReport += Get-HTMLDetail "Current IP Address"
    $MyReport += (((gc c:\Audit\DracNetworkDetails.txt) -split "=")[11]).trim()

    He adds the label then the vlaue.  This is an old one from a vbscript that I started about four years ago to inject simple labeled values into an HTML string. Part of it looks to be missing or they have used a demo version and not the final method.

    This will work better.

    $txt=cat test.txt
    $hash=$txt[1..$txt.length]|Out-String|ConvertFrom-StringData
    $obj=New-Object PSObject -Property $hash
    $obj|ConvertTo-HTML -Fragment
    

    We can do this for as many table componets that we may have and add tem all together at the end.  We can add seperate style sheets to each element and to teh overall dicument.

    Try this:

    get-process |
         select ProcessName,Id,Handles,WS |
         ConvertTo-Html -css http://www.designedsystemsonline.com/upload/testalign.css |
         Out-file demo.htm
    .\demo.htm

    This is my stock demo of gow to generate an HTML report with style.  On day I will collect a bunch of my style sheets and  post them to the link. This style was built to be ugly just to show how easy it is to style a report.

     

     

     


    jv
    • Proposed as answer by Bigteddy Sunday, September 4, 2011 4:56 PM
    Sunday, September 4, 2011 4:43 PM
  •  

    Try this:

    get-process |
         select ProcessName,Id,Handles,WS |
         ConvertTo-Html -css http://www.designedsystemsonline.com/upload/testalign.css |
         Out-file demo.htm
    .\demo.htm

    This is my stock demo of gow to generate an HTML report with style.  On day I will collect a bunch of my style sheets and  post them to the link. This style was built to be ugly just to show how easy it is to style a report.

     

     

     


    jv

    That's very cool.  I tried it, and it was very quick. (but ugly).
    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Sunday, September 4, 2011 4:56 PM
  • Here is a simple demo using the OPs own data and my style sheet,

    I have placed teh text file on teh server to so anyone can run it and get the same result.  The style sheet is teh same (ugly) but now we output as a list.  The output can be formaed and places anywhere on the web page we want by just changing the style sheet.

    Run it an you will begin to see what I am getting at,

    $txt=$wc.DownloadString('http://www.designedsystemsonline.com/upload/test.txt')
    $txt=$txt.Replace(':','=0')
    $hash=$txt|ConvertFrom-StringData
    $drac=New-Object PSObject -property $hash
    $drac|ConvertTo-HTML -As LIST -css http://www.designedsystemsonline.com/upload/testalign.css |
         Out-file dracdemo.htm
    .\dracdemo.htm
    

    By the way. This was all doneon the PowerShell Team blog I believe.  I haven't yet found the post but will post the link when I do.

     


    jv
    Sunday, September 4, 2011 4:59 PM
  • By the way. This was all doneon the PowerShell Team blog I believe.  I haven't yet found the post but will post the link when I do.

     


    jv
    Please do.  I'm bored.

    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Sunday, September 4, 2011 5:02 PM
  • UGLY!  Now that's not very nice:

     

    Well- here is a pretty one.

    $head = @'
    <style>
     BODY{font-family:Verdana; background-color:lightblue;}
     TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
     TH{font-size:1.3em; border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:#FFCCCC}
     TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:yellow}
    </style>
    '@
    $header = "<H1>Reporting Service Status</H1>"
    $title = "Example HTML Output"
    
    Get-Service | 
      Select-Object Status, Name, DisplayName | 
      ConvertTo-HTML -head $head -body $header -title $title | 
      Out-File report.htm
    .\report.htm
    
    

     


    jv
    Sunday, September 4, 2011 5:14 PM
  • That is so cool, it makes me want to learn Cascading Style Sheets (not).  But I suppose it's tempting.  I saved that away, 'cause I like it so much. 

    I had no idea one could do that with Powershell.  This truly is an amazing language.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Sunday, September 4, 2011 5:18 PM
  • That is so cool, it makes me want to learn Cascading Style Sheets (not).  But I suppose it's tempting.  I saved that away, 'cause I like it so much. 

    I had no idea one could do that with Powershell.  This truly is an amazing language.


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"


    Since you aer so bored here is some more:

    http://blogs.msdn.com/b/powershell/archive/2009/10/30/sending-automated-emails-with-send-mailmessage-convertto-html-and-the-powershellpack-s-taskscheduler-module.aspx
    $messageParameters = @{                       
                    Subject = "Installed Program report for $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())"                       
                    Body = Get-WmiObject Win32_Product |                        
                        Select-Object Name, Version, Vendor |            
                        Sort-Object Name |            
                        ConvertTo-Html |                        
                        Out-String                       
                    From = "Me@MyCompany.com"                       
                    To = "Me@MyCompany.com"                       
                    SmtpServer = "SmtpHost"                       
                }                       
     Send-MailMessage @messageParameters -BodyAsHtml

    Here is your tutorial on style sheets.  The basics only take about an hour.

    http://w3schools.com/css/default.asp

     


    jv
    Sunday, September 4, 2011 5:35 PM
  • I forgot to tell you that C# Express can edit CSS in a GUI with all of the bells and whistles.  When the CSS gets big I go from notepad to Visual Studio because it is quicker to see the outcome. 

    I can style right on an HTML page and have it created in the style sheet. WYSIWYG.

     

     


    jv
    • Edited by jrv Sunday, September 4, 2011 5:38 PM
    Sunday, September 4, 2011 5:37 PM
  • Yes, it's time for me to get back to my C# self-training.  One thing at a time, I say (or maybe two or three, but there's a limit to the speed of what this old brain can absorb.)


    [string](0..9|%{[char][int](32+("39826578840055658268").substring(($_*2),2))})-replace "\s{1}\b"
    Sunday, September 4, 2011 5:44 PM
  • Guy's only saw your additional comments last night...JV is right in that the html script i use is quite old but it does provide a format that’s ok ish...for now.  The report is just to check several server settings and then send it to various people. With little knowledge, this has been a challenge for me to do but you've got to start somewhere....help from you guy's has helped me get it on its way and I’m grateful for that. What I’m hoping/ wanting to do is to build an app with a GUI front end that will allow you to select various server settings and then generate the results in various formats.....html being one. So I’m probably going to need Primal Forms for the gui, sql for db etc but it will be a good while before i get there so I’m just trying to get what i can in terms of PowerShell commands and then use that to create a report......JV could probably do this in half the commands and in a of tenth of the time but i don't have that knowledge....:(

    I've tried to post a smaller version of the script for the benefit of Bigteddy but it's too big.

    Wednesday, September 7, 2011 9:23 AM
  • JV is right in that the html script i use is quite old but it does provide a format that’s ok ish...for now.  but it's too big.


    It would be far easier to start from scratch with PowerSHell. Trying to fit PowerShell around an old and clearly broken technique will only make you task harder.

    If the report format in HTML is what you prefer then just emulate teh format in PowerShell using a style sheet.

    See the following thread for more techiques:

    http://www.scriptinganswers.com/forums/scriptinganswers/forum_posts.asp?TID=4668&KW=

    Towards the end of teh discussion is a fiscussion of how to use HTML in POwerSHell effectively and how to build complex web page reports from multiple PoSH data extractions.

     


    jv
    Wednesday, September 7, 2011 2:19 PM