Lab Setup Script – Part 5 – Advanced Features 2

With the 5th post in this series, I’ve added even more advanced features to the ever growing list:

  • Email population – custom text, attachments and more
  • Server setting configuration – message tracking, event log sizes, etc
  • DLP Fingerprinting configuration
  • Change Server URLs

The entire script (download link at the bottom) is getting a bit massive and is certainly my largest scrip to date. The Server Settings options, which was added for this part, was almost 230 lines of code. It is, however over 1600 lines now. So with one more part to complete I would imagine the script will crest 2000 lines at some point. This may require me to rewrite or to segment out into different scripts (I already call an XML script which is also another 200 lines by itself).

Additions for Part 5
Here are all the script changes for version 1.4 of the Lab Setup Script.

Email Population – Including Scheduling
Here I’ve added another large section of code and begun to work with a new way to generate a list of random mailboxes in an Exchange environment. Because of that change, this section is around 175 lines of new code as well.

# Menu Coding
create-emailps1
$decision = read-host "Do you want to run the PS1 file manually or schedule it"
if ($decision -eq "y") {
     schedule-emails
} else {
    write-host " "
    write-host "OK.  Make sure you run the script that you created!" -ForegroundColor yellow
    write-host " "
}

# Start random mailbox function
function random-mailboxes {
    # Prep Random mailboxes (version 1)
    $mbxcount = (get-mailbox | where {($_.RecipientTypeDetails -ne "DiscoveryMailbox") -and ($_.RecipientTypeDetails -ne "RoomMailbox")}).count
    $range =  get-random -minimum 0 -maximum $mbxcount
    $mailbox = (get-mailbox | where {($_.RecipientTypeDetails -ne "DiscoveryMailbox") -and ($_.RecipientTypeDetails -ne "RoomMailbox")}).primarysmtpaddress

    # Create Random User List
    $counter = 0
    $userlist = @()
    while ($counter -lt $range) {
        $position = get-random -minimum 0 -maximum $mbxcount
        $alias = $mailbox[$position].local+"@"+$mailbox[$position].domain
        $validation = $false
#        foreach ($line in $userlist) {
            if ($userlist -contains $alias) {
                $validation = $true
                $counter--
            } 
        $counter++
#      }
        if ($validation -eq $false) {
            # Put all the mailboxes into a list of unique results
            $global:userlist += @($alias)
        }
    }
}

