Powershell script for multiple URLs

Vin 21 Reputation points
2020-08-04T19:34:54.94+00:00

Please help me ,iM.new to powershell scripts
I have to create a script to check multiple URLs and get the output in text file

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,551 Reputation points
    2020-08-04T21:06:33.697+00:00

    Homework assignment? ;-)

    Use Invoke-WebRequest.

    For example:

    $Uris = 'www.site.one','www.site.two'
    $Uris |
        ForEach-Object{
            $Status = ""        # entirely optional if you don't care whay things failed
            Try{
                $Response = Invoke-WebRequest -Uri $_ -ErrorAction Stop
                $Status = $Response.StatusCode
                # Do something with the body part of the response
            }
            Catch{ 
                $Status = $Response.StatusCode
                # react to error here
            }
        }
    

    How you deal with the "output" of the operation is up to you. Are you looking for specific data found in a HTML tag? Or just dumping the entire body of the response into a file?

    1 person found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. soumi-MSFT 11,766 Reputation points Microsoft Employee
    2020-08-05T06:21:35.623+00:00

    @Vin , You can check the snippet shared as an attachment to this response. The following snippet would help you get the outputs in both .csv and .txt formats

    Note: Make sure to update the extension of the attached file to .ps1 from .txt.

    Hope this helps.

    Do let us know if this helps and if there are any more queries around this, please do let us know so that we can help you further. Also, please do not forget to accept the response as Answer; if the above response helped in answering your query


  2. Vin 21 Reputation points
    2020-08-05T16:03:47.747+00:00

    Please help me on this it would be grateful,

    Just want another help that I have 3 scripts which I have saved as one program(ps1)... Thing is how to get the powershell output screen as email (not as text file or any other format) is there a way becoz i have a 3 different programs combined as one and output runs successfully but sending mail should be the output as email

    0 comments No comments

  3. Rich Matheisen 46,551 Reputation points
    2020-08-05T17:48:14.813+00:00

    You can attach the saved output (i.e., a file) to an e-mail by using the Send-MailMessage cmdlet.

    0 comments No comments

  4. soumi-MSFT 11,766 Reputation points Microsoft Employee
    2020-08-05T20:21:13.553+00:00

    @Vin , You can add the following snippet at the end of the script that I earlier shared as attachment:

    $username = "user-email-address"  
    $password = "password"  
    $sstr = ConvertTo-SecureString -string $password -AsPlainText -Force  
    $cred = New-Object System.Management.Automation.PSCredential -argumentlist $username, $sstr  
    $smtpServer = "smtp.office365.com" #if using Exchange Online, or put down the preferred smtp server address  
    $sendTo = "user-email-address"  
    $sentFrom = $username  
    $messageSubject = "Test message"  
      
    $body = $uriList | ConvertTo-Html | Out-String  
      
    Send-MailMessage -To $sendTo `  
                     -From $sentFrom `  
                     -Subject $messageSubject `  
                     -Body $body `  
                     -BodyAsHtml `  
                     -smtpserver $smtpServer `  
                     -usessl `  
                     -Credential $cred `  
                     -Port 587  
    

    This would help in sending the report in an email. Also, I am attaching the complete script as attachment to this response.

    Note: Make sure to update the extension of the attached file to .ps1 from .txt.

    Hope this helps.

    Do let us know if this helps and if there are any more queries around this, please do let us know so that we can help you further. Also, please do not forget to accept the response as Answer; if the above response helped in answering your query


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.