############################################################################### # Setup Global Variables ############################################################################### $global:domainFull = "matrix.contoso.com" $global:domainDn = "DC=matrix,DC=txstate,DC=edu" $global:domainNetbios = "txstate" $global:domainRoot = "contoso.com" $global:domainEmail = "contoso.com" $global:scriptAcct = "scriptadmin@contoso.com" $global:subject = "" $global:coreSystemsSqlNode = "mssqlnode1" $global:coreSystemsSqlInstance = "mssqldb1" $global:dotNetSqlNode = "mssqlnode3" $global:dotNetSqlInstance = "mssqldb3" $global:o365Tenant = "txst" $global:o365Acct = "sa-o365admin@txst.onmicrosoft.com" $global:educplusLicense = $o365tenant + ":STANDARDWOFFPACK_IW_FACULTY" $global:stuLicense = $o365tenant + ":STANDARDWOFFPACK_IW_STUDENT" $global:facLicense = $o365tenant + ":STANDARDWOFFPACK_IW_FACULTY" $global:retLicense = $o365tenant + ":EXCHANGE_STANDARD_ALUMNI" $global:guestLicense = $o365tenant + ":STANDARDWOFFPACK_IW_FACULTY" $global:e5License = $o365tenant + ":ENTERPRISEPREMIUM_FACULTY" $global:aadPremLicense = $o365tenant + ":AAD_PREMIUM" $global:projPremLicense = $o365tenant + ":PROJECTPREMIUM_FACULTY" $global:streamLicense = $o365tenant + ":STREAM" $global:o365Admin = $env:username + "@" + $domainRoot $global:operator = $env:username $global:smtpServer = "smtp.contoso.com" $global:cpServer = "crashplan.contoso.com" $global:cpAcct = "sa-crashplan-tasks" $global:cpAdminOrgUid = "0123456789" $global:cpHdUid = "0123456789" $global:cpDefaultUid = "DEFAULT_CUSTOMER_ORG_ID" ############################################################################### # Setup Environment - Part 1 ############################################################################### Import-Module ActiveDirectory -ErrorAction SilentlyContinue Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; $global:rand = New-Object System.Random ############################################################################### # SHARED FUNCTIONS # ############################################################################### ############################################################################### # Function Name: Select-TRServer # Description: Select random server from list ############################################################################### Function Select-TRServer { [CmdletBinding()] Param ( [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] [String[]]$serverList ) # Checks all servers to see if they are Alive $liveServerList = @() ForEach ($singleServer in $serverList) { $srvrName = $singleServer $connect = Test-Connection $srvrName # If the connection was successful add this DC to the array and close the connection If ($connect) { # Add the FQDN of the DC to the array $liveServerList += $srvrName # Close the TcpClient connection Write-Verbose "successfully tested $srvrName" } Else { Write-Verbose "$srvrName failed" } } Return $liveServerList } ############################################################################### # Function Name: Get-TRDC # Description: Sets up a connection to a random DC ############################################################################### Function Get-TRDC { $dcList = @() $errorActionPreference = "SilentlyContinue" Write-Verbose "Find Active DC" # Get DC to run script against $allDcs = ([System.DirectoryServices.ActiveDirectory.ActiveDirectorySite]::GetComputerSite()).Servers $dc = Select-TRServer $allDcs | Get-Random Return $dc } ############################################################################### # Function Name: Get-TRExch # Description: Sets up a remote session to a random Exchange transport server ############################################################################### Function Get-TRExch { $exchList = @() $errorActionPreference = "SilentlyContinue" Write-Verbose "Find Active Exchange server" # Get Exchange server to run script against $exchServers = (Get-ADGroupMember "Exchange Servers" | Where-Object {$_.objectclass -EQ "computer"}).Name $exchServer = Select-TRServer $exchServers | Get-Random Return $exchServer } ############################################################################### # Function Name: Write-TRLogFile # Description: Writes record to log file ############################################################################### Function Write-TRLogFile ($log, $message) { Add-Content $log $message } ############################################################################### # Function Name: Send-TRErrorLogFile # Description: Writes records to error log file ############################################################################### Function Write-TRErrorLogFile ($log, $message, $values) { Write-Verbose "Writing Error Log" $message | Out-File $log -Append -Width 1000 If ($values) { "Values: " + $values | Out-File $log -Append -Width 1000 } " " | Out-File $log -Append -Width 1000 } ############################################################################### # Function Name: Send-TRLogFile # Description: Emails log file ############################################################################### Function Send-TRLogFile ($emailTo, $emailFrom, $emailSubject, $emailBody, $emailAttach) { If ($emailAttach) { Write-Verbose "Send log, with attachment, to Windows Team" If (-NOT $whatIf -or $tstMail -EQ "mail") { Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -BodyAsHtml -Attachment $emailAttach -SmtpServer $smtpServer } } Else { Write-Verbose "Send log, no attachment, to Windows Team" If (-NOT $whatIf -or $tstMail -EQ "mail") { Send-MailMessage -To $emailTo -From $emailFrom -Subject $emailSubject -Body $emailBody -BodyAsHtml -SmtpServer $smtpServer } } } ############################################################################### # Function Name: Send-TRBCCLogFile # Description: Emails log file to BCC ############################################################################### Function Send-TRBCCLogFile ($emailTo, $emailFrom, $emailBcc, $emailSubject, $emailBody, $emailAttach) { If ($emailAttach) { Write-Verbose "Send log, with attachment, to Windows Team" If (-NOT $whatIf -or $tstMail -EQ "mail") { Send-MailMessage -To $emailTo -From $emailFrom -Bcc $emailBcc -Subject $emailSubject -Body $emailBody -BodyAsHtml -Attachment $emailAttach -SmtpServer $smtpServer } } Else { Write-Verbose "Send log, no attachment, to Windows Team" If (-NOT $whatIf -or $tstMail -EQ "mail") { Send-MailMessage -To $emailTo -From $emailFrom -Bcc $emailBcc -Subject $emailSubject -Body $emailBody -BodyAsHtml -SmtpServer $smtpServer } } }