Function create-emailps1 {
    $filepath = read-host "Enter the filename and path of the PS1 file to be run for email population [i.e. c:\temp\scripttorun.ps1]"
    $sendto = read-host "Do you want to send emails to (a) All mailboxes, (r) Random mailboxes or (c) use a CSV file"
    
    # Set the email server used for email relay
    $transport = (get-transportservice).name
    $smtpserver = $transport[0]

    if ($sendto -eq "a") {
        write-host " "
        write-host "This section is for sending test messages to all mailboxes" -ForegroundColor green
        write-host " "
        $noofcopies = read-host "How many test email messages to be sent per sender"
        $subject = read-host "Enter a subject line for the test message"
        $body = "This is a test message to help populate email in the mailboxes on Exchange 2013."
        $attachment = read-host "Specify an attachement for the test email"
        $mbxcount =  ($mailboxes = Get-Mailbox -Filter 'RecipientTypeDetails -eq "UserMailbox"' -resultsize unlimited).count
        $mbxcount--
        $mailboxes = (Get-Mailbox -Filter 'RecipientTypeDetails -eq "UserMailbox"' -resultsize unlimited).primarysmtpaddress
        # Create one line per mailbox
        foreach ($line in $mailboxes) {
            # Recipient - starts from the end of the mailbox variable
            $recipient = $mailboxes[$mbxcount]
            # Sender starts from the beginning of the mailbox variable
            $sender = $line
            $variable = '$counter'
            add-content $filepath "$variable = 0"
            add-content $filepath "while ('$counter' -lt $noofcopies) {"
            add-content $filepath "send-mailmessage -from '$sender' -to '$recipient' -subject '$subject' -body '$body' -smtpserver '$smtpserver' -attachment '$attachment'"
            add-content $filepath "$variable++"
            add-content $filepath "}"
            $mbxcount--
        }
    }
    if ($sendto -eq "r") {
        write-host " "
        write-host "This section is for sending test messages to random mailboxes" -ForegroundColor green
        write-host " "
        $noofcopies = read-host "How many test email messages to be sent per sender"
        $subject = read-host "Enter a subject line for the test message"
        $body = "This is a test message to help populate email in the mailboxes on Exchange 2013."
        $attachment = read-host "Specify an attachement for the test email"
        # Choose random mailboxes
        random-mailboxes
        $sender = $userlist
        random-mailboxes
        $recipient = $userlist
        # Create one line per mailbox
        foreach ($line in $sender) {
            foreach ($line2 in $recipient) {
                $sender = $line
                $recipient = $line2
                $variable = '$counter'
                add-content $filepath "$variable = 0"
                add-content $filepath "while ($variable -lt $noofcopies) {"
                add-content $filepath "send-mailmessage -from '$sender' -to '$recipient' -subject '$subject' -body '$body' -smtpserver '$smtpserver' -attachment '$attach'"
                add-content $filepath "$variable++"
                add-content $filepath "}"
            }
        }
    }
    if ($sendto -eq "c") {
        write-host " "
        write-host "This section is for sending test messages using a CSV file" -ForegroundColor green
        write-host " "

      # CSV format should be sender,recipient,subject,body,attachment,noofcopies
        $csvfile = read-host "Specify a CSV file to read from"
        $csv = import-csv $csvfile
        $filepath = read-host "Enter the filename and path of the PS1 file to be run for email population [i.e. c:\temp\scripttorun.ps1]"
        # $transport = "lab09-ex01"
        foreach ($line in $csv) {
            $sender = $line.sender
            $recipient = $line.recipient
            $subject = $line.subject
            $body = $line.body
            $attachment = $line.attachment
            $noofcopies = $line.noofcopies
            $variable = '$counter'
            add-content $filepath "$variable = 0"
            add-content $filepath "while ($variable -lt $noofcopies) {"
            add-content $filepath "send-mailmessage -from '$sender' -to '$recipient' -subject '$subject' -body '$body' -smtpserver '$smtpserver' -attachment '$attachment'"
            add-content $filepath "$variable++"
            add-content $filepath "}"
        }     
    }
    $global:filepath
}

# Schedule task
function schedule-emails {

$name = read-host "Name of the scheduled job in PowerShell"
$frequency = read-host "How often do you want the emails tobe sent [hourly, daily, weekly]"
$time = read-host "At what time should the task run (not hourly) [ie 12:00AM, 4:00PM]"

$range = read-host "Do you want this task to (1) run indefinately or (2) run for a period of time [1 or 2]"
if ($range -eq "2") {
	$start = read-host "Enter a start date [1/1/2015]"
	$stop = read-host "enter a stop date [4/1/15]"
    $duration = new-timespan -start $start -end $stop
} else {
    $duration = "([TimeSpan]::MaxValue)"
}

# Get days of the week for weekly task
if ($frequency -eq "weekly") {
    $weeks = read-host "Trigger the script every how many weeks [1 or 2 or 3 or...]"
	$numdays = read-host "How many days of the week do you want to schedule for[# 1 - 7]"
    $daysofweek = @()
	$counter = 0
        while ($counter -lt $numdays){
            $dow = read-host "Enter a number for the day of the week [Sunday, Monday, Tuesday, etc]"
            $counter++
            $daysofweek += ,@($dow)
    }
    $daysofweek
    $trigger = New-JobTrigger –Weekly –DaysOfWeek $daysofweek –At $time -weeksinterval $weeks
    $trigger | fl
}

if ($frequency -eq "hourly") {
    $hourly = read-host "Enter the number of hours between jobs"
    $startdate = read-host "Enter a start date [i.e. 1/1/2015]"
    $trigger = New-JobTrigger -Once -At "$startdate $time" -RepetitionInterval (New-TimeSpan -Hour $hourly) -RepetitionDuration ([TimeSpan]::MaxValue)
    $trigger | fl
}

if ($frequency -eq "daily") {
    $days = read-host "What is the interval (in days) that the script should run"
	$trigger = New-JobTrigger –daily –At $time -daysinterval $days
    $trigger |fl
}

Register-ScheduledJob -Name $name -FilePath $filepath -Trigger $trigger
}

