Advertisement
Guest User

Untitled

a guest
Mar 9th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.13 KB | None | 0 0
  1. $ErrorActionPreference = "SilentlyContinue"
  2. $WarningActionPreference = "SilentlyContinue"
  3.  
  4. $CheckPS = Get-Command Get-Mailbox
  5. If ($CheckPS.Name -ne "Get-Mailbox")
  6. {Write-Host "This script must be run using the Exchange Management Shell. Please close this PowerShell session, open the Exchange Management Shell, and then run the configuration script again." -back red -for black
  7. $ErrorActionPreference = "Continue"
  8. exit}
  9.  
  10. $ErrorActionPreference = "Continue"
  11.  
  12. [Void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
  13. [Void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
  14.  
  15. # Start Exchange Services
  16.  
  17. $MBXServer = (Get-ExchangeServer | where-object {($_.ServerRole -like "Mailbox*") -and ($_.Name -eq $env:computername)}).Name
  18.  
  19. Get-Service -ComputerName $MBXServer | where-object {($_.Name -like "MSEx*") -and ($_.Name -notlike "MSExchangePop3*") -and ($_.Name -notlike "MSExchangeImap4*") -and ($_.Name -notlike "MSExchangeMonitoring")} | Start-Service
  20.  
  21. # Restart Certificate Services on Domain Controller
  22.  
  23. $DC = (Get-ADDomain).InfrastructureMaster
  24.  
  25. Invoke-Command -ComputerName $DC {Restart-Service CertSvc} | out-null
  26.  
  27. Start-Sleep -s 10
  28.  
  29. # Set child domain name
  30.  
  31. Do
  32. {
  33. $cdname = [Microsoft.VisualBasic.Interaction]::InputBox("Please type your five digit lab number. This number will become part of the on-premises domain used throughout the labs.", "Lab Number")
  34.  
  35. if (($cdname.length -eq 5) -and ($cdname -match '\d{5}'))
  36. {
  37. [Void][System.Windows.Forms.MessageBox]::Show("Your On-premises public domain name is `n `nLab$cdname.O365Ready.com" , "On-premises Domain Name" , 0)
  38. $LabDomain = ("Lab" + $cdname + ".O365Ready.com")
  39. }
  40. if ($cdname -eq "")
  41. {
  42. Write-Host "No value input detected or operation has been cancelled."
  43. exit
  44. }
  45. if (($cdname.length -ne 5) -or ($cdname -notmatch '\d{5}'))
  46. {
  47. [Void][System.Windows.Forms.MessageBox]::Show("The lab number must be a five digit value. Please retry your entry" , "Invalid entry" , 1, "Warning")
  48. }
  49.  
  50. }
  51.  
  52. While ($LabDomain -eq $null)
  53.  
  54. # Get Public IP address TMG1 from user input
  55.  
  56. [String]$IP = [Microsoft.VisualBasic.Interaction]::InputBox("Please type your organization's public IP address.", "Public IP Address")
  57.  
  58. if ($IP -notlike '*.*.*.*')
  59. {
  60. [Microsoft.VisualBasic.Interaction]::MsgBox("The IP address does not appear to be in the correct format. For example 192.168.0.10 or 10.0.1.50")
  61. [String]$IP = [Microsoft.VisualBasic.Interaction]::InputBox("Please type your organization's public IP address.", "Public IP Address")
  62. }
  63.  
  64. # Get Edge Server IP address from user input
  65.  
  66. [String]$EDG1PubIP = [Microsoft.VisualBasic.Interaction]::InputBox("Please type your Skype for Business Server Edge server's public IP address.", "Skype for Business Server Edge Public IP Address")
  67.  
  68. if ($EDG1PubIP -notlike '*.*.*.*')
  69. {
  70. [Microsoft.VisualBasic.Interaction]::MsgBox("The IP address does not appear to be in the correct format. For example 192.168.0.10 or 10.0.1.50")
  71. [String]$EDG1PubIP = [Microsoft.VisualBasic.Interaction]::InputBox("Please type your Skype for Business Server Edge server's public IP address.", "Skype for Business Server Edge Public IP Address")
  72. }
  73.  
  74.  
  75. # Create new accepted domain based on lab domain and set as authoritative
  76.  
  77. Write-Host "Creating a new accepted domain for your lab domain" -back yellow -for black
  78.  
  79. New-AcceptedDomain -Name "On-premises Lab Domain" -DomainType "Authoritative" -DomainName $LabDomain | out-null
  80.  
  81. Start-Sleep -s 10
  82.  
  83.  
  84. # Create e-mail address policy based on child domain and set as primary
  85.  
  86. Write-Host "Creating a new email address policy for your lab domain" -back yellow -for black
  87.  
  88. Set-EmailAddressPolicy -Identity "Default Policy" -EnabledEmailAddresstemplates "smtp:%m@contoso.local","SMTP:%m@$LabDomain"
  89.  
  90. Start-Sleep -s 5
  91.  
  92.  
  93. # Remove secondary SMTP address
  94.  
  95. Set-EmailAddressPolicy -Identity "Default Policy" -EnabledEmailAddresstemplates "SMTP:%m@$LabDomain"
  96.  
  97. Start-Sleep -s 5
  98.  
  99.  
  100. # Update e-mail address policy
  101.  
  102. Write-Host "Updating the organization's email address policy" -back yellow -for black
  103.  
  104. Update-EmailAddressPolicy -Identity "Default Policy"
  105.  
  106. Start-Sleep -s 5
  107.  
  108.  
  109. # Create a new DNS zone based on the Child domain
  110.  
  111. Write-Host "Creating DNS records for your lab domain" -back yellow -for black
  112.  
  113. $session = New-PSSession -ComputerName $DC
  114.  
  115.  
  116. # Create DNS zones
  117.  
  118. Invoke-Command -Session $session -ScriptBlock {
  119.  
  120. $ExchangeIP = ([System.Net.Dns]::GetHostAddresses("MBX1")).IPAddressToString
  121.  
  122. $SFE1IP = ([System.Net.Dns]::GetHostAddresses("SFE1")).IPAddressToString
  123.  
  124. $EDG1IP = ([System.Net.Dns]::GetHostAddresses("EDG1")).IPAddressToString
  125.  
  126. $SRV1IP = ([System.Net.Dns]::GetHostAddresses("SRV1")).IPAddressToString
  127.  
  128. [String]$Labdomain1 = $args
  129.  
  130. [String]$Rootdomain1 = $RootDomain
  131.  
  132. $IP = Get-NetIPAddress | Where-Object {($_.AddressFamily -eq "IPv4") -and ($_.IPAddress -ne "127.0.0.1")}
  133.  
  134.  
  135. # Create Lab Domain Zone
  136.  
  137. Add-DnsServerPrimaryZone -Name $Labdomain1 -ReplicationScope 'Domain'
  138.  
  139. Start-Sleep -s 5
  140.  
  141.  
  142. # Create new hosts in the lab domain DNS zones
  143.  
  144. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name $LabDomain1 -IPv4Address $ExchangeIP
  145.  
  146. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name mail -IPv4Address $ExchangeIP
  147.  
  148. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name EDG1 -IPv4Address $EDG1IP
  149.  
  150. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name lyncdiscoverinternal -IPv4Address $SFE1IP
  151.  
  152. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name admin -IPv4Address $SFE1IP
  153.  
  154. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name fs -IPv4Address $SRV1IP
  155.  
  156. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name meet -IPv4Address $SFE1IP
  157.  
  158. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name dialin -IPv4Address $SFE1IP
  159.  
  160. Add-DnsServerResourceRecord -ZoneName $LabDomain1 -Srv -Name "_sipinternaltls._tcp" -DomainName "sip.$LabDomain1" -Priority 0 -Weight 0 -Port 5061
  161.  
  162. Add-DnsServerResourceRecord -ZoneName $LabDomain1 -Srv -Name "_autodiscover._tcp" -DomainName "mail.$LabDomain1" -Priority 0 -Weight 0 -Port 443
  163.  
  164. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name sip -IPv4Address $SFE1IP
  165.  
  166. Add-DnsServerResourceRecordA -ZoneName $LabDomain1 -Name redpool -IPv4Address $SFE1IP
  167.  
  168. Add-DnsServerResourceRecordA -ZoneName $env:USERDNSDOMAIN -Name mail -IPv4Address $ExchangeIP
  169.  
  170. Add-DnsServerResourceRecordA -ZoneName $env:USERDNSDOMAIN -Name TMG1 -IPv4Address 192.168.0.254
  171.  
  172. Add-DnsServerResourceRecordA -ZoneName $env:USERDNSDOMAIN -Name sip -IPv4Address $SFE1IP
  173.  
  174. Add-DnsServerResourceRecordA -ZoneName $env:USERDNSDOMAIN -Name int-meetings -IPv4Address $SFE1IP
  175.  
  176. Add-DnsServerResourceRecordA -ZoneName $env:USERDNSDOMAIN -Name redpool -IPv4Address $SFE1IP
  177.  
  178. Add-DnsServerResourceRecordA -ZoneName $env:USERDNSDOMAIN -Name admin -IPv4Address $SFE1IP
  179.  
  180. #Add-DnsServerResourceRecordA -ZoneName $env:USERDNSDOMAIN -Name SRV2 -IPv4Address 192.168.0.195
  181.  
  182. Start-sleep -s 5
  183.  
  184. } -args $LabDomain
  185.  
  186.  
  187. # Create Lab Organizational Units
  188.  
  189. Write-Host "Creating new organizational units" -back yellow -for black
  190.  
  191. New-ADOrganizationalUnit -Name Accounts | out-null
  192.  
  193. New-ADOrganizationalUnit -Name Managers | out-null
  194.  
  195. New-ADOrganizationalUnit -Name Online | out-null
  196.  
  197.  
  198. # Add the Child domain as a UPN Suffix
  199.  
  200. Write-Host "Creating the UPN suffix for your lab domain" -back yellow -for black
  201.  
  202. $usn = "$LabDomain"
  203. $root = [ADSI]"LDAP://rootDSE"
  204. $conf = [ADSI]"LDAP://cn=partitions,$($root.configurationNamingContext)"
  205. $conf.uPNSuffixes += $usn
  206. $conf.SetInfo()
  207.  
  208. Start-Sleep -s 10
  209.  
  210.  
  211. # Setting the lab domain as the default accepted domain
  212.  
  213. Write-Host "Setting your lab domain as the default accepted domain" -back yellow -for black
  214.  
  215. Set-AcceptedDomain -Identity "On-premises Lab Domain" -MakeDefault $true | out-null
  216.  
  217. Start-Sleep -s 5
  218.  
  219.  
  220. # Create Mailboxes
  221.  
  222. Write-Host "Creating user mailboxes" -back yellow -for black
  223.  
  224. # Set Mailbox Database name variable
  225.  
  226. $MDBName = (Get-MailboxDatabase -Server $MBXServer).Name
  227.  
  228. # Mail Enable Adminsitrator
  229.  
  230. Enable-Mailbox -Identity Administrator -Alias Administrator -Database "$MDBName" | out-null
  231.  
  232. # Set Administrator account UPN suffix
  233.  
  234. Set-Mailbox -Identity Administrator -UserPrincipalName Administrator@$LabDomain | out-null
  235.  
  236. # Set Administrator mailbox language and location
  237.  
  238. Set-MailboxRegionalConfiguration -Identity Administrator -TimeZone "Pacific Standard Time" -Language en-us -ErrorAction SilentlyContinue
  239.  
  240. # Update Offline address book
  241.  
  242. Update-OfflineAddressBook -Identity (Get-OfflineAddressBook).Name
  243.  
  244. Start-Sleep -s 5
  245.  
  246. # Create users and mail enabled Users and set password to never expire
  247.  
  248. Import-Csv "DemoMailboxes.csv" | ForEach-Object {$Password = ConvertTo-SecureString $_.Password -asPlainText -force; New-Mailbox -Alias $_.Alias -FirstName $_.Fname -LastName $_.Lname -Name $_.name -userPrincipalName ($_.Alias + "@$LabDomain") -OrganizationalUnit $($env:USERDNSDOMAIN + $_.OU) -Password $Password -ResetPasswordOnNextLogon $False -Database "$MDBName" | out-null}
  249.  
  250. Start-Sleep -s 3
  251.  
  252. Import-Csv "DemoMailboxes.csv" | ForEach-Object {(Set-ADUser $_.Alias -PasswordNeverExpires $True);(Set-MailboxRegionalConfiguration -Identity $_.Alias -TimeZone "Pacific Standard Time" -Language en-us -ErrorAction SilentlyContinue)}
  253.  
  254. $count =0
  255. $imagepath = ".\DemoUsersPictures\"
  256. $csv = Import-Csv "DemoMailboxes.csv"
  257.  
  258. foreach($line in $csv)
  259. {
  260. $user = $photo = $image = $null
  261. # write-host $line.Name,$line.OfficePhone,$line.StreetAddress
  262.  
  263. # Need to calculate progress and update UI
  264. #write-progress -percentcomplete 25 -Activity Installing -Status Installing
  265.  
  266. $count=$count+1
  267.  
  268. try {
  269.  
  270. $user = Get-ADUser $line.Alias -Properties city,company,country,department,displayname,emailaddress,givenname,initials,manager,officephone,postalcode,state,streetaddress,surname,title,office,userprincipalname
  271. if ($user -ne $null)
  272. {
  273. $image=$imagepath+$line.Name+".jpg"
  274. $photo=[byte[]](Get-Content $image -Encoding byte)
  275. if ($photo -ne $null)
  276. {
  277. Set-ADUser $line.Alias -Replace @{thumbnailPhoto=$photo}
  278. }
  279. $user.City=$line.City
  280. $user.Company="Contoso"
  281. $user.Department=$line.Department
  282. $user.Office=$line.Office
  283.  
  284. $user.DisplayName=$line.Name
  285.  
  286. $user.GivenName=$line.Fname
  287. if ($line.MiddleInitial -ne "")
  288. {
  289. $user.Initials=$line.MiddleInitial
  290. }
  291.  
  292.  
  293. if ($line.Zip -ne "")
  294. {
  295. $user.PostalCode=$line.PostalCode
  296. }
  297.  
  298. if ($line.State -ne "")
  299. {
  300. $user.State=$line.State
  301. }
  302.  
  303. if ($line.CountryCode -ne "")
  304. {
  305. $user.Country=$line.CountryCode
  306. }
  307.  
  308. $user.OfficePhone=$line.OfficePhone
  309. $user.StreetAddress=$line.StreetAddress
  310. $user.Surname=$line.Lname
  311. $user.Title=$line.Title
  312.  
  313. Set-ADUser -instance $user
  314. if ($line.ManagerAlias -ne "")
  315. {
  316. Set-ADUser -Identity $line.Alias -Manager $line.ManagerAlias
  317. }
  318. }
  319.  
  320. }
  321. catch {}
  322.  
  323. }
  324.  
  325. Start-Sleep -s 3
  326.  
  327.  
  328. # Create Public and Private Certificate Requests
  329.  
  330. Write-Host "Creating the certificate request for your lab domain's public certificate" -back yellow -for black
  331.  
  332. $PubCert = New-ExchangeCertificate -FriendlyName "Lab Certificate" -GenerateRequest -SubjectName cn=sip.$LabDomain -DomainName $LabDomain,mbx1.$LabDomain,fs.$LabDomain,mail.$LabDomain,Lyncdiscover.$LabDomain,sip.$LabDomain,webconf.$LabDomain,meet.$LabDomain,dialin.$LabDomain,meetings.$LabDomain,admin.$LabDomain,poola.$LabDomain -PrivateKeyExportable $true
  333.  
  334. Set-Content -Path "C:\LabFiles\PubCertRequest.txt" -Value $PubCert
  335.  
  336. $PrivCert = New-ExchangeCertificate -FriendlyName "IM Certificate" -GenerateRequest -SubjectName cn=mail.$env:USERDNSDOMAIN -DomainName mail.$env:USERDNSDOMAIN,$($MBXServer + "." + $env:USERDNSDOMAIN),mail.$LabDomain -PrivateKeyExportable $true
  337.  
  338. Set-Content -Path "C:\Scripts\PrivCertRequest.req" -Value $PrivCert
  339.  
  340.  
  341. # Create external SMTP connector
  342.  
  343. Write-Host "Creating/Updating the SMTP connectors" -back yellow -for black
  344.  
  345. New-SendConnector -Name "External Email" -Usage Internet -AddressSpaces "SMTP:*;20" -SourceTransportServers $MBXServer | out-null
  346.  
  347.  
  348. # Update server URLs
  349.  
  350. Write-Host "Updating Exchange URLs for your lab domain" -back yellow -for black
  351.  
  352. Get-EcpVirtualDirectory | Set-EcpVirtualDirectory -ExternalURL https://mail.$LabDomain/ecp -WarningAction SilentlyContinue | out-null
  353.  
  354. Get-WebServicesVirtualDirectory | Set-WebServicesVirtualDirectory -ExternalURL https://mail.$LabDomain/EWS/Exchange.asmx -Force | out-null
  355.  
  356. Get-ActiveSyncVirtualDirectory | Set-ActiveSyncVirtualDirectory -ExternalURL https://mail.$LabDomain/Microsoft-Server-ActiveSync | out-null
  357.  
  358. Get-OabVirtualDirectory | Set-OabVirtualDirectory -ExternalURL https://mail.$LabDomain/OAB | out-null
  359.  
  360. Get-OwaVirtualDirectory | Set-OwaVirtualDirectory -ExternalUrl https://mail.$LabDomain/owa | out-null
  361.  
  362. Get-PowerShellVirtualDirectory | Set-PowerShellVirtualDirectory -ExternalURL https://mail.$LabDomain/powershell | out-null
  363.  
  364. Set-OutlookAnywhere -Identity "$MBXServer\rpc (Default Web Site)" -ExternalHostname mail.$LabDomain -ExternalClientsRequireSsl $False -ExternalClientAuthenticationMethod Negotiate -WarningAction SilentlyContinue | out-null
  365.  
  366. Get-ClientAccessServer | Set-ClientAccessServer -AutoDiscoverServiceInternalUri https://mail.$LabDomain/Autodiscover/Autodiscover.xml | out-null
  367.  
  368. Set-EcpVirtualDirectory "$MBXServer\ECP (Default Web Site)" -InternalUrl ((Get-EcpVirtualDirectory "$MBXServer\ECP (Default Web Site)").ExternalUrl) -WarningAction SilentlyContinue
  369.  
  370. Set-WebServicesVirtualDirectory "$MBXServer\EWS (Default Web Site)" -InternalUrl ((get-WebServicesVirtualDirectory "$MBXServer\EWS (Default Web Site)").ExternalUrl)
  371.  
  372. Set-ActiveSyncVirtualDirectory "$MBXServer\Microsoft-Server-ActiveSync (Default Web Site)" -InternalUrl ((Get-ActiveSyncVirtualDirectory "$MBXServer\Microsoft-Server-ActiveSync (Default Web Site)").ExternalUrl)
  373.  
  374. Set-OabVirtualDirectory "$MBXServer\OAB (Default Web Site)" -InternalUrl ((Get-OabVirtualDirectory "$MBXServer\OAB (Default Web Site)").ExternalUrl)
  375.  
  376. Set-OwaVirtualDirectory "$MBXServer\OWA (Default Web Site)" -InternalUrl ((Get-OwaVirtualDirectory "$MBXServer\OWA (Default Web Site)").ExternalUrl)
  377.  
  378. Set-PowerShellVirtualDirectory "$MBXServer\PowerShell (Default Web Site)" -InternalUrl ((Get-PowerShellVirtualDirectory "$MBXServer\PowerShell (Default Web Site)").ExternalUrl)
  379.  
  380. Set-OutlookAnywhere -Identity "$MBXServer\rpc (Default Web Site)" -InternalHostname mail.$LabDomain -InternalClientsRequireSsl $False -WarningAction SilentlyContinue
  381.  
  382. #Set KDS Root Key
  383.  
  384. #Add-KdsRootKey -EffectiveTime (Get-Date).AddHours(-10) | out-null
  385.  
  386. # Enable Office Web Apps Server
  387.  
  388. Set-OrganizationConfig -WACDiscoveryEndpoint https://was1.contoso.local/hosting/discovery -WarningAction SilentlyContinue | out-null
  389.  
  390. Set-OWAVirtualDirectory "$MBXServer\OWA (Default Web Site)" -WacViewingOnPublicComputersEnabled:$true -WacViewingOnPrivateComputersEnabled:$true -ForceWacViewingFirstOnPublicComputers $true -ForceWacViewingFirstOnPrivateComputers $true -WarningAction SilentlyContinue | out-null
  391.  
  392.  
  393. # Update SfBTopology tbxml
  394.  
  395. Write-Host "Creating Skype for Business Server Topology TBXML" -back yellow -for black
  396.  
  397. #winrm set winrm/config/client '@{TrustedHosts = "EDG1,SRV2,TMG1"}'
  398.  
  399. $username = "Admin"
  400.  
  401. $password = ConvertTo-SecureString 'Pa$$w0rd' -asplaintext -force
  402.  
  403. $cred = New-Object System.Management.Automation.PsCredential($username,$password)
  404.  
  405. New-CimSession -Name EDG1 -ComputerName EDG1 -Credential $cred -Authentication Negotiate | out-null
  406.  
  407. $EDG1ExtIP = (Get-NetIPAddress -AddressFamily IPv4 -CimSession (Get-CimSession) | where {$_.IPAddress -ne ("127.0.0.1") -and ($_.IPAddress -ne "192.168.0.15")}).IPAddress
  408.  
  409. (Get-Content \\SFE1\C$\Scripts\SkypeTopology.tbxml) | Foreach-Object {$_ -replace "contoso.rename","$LabDomain"} | Foreach-Object {$_ -replace "192.168.1.3","$EDG1ExtIP"} | Foreach-Object {$_ -replace "192.168.1.2","$EDG1PubIP"} | Foreach-Object {$_ -replace "SkypeTopology.tbxml","SkypeTopology$Labdomain.tbxml"} | Set-Content \\SFE1\C$\LabFiles\SkypeTopology$Labdomain.tbxml
  410.  
  411.  
  412. # Update TMG rules and DNS files
  413.  
  414. Write-Host "Updating TMG rules and creating your lab domain DNS zone and hosts" -back yellow -for black
  415.  
  416. New-PSDrive -Persist -Name T -PSProvider FileSystem -Credential $cred -Root \\TMG1\c$\LabFiles | out-null
  417.  
  418. (Get-Content T:\LabTMGRules.xml) | Foreach-Object {$_ -replace "XXXXX","$cdname"} | Set-Content T:\LabTMGRules-Lab$cdname.xml
  419.  
  420. (Get-Content T:\LabXXXXX.O365Ready.com.dns) | Foreach-Object {$_ -replace "XXXXX","$cdname"} | Foreach-Object {$_ -replace "192.168.1.1","$IP"} | Foreach-Object {$_ -replace "192.168.1.2","$EDG1PubIP"} | Set-Content T:\$LabDomain.dns
  421.  
  422. Remove-PSDrive T
  423.  
  424. # Create DNS zones
  425.  
  426. $sessionTMG = New-PSSession -ComputerName TMG1 -Credential $cred
  427.  
  428. Invoke-Command -Session $sessionTMG -ScriptBlock {
  429.  
  430. $Dnscmd = "Dnscmd /ZoneAdd $args /Primary /file $args.dns"
  431.  
  432. cmd.exe /c $Dnscmd | Out-Null
  433.  
  434. Stop-Service DNS
  435.  
  436. Copy-Item C:\LabFiles\$args.dns C:\Windows\System32\dns\$args.dns -Force
  437.  
  438. Start-Service DNS
  439.  
  440. } -args $LabDomain
  441.  
  442.  
  443. # Stop Exchange Services
  444.  
  445. Write-Host "Restarting Exchange Services" -back yellow -for black
  446.  
  447. $svcName = "MSExchangeADTopology"
  448.  
  449. # Get dependent services
  450. $depSvcs = Get-Service -Name $svcName -dependentservices | Where-Object {$_.Status -eq "Running"} |Select -Property Name
  451.  
  452. function StopDepServices {
  453.  
  454. # Check to see if dependent services are started
  455. if ($depSvcs -ne $null) {
  456. # Stop dependencies
  457. foreach ($depSvc in $depSvcs)
  458. {
  459. Stop-Service $depSvc.Name
  460. do
  461. {
  462. $service = Get-Service -name $depSvc.Name | Select -Property Status
  463. Start-Sleep -seconds 1
  464. }
  465. until ($service.Status -eq "Stopped")
  466. }
  467. }
  468. }
  469.  
  470.  
  471. function RestartServices {
  472.  
  473. # Restart service
  474. Restart-Service $svcName -force
  475. do
  476. {
  477. $service = Get-Service -name $svcName | Select -Property Status
  478. Start-Sleep -seconds 1
  479. }
  480. until ($service.Status -eq "Running")
  481.  
  482.  
  483. $depSvcs = Get-Service -name $svcName -dependentservices |Select -Property Name
  484.  
  485. # Check for Auto start flag on dependent services and start them even if they were stopped before
  486. foreach ($depSvc in $depSvcs)
  487. {
  488. $startMode = gwmi win32_service -filter "NAME = '$($depSvc.Name)'" | Select -Property StartMode
  489. if ($startMode.StartMode -eq "Auto") {
  490. Start-Service $depSvc.Name
  491. do
  492. {
  493. $service = Get-Service -name $depSvc.Name | Select -Property Status
  494. Start-Sleep -seconds 1
  495. }
  496. until ($service.Status -eq "Running")
  497. }
  498. }
  499. }
  500.  
  501.  
  502. StopDepServices
  503. Stop-Service MSExchangeIS
  504. Start-Sleep -s 5
  505.  
  506. RestartServices
  507.  
  508.  
  509. # Delete Index
  510.  
  511. $CopyStatus = Get-MailboxDatabaseCopyStatus
  512.  
  513. If ($CopyStatus.ConentIndexState -eq "FailedAndSuspended"){
  514.  
  515. Stop-Service MSExchangeFastSearch
  516. Stop-Service HostControllerService
  517.  
  518. $IndexFolder = Get-ChildItem -Path "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$MDBName" | Where-Object {($_.PSIsContainer) -and ($_.Name -like "*.Single")}
  519. Remove-Item "C:\Program Files\Microsoft\Exchange Server\V15\Mailbox\$MDBName\$IndexFolder" -Force -Recurse
  520.  
  521. Start-Service MSExchangeFastSearch
  522. Start-Service HostControllerService
  523.  
  524. }
  525.  
  526. # Complete
  527. ""
  528. ""
  529. Write-Host "Configuration complete" -back yellow -for black
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement