question

Vin-5708 avatar image
0 Votes"
Vin-5708 asked shaider007 commented

Powershell script for multiple URLs

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered RichMatheisen-8856 commented

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?

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How can i get output of this result in text file format

0 Votes 0 ·

What is it you want to save to a text file????

This might help you understand what the response from the Invoke-WebRequest contains and how to deal with the various parts.
powershell-invoke-webrequest-parse-and-scrape-a-web-page


0 Votes 0 ·
soumi-MSFT avatar image
0 Votes"
soumi-MSFT answered shaider007 commented

@Vin-5708, 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




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@soumi-MSFT Quick question, as per your script you attached (urlstatuscheck.txt), I tried it and I noticed for those Unable to reach websites, it does not show on CSV File, it only shows on PowerShell console in red font color. If we want to show those Unable to Reach website in Excel CSV file, what do we need to add on the script please? I'm also new to Powershell. Thanks

0 Votes 0 ·
Vin-5708 avatar image
0 Votes"
Vin-5708 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

soumi-MSFT avatar image
0 Votes"
soumi-MSFT answered RichMatheisen-8856 commented

@Vin-5708, 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



urlstatuscheck.txt (1.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It looks to me as if he wants the web page to be presented as the message body in the e-mail.

If that's the case he wants the "$response.RawContent" property or maybe "$response.Content", but neither of those is (probably) going to be what he thinks they are . . . unless the web page is a very simple one.

0 Votes 0 ·
Vin-5708 avatar image
0 Votes"
Vin-5708 answered RichMatheisen-8856 edited

Thank you so much it worked

But, I have script of simple get services listing name status of our production server while sending email the table looks unaligned is there any way make it aligned

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

How about posting the relevant parts of your code rather than having us guess at what you're already doing?

The simple answer is to pipe the results of your get-service|select-object into the ConvertTo-HTML cmdlet and include the result as the message body in a message formatted as HTML in Send-MailMessage.

1 Vote 1 ·