First Scenario

In this first scenario, I asked for 2 copies of an email to go to random mailboxes in the environment. I was asked if I wanted to schedule the script to run and I decided to do this later. Also note you can run it manually from the script itself if you wish:

populateemail-1

Second Scenario

In this scenario I will select all mailboxes, choose not to schedule it, but instead run it via the script:

populateemail-2

Exchange Server Settings

This code section represents one of the largest blocks of code I’ve written to perform a certain function. At around 230 lines of code, it performs three ‘simple’ functions – set event log levels for one or more servers, set Message Tracking Log settings for one or more servers, and configure Outlook Anywhere for one or more servers. The variety of options and settings contributed to the length and some complexity to the script. Formatting, spacing and overall look/feel of the script also added to the lines of code. That was done mostly to make it easier for an administrator to work with the data required to make this code effective.

# Menu coding
     write-host " "
     write-host "This part of the script will configure event log sizes, message tracking log options, and Outlook Anywhere" -ForegroundColor yellow
     start-sleep 2
     Set-ServerSettings

# Start Server Settings function
function Set-ServerSettings {
    CLS
    $servers = (get-exchangeserver).name
    write-host "Do you want change any of your servers event log sizes [y or n]? " -ForegroundColor Green -NoNewline
    $seteventlogs = read-host 
    if ($seteventlogs -eq "y") {
        write-host " "
        write-host "Do you want to set the event logs for all servers the same size [y or n] " -ForegroundColor yellow -NoNewline
        $allorone = read-host     
        if ($allorone -eq 'y') {
                write-host " "
                $answer1 = read-host "Would you like to set the size of your System Log [y or n]"
                if ($answer1 -eq "y") {
                    write-host " "
                    $syssize = read-host "What size would you like for your System Event log [i.e. 100MB]"
                    $size = Invoke-expression $syssize
                    foreach ($line in $servers) {
                        limit-eventlog -computername $line "System" -MaximumSize $size
                    }
                }
                write-host " "
                $answer2 = read-host "Would you like to set the size of your Application Log [y or n]"
                if ($answer2 -eq "y") {
                    write-host " "
                    $appsize = read-host "What size would you like for your Application Event log [i.e. 100MB]"
                    $size = Invoke-expression $appsize 
                    foreach ($line in $servers) {
                        limit-eventlog -computername $line "Application" -MaximumSize $size
                    }
                }
                write-host " "
                $answer3 = read-host "Would you like to set the size of your Security Log [y or n]"
                if ($answer3 -eq "y") {
                    write-host " "
                    $secsize = read-host "What size would you like for your Security Event log [i.e. 100MB]"
                    $size = Invoke-expression $secsize
                    foreach ($line in $servers) {
                        limit-eventlog -computername $line "Security" -MaximumSize $size
                    }
                }
                write-host " "
                foreach ($line in $servers) {
                    $customeranswer = read-host "Would you like to modify custom log limits on the $line server [y or n]"
                    if ($customeranswer -eq "y") {
                        $num = read-host "How many custom logs will you need to modify"
                        $counter = 0
                        while ($counter -lt $num) {
                            $logname = read-host "What is the name of the log you wish to modify"
                            $logsize = read-host "Would you like to set the size of your $logname Log [i.e. 100MB]"
                            limit-eventlog -computername $line $logname -MaximumSize $logsize
                        }
                    }
                    write-host " "
                }
        } 
        if ($allorone -eq 'n') {
            foreach ($line in $servers) {
                write-host " "
                write-host "OK.  Let's set all the servers individually" -ForegroundColor Green
                write-host " "
                $answer1 = read-host "Would you like to set the size of your System Log on $line [y or n]"
                if ($answer1 -eq "y") {
                    $syssize = read-host "What size would you like for your System Event log"
                    #translate to Int64 for use with limit-eventlog cmdlet
                    $size = Invoke-expression $syssize
                    limit-eventlog -computername $line "System" -MaximumSize $size
                }
                write-host " "
                $answer2 = read-host "Would you like to set the size of your Application Log on $line [y or n]"
                if ($answer2 -eq "y") {
                    $appsize = read-host "What size would you like for your Application Event log"
                    #t ranslate to Int64 for use with limit-eventlog cmdlet
                    $size = Invoke-expression $appsize
                    limit-eventlog -computername $line "Application" -MaximumSize $size
                }
                write-host " "
                $answer3 = read-host "Would you like to set the size of your Security Log on $line [y or n]"
                if ($answer3 -eq "y") {
                    $secsize = read-host "What size would you like for your Security Event log"
                    # translate to Int64 for use with limit-eventlog cmdlet
                    $size = Invoke-expression $syssize
                    limit-eventlog -computername $line "Security" -MaximumSize $secsize
                }
                write-host " "
                $customeranswer = read-host "Would you like to modify custom log limits on the $line server [y or n]"
                if ($customeranswer -eq "y") {
                    write-host " "
                    $num = read-host "How many custom logs will you need to modify"
                    write-host " "
                    $counter = 0
                    while ($counter -lt $num) {
                        $logname = read-host "What is the name of the log you wish to modify"
                        $logsize = read-host "Would you like to set the size of your $logname Log"
                         #translate to Int64 for use with limit-eventlog cmdlet
                        $size = Invoke-expression $logsize
                        limit-eventlog -computername $line $logname -MaximumSize $size
                    }
                }
            }
        }
        write-host " "
        write-host "All Event logs have been modifed as specified." -ForegroundColor yellow
        write-host " "
    }

write-host "Do you want change any of your servers message tracking log settings [y or n]? " -ForegroundColor Green -NoNewline
$messagetrack = read-host 
if ($messagetrack -eq "y") {
    # Set the Message Tracking Log settings
    write-host " "
    write-host "Do you want to change the Message Tracking Log settings on your Exchange servers all the same [y or n] " -ForegroundColor yellow -NoNewline
    $Modify = read-host 
    $servers = (get-transportservice).name
    if ($modify -eq "y") {
        write-host " "
        $maxage = read-host "What is the max age setting you want to configure your tracking logs with for all of your Exchange Servers [dd.hh:mm:ss]"
        write-host " "
        $maxdirsize2 = read-host "What is the maximum directory size you want to setup for all of your Exchange Servers (in MB)"
        $maxdirsize =[string]$maxdirsize2+"MB"
        write-host " "
        $maxfilesize2 = read-host "What is the mazimum log fize size you wish to setup on all Exchange Servers (in MB)"
        $maxfilesize = [string]$maxfilesize2+"MB"
        foreach ($line in $servers) {
            set-transportservice -identity $line -MessageTrackingLogMaxAge $maxage -MessageTrackingLogMaxDirectorySize $maxdirsize -MessageTrackingLogMaxFileSize $maxfilesize
        }
    } else {
        foreach ($line in $servers) {
            write-host " "
            $maxage = read-host "What is the max age setting you want to configure your tracking logs with on $line [dd.hh:mm:ss]"
            write-host " "
            $maxdirsize = read-host "What is the maximum directory size you want to setup on $line (in MB)"
            $size = Invoke-expression $maxdirsize
            $maxfilesize = read-host "What is the maximum log fize size you wish to setup on $line (in MB)"
            $size2 = Invoke-expression $maxfilesize
            set-transportservice -identity $line -MessageTrackingLogMaxAge $maxage -MessageTrackingLogMaxDirectorySize $size -MessageTrackingLogMaxFileSize $size2
        }
    }
}    

write-host "Do you want change any of your servers Outlook Anywhere settings [y or n]? " -ForegroundColor Green -NoNewline
$OA = read-host
if ($OA -eq "y") {
    # Set the Outlook Anywhere settings
        write-host " "
        write-host "Do you want to change the Outlook Anywhere settings on your Exchange servers all the same [y or n] " -ForegroundColor Yellow -nonewline
        $OAdecision = read-host
        $servers = (get-exchangeserver).name
    if ($OAdecision -eq "y") {
            write-host " "
            $SSL = read-host "Configure SSL Offloading [t or f]"
            if ($ssl -eq "t") {$SSL = $true} else {$ssl = $false}
            $iisauth = read-host "What IIS Authenticaiton methods do you want configure"
            $internalname = read-host "What is the internal host name for Outlook Anywhere"
            $externalname = read-host "What is the external name for Outlook Anywhere"
            $permission = read-host "Do you want to set a (1) default permission or (2) split the internal and external permissions  [1 or 2]"
            if ($permission -eq "2") {
                    # Set external configuration
                    write-host "Enter the default permissions for external clients.  Acceptable answers are:"
                    write-host "Basic | Digest | Ntlm | Fba | WindowsIntegrated | LiveIdFba | LiveIdBasic | WSSecurity | Certificate | NegoEx | OAuth | Adfs | Kerberos | Negotiate | LiveIdNegotiate | Misconfigured" -ForegroundColor Green
                    write-host " "
                    $external = read-host "Your choice is"
                    # Set external SSL
                    $externalssl = read-host "Is SSL required for external connections (t or f)"
                    if ($externalssl -eq "t") {$externalssl = $true} else {$externalssl = $false}
                    write-host " "
                
                    # Set internal configuration
                    write-host "Enter the default permissions for internalclients"
                    write-host "Acceptable answers are " -nonewline
                    write-host "Basic | Digest | Ntlm | Fba | WindowsIntegrated | LiveIdFba | LiveIdBasic | WSSecurity | Certificate | NegoEx | OAuth | Adfs | Kerberos | Negotiate | LiveIdNegotiate | Misconfigured" -ForegroundColor Green
                    write-host " "
                    $internal = read-host "Your choice is"
                    # Set internal SSL
                    $internalssl = read-host "Is SSL required for internal connections (t or f)"
                    if ($internalssl -eq "t") {$internalssl = $true} else {$internalssl = $false}
                    write-host " "
                    get-outlookanywhere | set-OutlookAnywhere -ExternalClientAuthenticationMethod $external -ExternalClientsRequireSsl $externalSSL -SSLOffloading $ssl -ExternalHostName $externalname -IISAuthenticationMethods $iisauth -InternalClientsRequireSsl $internalSSL -internalhostname $internalname
                } else {
                    write-host "Enter the default permissions for internal and external clients"
                    write-host "Acceptable answers are " -nonewline
                    write-host "Basic | Digest | Ntlm | Fba | WindowsIntegrated | LiveIdFba | LiveIdBasic | WSSecurity | Certificate | NegoEx | OAuth | Adfs| Kerberos | Negotiate | LiveIdNegotiate | Misconfigured" -ForegroundColor Green
                    write-host " "
                    $default = read-host "Your choice is"
                    get-outlookanywhere | set-OutlookAnywhere -DefaultAuthenticationMethod $default -SSLOffloading $ssl -ExternalHostName $externalname -IISAuthenticationMethods $iisauth -internalhostname $internalname
            
                }

        } else {
            foreach ($line in $servers) {
                write-host " "
                write-host "Configure Outlook Anywhere for the server - $line." -ForegroundColor Green
                write-host " "
                $cam = read-host "What client authentiction method(s) would you like to configure for Outlook Anywhere"
                $SSL = read-host "Configure SSL Offloading [t or f]"
                if ($ssl -eq "t") {$SSL = $true} else {$ssl = $false}
                $iisauth = read-host "What IIS Authenticaiton methods do you want configure"
                $internalname = read-host "What is the internal host name for Outlook Anywhere"
                $externalname = read-host "What is the external name for Outlook Anywhere"
                $permission = read-host "Do you want to set a (1) default permission or (2) split the internal and external permissions  [1 or 2]"
                write-host " "
                if ($permission -eq "2") {
                    # Set external configuration
                    write-host "Enter the default permissions for external clients.  Acceptable answers are:"
                    write-host "Basic | Digest | Ntlm | Fba | WindowsIntegrated | LiveIdFba | LiveIdBasic | WSSecurity | Certificate | NegoEx | OAuth | Adfs | Kerberos | Negotiate | LiveIdNegotiate | Misconfigured" -ForegroundColor Green
                    write-host " "
                    $external = read-host "Your choice is"
                    # Set external SSL
                    $externalssl = read-host "Is SSL required for external connections (t or f)"
                    if ($externalssl -eq "t") {$externalssl = $true} else {$externalssl = $false}
                    write-host " "
                
                    # Set internal configuration
                    write-host "Enter the default permissions for internalclients"
                    write-host "Acceptable answers are " -nonewline
                    write-host "Basic | Digest | Ntlm | Fba | WindowsIntegrated | LiveIdFba | LiveIdBasic | WSSecurity | Certificate | NegoEx | OAuth | Adfs | Kerberos | Negotiate | LiveIdNegotiate | Misconfigured" -ForegroundColor Green
                    write-host " "
                    $internal = read-host "Your choice is"
                    # Set internal SSL
                    $internalssl = read-host "Is SSL required for internal connections (t or f)"
                    if ($internalssl -eq "t") {$internalssl = $true} else {$internalssl = $false}
                    write-host " "
                    set-OutlookAnywhere -server $line -ExternalClientAuthenticationMethod $external -ExternalClientsRequireSsl $externalSSL -SSLOffloading $ssl -ExternalHostName $externalname -IISAuthenticationMethods $iisauth -InternalClientsRequireSsl $internalSSL -internalhostname $internalname
                } else {
                    write-host "Enter the default permissions for internal and external clients. Acceptable answers are:"
                    write-host "Basic | Digest | Ntlm | Fba | WindowsIntegrated | LiveIdFba | LiveIdBasic | WSSecurity | Certificate | NegoEx | OAuth | Adfs| Kerberos | Negotiate | LiveIdNegotiate | Misconfigured" -ForegroundColor Green
                    write-host " "
                    $default = read-host "Your choice is"
                    set-OutlookAnywhere -server $line -DefaultAuthenticationMethod $default -SSLOffloading $ssl -ExternalHostName $externalname -IISAuthenticationMethods $iisauth -internalhostname $internalname
            }
        }
    }
    }
} # End Server Settings funtion

