Archive for July, 2011

PowerShell Script Install-ExchPrereqs.ps1

Project Objective:
Reduce time spend installing Exchange 2010 prerequisites for various roles.

Problem to be solved:
Current method of setting prerequisites involves looking up a webpage. http://technet.microsoft.com/en-us/library/bb691354.aspx#WS08R2 This way you can save a few minutes per machine.

#***********************************************************************
# "Install-ExchPrereqs.ps1"
#
# Written by Aaron Wurthmann (aaron <AT> wurthmann <DOT> com)
#
# If you edit please keep my name as an original author and
# keep me apprised of the changes, see email address above.
# This code may not be used for commercial purposes.
# You the executor,runner,user accept all liability.
# This code comes with ABSOLUTELY NO WARRANTY.
# You may redistribute copies of the code under the terms of the GPL v2.
# -----------------------------------------------------------------------
# Prerequisite:
# Written for Exchange 2010 prerequisites for Windows Server 2008 R2
# -----------------------------------------------------------------------
# 2010.10.21 ver 1.0
#
# Summary:
# Installs Exchange 2010 prerequisites for Windows Server 2008 R2 uses
# required roll option to determine which prerequisites to install.
#
# From: http://technet.microsoft.com/en-us/library/bb691354.aspx#WS08R2
#************************************************************************

Param([string]$switch = 'help')

$Computer='.'
$ExpectedOS='Microsoft Windows Server 2008 R2*'

Function Get-Some-Help {
    write-host ''
    write-host 'Installs Exchange 2010 Prerequisites for Windows Server 2008 R2'
    write-host ''
    write-host 'Usage: Install-Exch-Prereqs.ps1 <options>' 
    write-host ''
    write-host 'Options:'
    write-host '    chtmu    Client Access, Hub Transport, Mailbox, and Unified Messaging server roles prerequisites'
    write-host '    cthm    Client Access, Hub Transport, and Mailbox server roles prerequisites'
    write-host '    cht    Client Access, and Hub Transport server roles prerequisites'
    write-host '    htm    Hub Transport, and Mailbox server roles prerequisites'
    write-host '    cm    Client Access, and Mailbox server roles prerequisites'
    write-host '    c    Client Access server role prerequisites'
    write-host '    ht    Hub Transport server role prerequisites'
    write-host '    m    Mailbox server role prerequisites'
    write-host '    u    Unified Messaging server role prerequisites'
    write-host '    et    Edge Transport role prerequisites'
    write-host ''
    exit(1)
}

Function Get-OS-Version {
    $OS=Get-WMIobject Win32_OperatingSystem -Comp $Computer | Select Caption,ServicePackMajorVersion
    If ($OS.Caption -notlike $ExpectedOS) {
        Get-Some-Help
    }
}

Get-OS-Version

Import-Module ServerManager

Switch ($switch) {
    
    #Default Get Some Help
    default {Get-Some-Help}
    
    #Client Access, Hub Transport, Mailbox, and Unified Messaging server roles.
    chtmu {
        Set-Service NetTcpPortSharing -StartupType Automatic
        Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server,Web-ISAPI-Ext,Web-Digest-Auth,Web-Dyn-Compression,NET-HTTP-Activation,RPC-Over-HTTP-Proxy,Desktop-Experience -Restart
    }
    
    #Client Access, Hub Transport, and Mailbox server roles.
    cthm {
    Set-Service NetTcpPortSharing -StartupType Automatic
    Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server,Web-ISAPI-Ext,Web-Digest-Auth,Web-Dyn-Compression,NET-HTTP-Activation,RPC-Over-HTTP-Proxy -Restart
    }

    #Client Access and Hub Transport server roles.
    cht {
    Set-Service NetTcpPortSharing -StartupType Automatic
    Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server,Web-ISAPI-Ext,Web-Digest-Auth,Web-Dyn-Compression,NET-HTTP-Activation,RPC-Over-HTTP-Proxy -Restart
    }

    #Hub Transport and Mailbox server roles.
    htm {Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server -Restart}

    #Client Access and Mailbox server roles.
    cm {
    Set-Service NetTcpPortSharing -StartupType Automatic
    Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server,Web-ISAPI-Ext,Web-Digest-Auth,Web-Dyn-Compression,NET-HTTP-Activation,RPC-Over-HTTP-Proxy -Restart
    }

    #Client Access server role only.
    c {
    Set-Service NetTcpPortSharing -StartupType Automatic
    Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server,Web-ISAPI-Ext,Web-Digest-Auth,Web-Dyn-Compression,NET-HTTP-Activation,RPC-Over-HTTP-Proxy -Restart
    }

    #Hub Transport or the Mailbox server role.
    ht {Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server -Restart}
    m {Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server -Restart}

    #Unified Messaging server role only.
    u {Add-WindowsFeature NET-Framework,RSAT-ADDS,Web-Server,Web-Basic-Auth,Web-Windows-Auth,Web-Metabase,Web-Net-Ext,Web-Lgcy-Mgmt-Console,WAS-Process-Model,RSAT-Web-Server,Desktop-Experience -Restart }

    #Edge Transport server role only.
    et {Add-WindowsFeature NET-Framework,RSAT-ADDS,ADLDS -Restart}
}

