Install SMTP Server and Telnet with PowerShell

This post has been republished via RSS; it originally appeared at: SharePoint Support Blog articles.

Summary

 

SharePoint and general app servers often use the SMTP Service to integrate e-mail into the systems functionality. Since Windows supports a well vetted SMTP Service, using the built-in application is often the best choice. With the said, when testing your app functionality with e-mail, Telnet is almost always needed and not installed by default.

 

If you find yourself always rebuilding servers to test new features and functionality, you may install SMTP and Telnet manually each time. If this is the case, why not use PowerShell?

 

What does the script do?

 

If SMTP is not installed, the installation will proceed. Once completed, it will then detect if Telnet is already installed. If not, you will be asked if Telnet should be installed. If you run the script a server with SMTP or Telnet already installed, the app installation will be detected and skipped.

I'm hoping this script will make your test builds quicker and more enjoyable in the future.

 

The Script:

 

# Check if SMTP is installed before installing it.
 $smtp = get-WindowsFeature "smtp-server"
 if(!$smtp.Installed){
 write-host "SMTP is not installed, installing it..." -ForegroundColor red
 add-WindowsFeature $smtp
 }

else{
 write-host "SMTP is already installed!" -ForegroundColor Cyan
 sleep 2
 }

# Once SMTP is installed, prompt to install Telnet, if not installed.
 $telnet = get-WindowsFeature "telnet-client"
 if(!$telnet.Installed){

#Use a popup windows with a yes or no choice
 $shell = new-object -comobject "WScript.Shell"
 $choice = $shell.popup("Telnet is not installed, do you want to install it?",0,"Install Telnet",4+32)
 }

else{
 write-host "Telnet is already installed, done!" -ForegroundColor Green
 break
 }

#If you get here telnet is not installed and the user choose Yes.
 if ($choice -eq "6"){
 write-host "Installing Telnet..." -ForegroundColor Green
Add-WindowsFeature $telnet
 write-host "Done!" -ForegroundColor Cyan 
}

#If you got here telnet is not installed but the user chose No.
 if ($choice -eq "7" -and !$telnet.Installed){
 write-host "Telnet is not installed and will not be installed!" -ForegroundColor Green
 break
 }

 

The Output:

 

Here is what you will see if SMTP and Telnet is not installed:

 

021518_1638_InstallSMTP1.png

 

 

Windows Popup asking if Telnet should be installed:

 

021518_1638_InstallSMTP2.png

 

Installing Telnet:

 

021518_1638_InstallSMTP3.png

 

Done:

 

021518_1638_InstallSMTP4.png

 

If SMTP and Telnet is already installed it be detected and skip the installation:

 

021518_1638_InstallSMTP5.png

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.