Sample code run through
Here are some scenarios I used to test with the code:

Outlook Anywhere Configuration:

ServerSettings1

Event Log Configuration:

ServerSettings2

Message Tracking Log Configuration:

ServerSettings3

DLP Fingerprinting

This section of code is used for creating transport rules based off of DLP Finger printing. More information on DLP fingerprinting can be found HERE. The code below simply takes a document, builds the fingerprint & data classification and applies it to a transport rule that you create.

# DLP Fingerprint function
function create-DLPfingerprint {
    CLS

    # Specify file for the DLP fingerprint
    $doc = read-host "Please enter the complete file path for the document you wsh to use for DLP Fingerprinting"
    $FingerPrint = Get-Content $doc -Encoding byte

    # Create the fingerprint
    write-host " "
    $description = read-host "Enter a description for the document to be fingerprinted"
    $DocumentFingerprint = New-Fingerprint -FileData $FingerPrint -Description $description

    # Create Dataclassification
    write-host " "
    $classification = read-host "Please enter a name for the new data classification"
    write-host " "
    $description2 = read-host "Please enter a description for the data classification"
    New-DataClassification -Name $classification -Fingerprints $DocumentFingerprint -Description $description2

    # Create transport rule based off the data classification
    write-host " "
    $rulename = read-host "Please enter a name for the transport rule related to DLP."
    write-host " "
    $NotifySender = read-host "Please enter a value for 'Notify Sender' [NotifyOnly | RejectMessage | RejectUnlessFalsePositiveOverride | RejectUnlessSilentOverride |
      RejectUnlessExplicitOverride]"
      write-host " "
    $mode = read-host "Please enter one of the following for the Transport Rule Mode [Audit | AuditAndNotify | Enforce]"
    write-host " "
    $sentToScope = read-host "For the Sent To Scope, please enter one of the following values [InOrganization | NotInOrganization | ExternalPartner | ExternalNonPartner]"
    New-TransportRule -Name $rulename -NotifySender $NotifySender -Mode $mode -SentToScope $senttoscope -MessageContainsDataClassification @{Name="$classification"}
    write-host " "
    Get-DataClassification $classification |ft
    write-host " "
    Get-TransportRule $rulename
    write-host " "
    write-host "Done creating the new Transport rule based off the DLP Fingerprint." -ForegroundColor green
} # End DLP Fingerprint function