PowerShell Script Send-Email.ps1

Project Objective:
Send simple e-mail with or without credentials, with or without SSL encryption.

Problem to be solved:
My current scripts do not account for SMTP authorization being required on send. This script can be used as a standalone script or with minor changes a function.

Future Versions:
Since the primary purpose of this script was versatility I need to re-add the ability to send attachments. I removed the incomplete functionality from because at the moment I do not need to send attachments.

Notes:

This script (or one like it) was originally written by Rodney Fisk – xizdaqrian@gmail.com with aid from: Christian @ StackOverflow.com and Core @ StackOverflow.com

#
# Send-Email.ps1
#
# Written by Aaron Wurthmann (aaron (AT) wurthmann (DOT) com)
#
# If you edit please keep my name as an original author and
# keep me apprised of the changes, see email address above.
# This code may not be used for commercial purposes.
# You the executor, runner, user accept all liability.
# This code comes with ABSOLUTELY NO WARRANTY.
# You may redistribute copies of the code under the terms of the GPL v2.
# -----------------------------------------------------------------------
# 2011.07.05 ver 2.0
#
# Summary:
# Send simple e-mail with or without credentials, with or without SSL encryption.
# The primary purpose of this script was versatility.
# -----------------------------------------------------------------------
# Usage:
# This script can be used as a standalone script or with minor changes a function.
# Example:
# Send-Email.ps1 -from aaron.wurthmann@domain.local -to aaron_wurthmann@domain.local -Subject "Test Subject #42" -Body "Nice body!" -Server smtp.domain.local -Port 587 -User USERNAME -Pass PASSWORD -SSL True
# -----------------------------------------------------------------------
# Orgin:
# This script (or one like it) was originally written by Rodney Fisk - xizdaqrian@gmail.com
#     with aid from: Christian @ StackOverflow.com and Core @ StackOverflow.com
# -----------------------------------------------------------------------



# Mandatory = $true means that the parameter is required and if not included you will be prompted for it.
# Alias is just that, an alias to a parameter verses using the actual parameter name. I.G. "From" vs. "EmailFrom"
param(
    [Parameter(Mandatory = $true,
        Position = 0,
        ValueFromPipelineByPropertyName = $true)]
        [Alias('From')]
    [String]$emailFrom,

    [Parameter(Mandatory = $true,
        Position = 1,
        ValueFromPipelineByPropertyName = $true)]
        [Alias('To')]
    [String[]]$emailRecipients,

    [Parameter(Mandatory = $true,
        Position = 2,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Subject' )]
    [String]$emailSubject,

    [Parameter(Mandatory = $true,
        Position = 3,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Body' )]
    [String]$emailBody,

    [Parameter(Mandatory = $true,
        Position = 4,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Server' )]
    [String]$SMTPServer,

    [Parameter(Mandatory = $false,
        Position = 5,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Port' )]
    [Int]$SMTPPort=25,
    
    [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'User' )]
    [string]$SMTPUser,

    [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'Password' )]
    [string]$SMTPPass,
    
    [Parameter(Mandatory = $false,
        ValueFromPipelineByPropertyName = $true)]
        [Alias( 'SSL' )]
    $SMTPSSL
)

#Create an SMTP client using .NET
$SMTPClient = New-Object Net.Mail.SMTPClient( $SmtpServer, $SmtpPort ) 

#If SSL is needed per parameter above enable SSL on the SMTP client
if ($SMTPSSL) { $SMTPClient.EnableSSL = $true }
#If a Username and Password have been added per parameter above add to SMTP client
if ($SMTPUser -and $SMTPPass) { $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $SMTPUser, $SMTPPass ) }

#Create new message to be sent
$emailMessage = New-Object System.Net.Mail.MailMessage
#Add who will send the message to the message envelope
$emailMessage.From = $emailFrom
#Add Recipient(s) to the message envelope (Note that this method adds all recipients to the same message)
foreach ( $emailRecipient in $emailRecipients ) { $emailMessage.To.Add( $emailRecipient ) }

#Add Subject to message envelope
$emailMessage.Subject = $emailSubject
#Add Body to message envelope
$emailMessage.Body = $emailBody

#Using SMTP client send email message
$SMTPClient.Send( $emailMessage )
#Clear email message (helps clean up memory use)
$emailMessage.Dispose()