Extract Data and put into Variables

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Joshua Taylor Joshua Taylor 2 years, 3 months ago.

  • Author
    Posts
  • #18427
    Profile photo of Wallace Lima
    Wallace Lima
    Participant

    Good morning friends
    How could I extract the information as follow:
    Scenario: Verify a windows service in a list of servers, start this service (if stopped) and send a email with a html table, where listed ok servers, restarted servers and offline servers.
    Filter the services and restart its already ok, I don´t know how extract data to build a report.
    sorry for misspelling and thank you for attention

    great forum! congrats from Brasil

    
    $server = Get-Content c:\temp\server.txt
    
    
    foreach ($s in $server) {
    
    $service = Get-Service -Name SomeService -ComputerName $s -ErrorAction SilentlyContinue
    
    if(Test-Connection -ComputerName $s -Count 1 -quiet)  
    
    {
      if ( ($service).Status -eq "Running" )  
                    
                { 
                    
                     {"put ok servers into a variable A"}
                
                }
                  
             else
        
                {
                    $service | where {$_.status -eq 'Stopped'} | Start-Service 
                    
                    {"put restarted servers into a variable B"} 
                }
                }
                
       
         else 
       
       {
         "put offline servers into a variable C"
       }
       }
    
     
     $body = "put variables A,B,C in html" 
    
    
     Send-MailMessage -To mail1 -From mail2 -Subject "ServiceStatus" -Body "$body" -SmtpServer mailserver -BodyAsHtml
    
    
  • #18435
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Take a look at our ebook on creating HTML reports in PowerShell (it's under the Resources menu).

    What you're doing now has a bit of a problem in that you're outputting "status" information to the pipeline, which is going to make the pipeline difficult to work with. Write-Verbose would be better. But, as the book outlines, the idea is to get whatever data you want and put it into objects – there are examples. Those can be more easily converted to HTML if you want a report in that form.

  • #18441
    Profile photo of Joshua Taylor
    Joshua Taylor
    Participant

    Here's an example of how you could begin to build the report.

  • #18511
    Profile photo of Wallace Lima
    Wallace Lima
    Participant

    I get it!

    Gentlemen's, thank you very much, it worked.

    Don, I didn't know about books section, certainly I will check to improve my skills (and check the results with verbose)

    Taylor, great example, such smart code, this trick:

    $Body +=
    
    to increment the results in line is just great.
    
    Thanks for attention guys you helped me a lot.
    
    att
    Wallace Lima
  • #18526
    Profile photo of Joshua Taylor
    Joshua Taylor
    Participant

    Glad to help!

You must be logged in to reply to this topic.