Link to home
Start Free TrialLog in
Avatar of Member_2_7966113
Member_2_7966113

asked on

Powershell Modification Requests

Hello Experts,

Can someone please help me modify the attached Powershell script so that it will:

1. Execute all the functions without me having to type them out. For example, if you do a Get-Help Test-TcpConnection -Full you will see that in order to Tests HTTP connectivity on the server 'myserver' I will need to type 'Test-TcpConnection -Name 'myserver' -PortNumber 80'. However, I would the script to execute the function when I run the script.

2. Send me an email if Test-TcpConnection fails for any of the parameters.

This will be much appreciated.

Cheers
Test-TcpConnection.txt
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

Add this to script and run after importing Test-TcpConnection cmdlet
Import-Module .\Test_TcpConnection.ps1
$ServerName = 'myserver'
$PortNumber = 80
Test-TcpConnection -Name $ServerName -PortNumber $PortNumber
Send-MailMessage -From "User01 <user01@example.com>" -To "User02 <user02@example.com>", "User03 <user03@example.com>" -Subject "Sending the Attachment" -Body "Forgot to send the attachment. Sending now." -Attachments "data.csv" -Priority High -dno onSuccess, onFailure -SmtpServer "smtp.fabrikam.com"

Open in new window

Avatar of Member_2_7966113
Member_2_7966113

ASKER

Hi Shaun,

Thanks for getting back to me.

I should first mention, that I'm pretty much a novice with Powershell. So I did what I thought you suggested and I got the following error screen

User generated image
Hi Shaun,

As you can see I'm bit of a novice, so any help you can provide will be greatly appreciated.

Cheers
Sorry, command should read

.\Test_TcpConnection.ps1
Import-Module .\Test_TcpConnection.ps1
$ServerName = 'myserver'
$PortNumber = 80
Test-TcpConnection -Name $ServerName -PortNumber $PortNumber
Send-MailMessage -From "User01 <user01@example.com>" -To "User02 <user02@example.com>", "User03 <user03@example.com>" -Subject "Sending the Attachment" -Body "Forgot to send the attachment. Sending now." -Attachments "data.csv" -Priority High -dno onSuccess, onFailure -SmtpServer "smtp.fabrikam.com"

Open in new window

That is still incorrect ;-).
The script assumes the Test_TcpConnection.ps1 is located in the current direectory (the folder the PS prompt shows). If not, provide the full path instead of .\.
I'll assume you do want to change only the server to test (if anything), and always keep the port at HTTP.
. .\Test_TcpConnection.ps1
$ServerName = 'Server1'
if (!(Test-TcpConnection -ComputerName $ServerName -PortNUmber 80 -Quiet))
{
  Send-MailMessage -SmtpServer mail.domain.com -From me@domain.com -To you@domain.com ˋ
       -Subject "TCP Test for $ServerName failed"
}

Open in new window

Hi Shaun

When you say add it to the script do you mean cut what you have done and paste it to the end of the script I uploaded?

Cheers
  • Create a new PS1 script and past this inside
.\Test_TcpConnection.ps1
Import-Module .\Test_TcpConnection.ps1
$ServerName = 'myserver'
$PortNumber = 80
Test-TcpConnection -Name $ServerName -PortNumber $PortNumber
Send-MailMessage -From "User01 <user01@example.com>" -To "User02 <user02@example.com>", "User03 <user03@example.com>" -Subject "Sending the Attachment" -Body "Forgot to send the attachment. Sending now." -Attachments "data.csv" -Priority High -dno onSuccess, onFailure -SmtpServer "smtp.fabrikam.com"

Open in new window

  • Copy Test_TcpConnection.ps1 into same location
  • Then run new script to execure
Shaun, did you bother to compare your code with mine, to see where you are totally wrong with?
As I said in previous comment
Copy Test_TcpConnection.ps1 into same location
and you can use working path if you schedule this
User generated image
Hi Guys,

I've created the new .ps1 as shown in the Top Pane of the image. However, I'm getting the error in the bottom pane.

Any thoughts?

RegardsUser generated image
As mentioned above, copy Test_TcpConnection.ps1 into same location and change your working path
-OR-
change .\Test_TcpConnection.ps1 to full path
In your case your working path is "C:\Users\pattec01\Google Drive\AZURE\POWERSHELL\SCRIPTS"
Run
cd  "C:\Users\pattec01\Google Drive\AZURE\POWERSHELL\SCRIPTS"

Open in new window

You can even add it to the top of the script
OK, I need to be straight with you. I managed to get the script to run with you helpful suggestions, however I wanted to figure out how to make the script run without having to reference a PATH - i.e. just execute the function when it is run. This is because I'm copying the script in what is called a Microsoft Azure Grahpical runbookhttps://docs.microsoft.com/en-us/azure/automation/automation-first-runbook-graphical

Therefore, if I can get the function to execute at the time the script is RUN that would be great.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It is not a module ergo import-module will fail.
Modules must be in the $env:psmodulepath

in one of the psmodulepath directories create a folder called
test-tcpconnection
copy test-tcpconnection.ps1 to this directory and rename it test-tcpconnection.psm1

now you can import the module and no need for the .\test-tcpconnection.ps1

This should autoload so you can omit the import-module
Fantastic. This worked like a dream.

Cheers