Sample code run through

DLPFingerprinting1

Which ends up with the new data classification and transport rule being created:

DLPFingerprinting2

Changing Exchange Server URLs

This is a new and improved version of the URL script I’ve written before. This version is specific to Exchange 2013.

# Menu codng
23 { # Configure URLs
     Configure-URLs
}

# Start of the URL function
function  Configure-URLs {
    $FQDN = Read-Host 'Enter the URL you want to use for your Exchange URLs.'
    $FQDN2 = read-host "Enter the URL you want to use for AutoDiscover [ie autodiscover.mail.com]"
    write-host $FQDN" will be used for the common name of all virtual directories."
    start-sleep -seconds 10

    # To be used if setting all CAS the same
    $cas = get-clientaccessserver

    # To be used if setting only one CAS server up
    # $cas = Read-Host 'Enter the name of the CAS server you want to modify.'
    foreach ($line in $cas) {
        $casserver = $line.name
        write-host $casserver" will have it's URLs modified."
    }
    start-sleep -seconds 10

    # Set various VD - ews, oab, autodiscover, ecp, autodiscover, outlook anywhere, 

    write-host " "
    write-host "You picked "$FQDN" for the name in the environment." -ForegroundColor green
    write-host "Your FQDNS will be as follows for internal and external URLs:"
    write-host " "
    write-host "https://$FQDN/exchange.asmx"
    write-host "https://$FQDN/owa"
    write-host "https://$FQDN/Microsoft-Server-ActiveSync"
    write-host "https://$FQDN/oab"
    # Skip if pre-2010
    write-host "https://$FQDN/ecp"
    write-host " "
    write-host "Is this OK?  Settings will apply in 5 seconds.  Hit any key to abort." -ForegroundColor green
    write-host " "
    start-sleep 5

    foreach ($line in $cas) {
        # EWS setting
        Set-WebServicesVirtualDirectory -Identity "$line\EWS (Default Web Site)" -internalurl https://$FQDN/ews/exchange.asmx -externalurl https://$FQDN/ews/exchange.asmx -confirm:$false
        # OWA setting
        set-OWAVirtualDirectory -Identity "$line\owa (Default Web Site)" -InternalURL https://$FQDN/owa -externalurl https://$FQDN/owa -confirm:$false
        # ActiveSync setting
        Set-ActiveSyncVirtualDirectory -Identity "$line\Microsoft-Server-ActiveSync (Default Web Site)" -InternalURL https://$FQDN/Microsoft-Server-ActiveSync -externalurl https://$FQDN/Microsoft-Server-ActiveSync -confirm:$false
        #OAB setting
        Set-OabVirtualDirectory -Identity "$line\OAB (Default Web Site)" -InternalURL https://$FQDN/oab -externalurl https://$FQDN/oab
        # Set Client Access Server Internal Uri
        set-clientaccessserver $line -AutoDiscoverServiceInternalUri https://$fqdn/autodiscover/autodiscover.xml
        # ECP setting
        Set-EcpVirtualDirectory -Identity "$line\ecp (Default Web Site)" -InternalURL https://$FQDN/ecp -externalurl https://$FQDN/ecp -confirm:$false
    }
} # End URL configuration function 

Sample Run through of changing your URLs

URLs1

What’s Next?

In next to last article of the series, I will add Detailed Mobile Device Policies (*), Detailed OWA Policies (*), custom mailbox creation, PST Importing and OAB manipulation (CU7+).

(*) Removed from this article due to length of coding already put into this article (500+ lines of code)
Download

Here is the script, all ~ 1800 lines as well as the XML script that goes with it:

LabSetupScript-1.4
XMLScript-Lab-1.3

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s