OCSItool_insiders.psm1

#OCSI Tool Insiders


Function OMtool{

$menu=Read-Host -Prompt "
                 Main Menu
=========================================
 
1. OP Generate Tool
2. Log Collectors
3. OOS Farm Rebuilder
4. OOS Farm Rebuilder(multi-server farm)
5. Install OOS
6. Quit
 
========================================
"

Clear-Host
Switch($menu){
1{$choice = OPGenerate}
2{$choice = Logsmenu}
3{$choice = Rebuild}
4{$choice = Rebuildmulti}
5{$choice = installoos}
6{$choice = "QUIT"}
}
$choice
}
Function Logsmenu{
$logs=Read-Host -Prompt "
 
      LogCollectors MENU
================================
1.OOS Log collector
2.Office ULS&CSI log collector
3.Return to main menu
4.Quit
================================
 
"

Clear-Host
switch($logs){
1{$choice=oosCollector}
2{$choice=CSIlog}
3{$choice=omtool}
4{$choice="QUIT"}
}
$choice
}
Function CSIlog{
#CSI log collector
$PSDefaultParameterValues['*:verbose'] = $true
$qg=net localgroup "Performance Log Users"
$user=whoami
$user1=$user -creplace '^[^\\]*\\', ''
function menu2{
$choice = Read-Host -Prompt "
              CSI Menu
=====================================
1. Set variables for CSI logging
2. Export logs
3. Cleanup
4. Restart machine
5. Return to main menu
=====================================
"

Clear-Host
switch ($choice){
1{$choice=log}
2{$choice=export}
3{$choice=cleanup}
4{$choice=Restart-computer -force}
5{$choice=OMtool}
}
$choice

}
function log{
#Add user to local group cmd
net localgroup "Performance Log Users" "$user" /add 
#New-Item -Path "C:\" -Name "MsLogs" -ItemType "Directory" -ErrorAction SilentlyContinue
#set registry entries

#NEW-Itemproperty -path 'HKLM:\System\CurrentControlSet\Control\Lsa' -Name 'LmCompatibilityLevel' -propertytype DWORD -value '1'
$regpath= "HKCU:\Software\Microsoft\Office\16.0\Common\Logging\"
$regcheck = Test-Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging\"

if ($regcheck -eq 0){New-Item –Path "HKCU:\Software\Microsoft\Office\16.0\Common\" -Name 'Logging'} 

if (Get-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging\" -Name 'MsoEtwTracingEnabled' -ErrorAction SilentlyContinue){Write-host -f green "DWORD MsoEtwTracingEnabled exists"} else {New-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging\" -Name 'MsoEtwTracingEnabled' -PropertyType DWORD -Value '1'}
if(get-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging" -Name 'EnableLogging' -ErrorAction SilentlyContinue){Write-host -f green "DWORD Enable Logging exists"}else{New-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging" -Name 'EnableLogging' -PropertyType DWORD -Value '0xd'}
if(Get-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging" -Name 'DefaultMinimumSeverity' -ErrorAction SilentlyContinue){Write-host -f green "DWORD DefaultMinimumSeverity exists"}else{New-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging" -Name 'DefaultMinimumSeverity' -PropertyType DWORD -Value '64'}
if(get-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging" -Name 'LogPath' -ErrorAction SilentlyContinue){Write-host -f green "String Log Path exists"}else{New-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging" -Name 'LogPath' -PropertyType string -value 'C:\MsLogs'}
if(get-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\FileIO" -Name 'PIIScrubbingDisabled' -ErrorAction SilentlyContinue){Write-host -f green "DWORD PIIScrubbingDisabled exists"}else{New-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\FileIO" -Name 'PIIScrubbingDisabled' -PropertyType dword -Value '1'}
#call menu
menu2
}
function cleanup{
#Remove user from local group cmd
if ($qg -contains $user){
net localgroup "Performance Log Users" "$user" /delete
}
#remove registry entries

#remove-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging\" -Name 'MsoEtwTracingEnabled'
#remove-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging" -Name 'EnableLogging'
#remove-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging" -Name 'DefaultMinimumSeverity'
Remove-Item -Path "HKCU:\Software\Microsoft\Office\16.0\Common\Logging"
Remove-ItemProperty –Path "HKCU:\Software\Microsoft\Office\16.0\Common\FileIO" -Name 'PIIScrubbingDisabled'
#call menu
menu2

}
function export{

#create a new folder under C:\, copy the logs and zip them
New-Item -Path "C:\MsLogs\" -Name "CSI" -ItemType "Directory"

cp -path "$env:localAPPDATA\Microsoft\Office\MsoLogs" -destination "C:\MSlogs\CSI"
Add-Type -assembly "system.io.compression.filesystem"
    [io.compression.zipfile]::CreateFromDirectory("C:\MSlogs", "C:\MSlogs.zip")

#call menu
menu2

}
menu2

}
Function OPgenerate {
        #Get the internal URL of the farm and trim the output to extract the URL
        Write-Progress -Activity 'OP Generate' -Status 'Starting OP Generate test...' -PercentComplete 0
        $url=get-officewebappsfarm|select-object internal*
        $url1=$url -replace ".*: ","" -replace ".*=", "" -replace "}", ""

        Write-Progress -Activity 'OP Generate' -Status 'Getting test file name...' -PercentComplete 0
        $file=Read-Host -Prompt "Input file name including extension (i.e. test.docx)"
        $name=hostname
        
        #Create the Source folder for the test and share it with Everyone
        Write-Progress -Activity 'OP Generate' -Status 'Creating the local shared directory' -PercentComplete 20
        New-Item "C:\test" -type directory | Out-Null;
        New-SMBShare -Name "test" -Path "C:\test" -fullaccess "everyone" | Out-Null;
        Read-Host "copy the file to C:\test. Press any key to continue when ready...";
        #Generate the link and open it in Explorer
        Write-Progress -Activity 'OP Generate' -Status 'Generating the test link...' -PercentComplete 40
        $op="$url1"+"/op/view.aspx?src=%5C%5C"+"$name"+"%5Ctest%5C"+"$file"
        $opf= $op -replace "//", "/" -replace "http:/", "http://"
        Write-Progress -Activity 'OP Generate' -Status 'Testing the generated link...' -PercentComplete 60
        $IE=new-object -com internetexplorer.application
        $IE.navigate2("$opf")
        $IE.visible=$true
        Write-Progress -Activity 'OP Generate' -Status 'Initiating Cleanup...' -PercentComplete 80
        $cleanup=Read-Host -Prompt "Perform clean-up (y/n)?"
        if ($cleanup -eq "y"){Remove-Item -Recurse -Path C:\test -Force -Confirm:$false}
        Write-Progress -Activity 'OP Generate' -Status 'Finished!' -PercentComplete 100
        Omtool
    } 
Function oosCollector{
    Write-Progress -Activity 'OOS Log Collector' -Status 'Starting log collection...' -PercentComplete 0
    ##################Universalvariables################################
    $path = "C:\MSlogs\"
    if(test-path $path){Remove-Item $path -confirm} #if log path exist, do cleanup
    $export=New-Item -Path "C:\" -Name "Mslogs" -ItemType "Directory"
    ##################eventviewer logs##########################
    # Config
    Write-Progress -Activity 'OOS Log Collector' -Status 'Setting variables...' -PercentComplete 10
    $logFileName = Read-Host -Prompt "What application logs do you want (eg. System, Application etc.)" # Add Name of the Logfile (System, Application, etc)
    $exportFileName = $logFileName + (get-date -f yyyyMMdd) + ".evt"
    $logFile = Get-WmiObject Win32_NTEventlogFile | Where-Object {$_.logfilename -eq $logFileName}
    $logFile.backupeventlog($path + $exportFileName)|out-null
    
    ################OOS Logs ####################################
    Write-Progress -Activity 'OOS Log Collector' -Status 'Getting farm configuration...' -PercentComplete 20
    $farmconfig=$path+"Farmconfig.txt"
    $farm=Get-officewebappsfarm > $farmconfig
    Write-Progress -Activity 'OOS Log Collector' -Status 'Setting verbosity to verbose...' -PercentComplete 30
    $loglocation=Get-officewebappsfarm | select *loglocation
    set-officewebappsfarm -logverbosity verbose|Out-Null
    Write-Progress -Activity 'OOS Log Collector' -Status 'Restarting WACSM service...' -PercentComplete 40
    Restart-Service WACSM|Out-Null
    Read-Host "Have you reproduced the error?"
    Write-Progress -Activity 'OOS Log Collector' -Status 'Exporting OOS logs...' -PercentComplete 50
    $loglocationtrim= $loglocation -replace ".*-","" -replace "@","" -replace "{","" -replace "}","" -replace ".*=",""
    $latest = Get-ChildItem -Path $loglocationtrim | Sort-Object LastAccessTime -Descending | Select-Object -First 2 
    $ooslogs=$latest.name
    Write-Progress -Activity 'OOS Log Collector' -Status 'Setting the verbosity back to medium...' -PercentComplete 60
    Set-officewebappsfarm -LogVerbosity "" | Out-Null
    Write-Progress -Activity 'OOS Log Collector' -Status 'Restarting WACSM service...' -PercentComplete 70
    Restart-Service WACSM|Out-Null
    
    Write-Progress -Activity 'OOS Log Collector' -Status 'Exporting OOS logs...' -PercentComplete 80
    foreach($ooslog in $ooslogs){
    $logl= "$loglocationtrim\$ooslog"
    cp -path $logl -destination $path}
    
    ##################Archive logs##############################
    Write-Progress -Activity 'OOS Log Collector' -Status 'Checking log integrity and archiving...' -PercentComplete 90
    if ((test-path $farmconfig) -and (Test-Path ($path+$exportfilename)) -and (Test-Path ($path+$ooslog))) {
        Write-Host "The following logs had been exported:
         
        $farmconfig
        $exportfilename
        $ooslogs
         
        Archiving and cleaning up...
        "
     
    }
    else{Write-Host "Log Export incomplete, try again." -ForegroundColor Red}
    $destination = "C:\MSlogs.zip"
    If(Test-path $destination) {Remove-item $destination -confirm:$false}
    Add-Type -assembly "system.io.compression.filesystem"
    [io.compression.zipfile]::CreateFromDirectory($path, $destination) 
    if(Test-Path $destination){Remove-Item $path -Recurse -Force -Confirm:$false}
    Write-Progress -Activity 'OOS Log Collector' -Status 'Finished!' -PercentComplete 100

     Omtool
} 
Function Installoos{

Write-Progress -Activity 'OOS Installer' -Status 'Starting...' -PercentComplete 0
    $OOSmachine=read-host -prompt "Input OOS machine name"
    $checkoos=read-Host "Is this $oosmachine the correct Office server machine(y/n)?"
    if ($checkoos -eq 'y'){ Write-Host "Confirmed Office Server machine name" -ForegroundColor Green}
    else {
            $OOSmachine=read-host -prompt "Input OOS machine correct name" 
        }
Write-Progress -Activity 'OOS Installer' -Status 'Setting variables...' -PercentComplete 0
    $oossetup=Read-host -prompt "Input path to OOS setup.exe (no quotation marks!)"
    $checksetup= read-Host "Is this $oossetup the correct Office server install path (y/n)?" 
    if ($checksetup -eq 'y'){ Write-Host "Confirmed Office Online Server setup.exe path" -ForegroundColor Green}
    else {
            $OOSsetup=read-host -prompt "Input the correct Office server install path" 
        }

Write-Progress -Activity 'OOS Installer' -Status 'Creating OOS setup config...' -PercentComplete 30
        $oosconfig="C:\oosconfig.xml"
        #create OOS setup config
        invoke-command -ComputerName $OOSmachine -ScriptBlock {New-item $using:oosconfig -ItemType file}
        invoke-command -ComputerName $OOSmachine -ScriptBlock {Set-content $using:oosconfig '<Configuration>
            <Logging Type="verbose" Path="C:\tmp" Template="OOServerSetup(*).log"/>
            <Display Level="none" CompletionNotice="no" />
            <INSTALLLOCATION Value="C:\Program Files\Microsoft Office Online Server\"/>
            <Setting Id="USINGUIINSTALLMODE" Value="0"/>
            <Setting Id="SETUPTYPE" Value="CLEAN_INSTALL"/>
            <Setting Id="SETUP_REBOOT" Value="Never"/>
        </Configuration>'
}
        Invoke-Command -ComputerName $OOSmachine -ScriptBlock { Add-WindowsFeature Web-Server,Web-Mgmt-Tools,Web-Mgmt-Console,Web-WebServer,Web-Common-Http,Web-Default-Doc,Web-Static-Content,Web-Performance,Web-Stat-Compression,Web-Dyn-Compression,Web-Security,Web-Filtering,Web-Windows-Auth,Web-App-Dev,Web-Net-Ext45,Web-Asp-Net45,Web-ISAPI-Ext,Web-ISAPI-Filter,Web-Includes,NET-Framework-Features,NET-Framework-45-Features,NET-Framework-Core,NET-Framework-45-Core,NET-HTTP-Activation,NET-Non-HTTP-Activ,NET-WCF-HTTP-Activation45,Windows-Identity-Foundation,Server-Media-Foundation}
Write-Progress -Activity 'Farm Rebuilder' -Status 'Installing OOS...' -PercentComplete 70
        Invoke-Command -ComputerName $OOSmachine -ScriptBlock {Start-Process "$using:oossetup" -ArgumentList "/config C:\OOSconfig.xml" -Wait}
        
}

Function Rebuild{
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Starting...' -PercentComplete 10
        #setting dependecies
        add-pssnapin "microsoft.sharepoint.powershell"
        #$PSDefaultParameterValues['*:WhatIf'] = $true
        
        #global variables
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Setting the variables...' -PercentComplete 20
        $internalURL=Read-host -prompt "Input OOS internal URL"
        $newfarmcmd= read-host -prompt "Paste the new farm command"
        $script =[scriptblock]::Create("$newfarmcmd")
        $OOSmachine=read-host -prompt "Input OOS machine name"
        $checkoos=read-Host "Is this $oosmachine the correct Office server machine(y/n)?"
        if ($checkoos -eq 'y'){ Write-Host "Confirmed Office Server machine name" -ForegroundColor Green}
        else {
            $OOSmachine=read-host -prompt "Input OOS machine correct name" 
        }
        
        $oossetup=Read-host -prompt "Input path to OOS setup.exe (no quotation marks!)"
        $checksetup= read-Host "Is this $oossetup the correct Office server install path (y/n)?" 
        if ($checksetup -eq 'y'){ Write-Host "Confirmed Office Online Server setup.exe path" -ForegroundColor Green}
        else {
            $OOSsetup=read-host -prompt "Input the correct Office server install path" 
        }
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Creating OOS setup config...' -PercentComplete 30
        $oosconfig="C:\oosconfig.xml"
        #create OOS setup config
        invoke-command -ComputerName $OOSmachine -ScriptBlock {New-item $using:oosconfig -ItemType file}
        invoke-command -ComputerName $OOSmachine -ScriptBlock {Set-content $using:oosconfig '<Configuration>
            <Logging Type="verbose" Path="C:\tmp" Template="OOServerSetup(*).log"/>
            <Display Level="none" CompletionNotice="no" />
            <INSTALLLOCATION Value="C:\Program Files\Microsoft Office Online Server\"/>
            <Setting Id="USINGUIINSTALLMODE" Value="0"/>
            <Setting Id="SETUPTYPE" Value="CLEAN_INSTALL"/>
            <Setting Id="SETUP_REBOOT" Value="Never"/>
        </Configuration>'
}
        #SP Part
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Remove Sharepoint bindings...' -PercentComplete 40
        Remove-SPWOPIBinding -all
        
        #OWA/OOS part
        #Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND } -credential USERNAME
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Exporting current farm config...' -PercentComplete 50
        Invoke-Command -ComputerName $OOSmachine -ScriptBlock { Get-officewebappsfarm } > farmconfig.txt
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Removing the OOS machine from the farm...' -PercentComplete 60
        Invoke-Command -ComputerName $OOSmachine -ScriptBlock { Remove-officewebappsmachine }
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Getting Office installed products and uninstalling...' -PercentComplete 70
        $products = Get-WmiObject -Class win32_product -ComputerName $OOSmachine -Filter "Name like '%office%'"
        foreach($product in $products){Write-host "$product is uninstalling"; $product.Uninstall()}
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Restarting OOS machine...' -PercentComplete 80
        Invoke-Command -ComputerName $OOSmachine -ScriptBlock { Restart-Computer -Force}
        do{}
        while(!(Test-Connection -Cn $oosmachine -BufferSize 16 -Count 1 -ea 0 -quiet))
        start-sleep -seconds 30
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Installing OOS...' -PercentComplete 90
        Invoke-Command -ComputerName $OOSmachine -ScriptBlock {Start-Process "$using:oossetup" -ArgumentList "/config C:\OOSconfig.xml" -Wait}
        #Read-Host "Is the install finished(check on OOS in task manager whether setup.exe or msiexec are still running)?"
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Restarting OOS machine...' -PercentComplete 93
        Invoke-Command -ComputerName $OOSmachine -ScriptBlock { Restart-Computer -force}
        do{}
        while(!(Test-Connection -Cn $oosmachine -BufferSize 16 -Count 1 -ea 0 -quiet))
        start-sleep -seconds 30
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Rebuilding farm...' -PercentComplete 95
        Invoke-Command -ComputerName $oosmachine -ScriptBlock $script
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Testing /hosting/discovery...' -PercentComplete 97
        $IE=new-object -com internetexplorer.application
        $IE.navigate2("$internalURL"+"/hosting/discovery")
        $IE.visible=$true
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Recreating Sharepoint bindings...' -PercentComplete 99
        New-SPWOPIBinding -ServerName $oosmachine -AllowHTTP
        (Get-SPSecurityTokenServiceConfig).AllowOAuthOverHttp
        Write-Progress -Activity 'Farm Rebuilder' -Status 'Finished...' -PercentComplete 100
        
        Omtool
    }   
Function Rebuildmulti{

$PSDefaultParameterValues['*:verbose'] = $true
Write-Progress -Activity 'Rebuilding Farm' -Status 'Starting...' -PercentComplete 0

#setting dependecies
add-pssnapin "microsoft.sharepoint.powershell"
#Getting OOS machines and determining the master
Write-Progress -Activity "Rebuilding Farm" -Status 'Determining environment...' -PercentComplete 0
$oosinteraction = (Get-SPWOPIBinding -Application wordpdf -Action preloadview).servername
$oosmaster=  Invoke-Command -ComputerName $oosinteraction -ScriptBlock {(Get-officewebappsmachine).mastermachinename}
$oosmachines= Invoke-Command -ComputerName $OOSinteraction -ScriptBlock { (Get-OfficeWebAppsFarm).machines.machinename }
$ooschildren= $oosmachines|Where-Object{$_ -ne $oosmaster}
$internalURL=Read-host -prompt "Input OOS internal URL"
$newfarmcmd= read-host -prompt "Paste the new farm command"
$script =[scriptblock]::Create("$newfarmcmd")
Write-Progress -Activity 'Rebuilding Farm' -Status 'Starting...' -PercentComplete 10
Function Rebuild{

        Write-Progress -Activity "Rebuilding $ooschild" -Status "Rebuilding $ooschild" -PercentComplete 10
        
        #$PSDefaultParameterValues['*:WhatIf'] = $true
        
        #global variables
        Write-Progress -Activity "Rebuilding $ooschild" -Status 'Setting the variables...' -PercentComplete 20 
        $oossetup=Read-host -prompt "Input path to OOS setup.exe (no quotation marks!)"
        $checksetup= read-Host "Is this $oossetup the correct Office server install path (y/n)?" 
        if ($checksetup -eq 'y'){ Write-Host "Confirmed Office Online Server setup.exe path" -ForegroundColor Green}
        else {
            $OOSsetup=read-host -prompt "Input the correct Office server install path" 
        }
        Write-Progress -Activity "Rebuilding $ooschild" -Status 'Creating OOS setup config...' -PercentComplete 30
        $oosconfig="C:\oosconfig.xml"
        #create OOS setup config
        invoke-command -ComputerName $OOSchild -ScriptBlock {New-item $using:oosconfig -ItemType file}
        invoke-command -ComputerName $OOSchild -ScriptBlock {Set-content $using:oosconfig '<Configuration>
            <Logging Type="verbose" Path="C:\tmp" Template="OOServerSetup(*).log"/>
            <Display Level="none" CompletionNotice="no" />
            <INSTALLLOCATION Value="C:\Program Files\Microsoft Office Online Server\"/>
            <Setting Id="USINGUIINSTALLMODE" Value="0"/>
            <Setting Id="SETUPTYPE" Value="CLEAN_INSTALL"/>
            <Setting Id="SETUP_REBOOT" Value="Never"/>
        </Configuration>'
}
        
        #OWA/OOS part
        #Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND } -credential USERNAME
        Write-Progress -Activity "Rebuilding $ooschild" -Status 'Removing the OOS machine from the farm...' -PercentComplete 60
        Invoke-Command -ComputerName $OOSchild -ScriptBlock {Remove-officewebappsmachine}
        Write-Progress -Activity "Rebuilding $ooschild" -Status 'Getting Office installed products and uninstalling...' -PercentComplete 70
        $products = Get-WmiObject -Class win32_product -ComputerName $OOSchild -Filter "Name like '%office%'"
        foreach($product in $products){Write-host "$product.name is uninstalling"; $product.Uninstall()}
        Write-Progress -Activity "Rebuilding $ooschild" -Status 'Restarting OOS machine...' -PercentComplete 80
        Invoke-Command -ComputerName $OOSchild -ScriptBlock { Restart-Computer -Force}
        do{}
        while(!(Test-Connection -Cn $ooschild -BufferSize 16 -Count 1 -ea 0 -quiet))
        start-sleep -seconds 180
        Write-Progress -Activity "Rebuilding $ooschild" -Status 'Installing OOS...' -PercentComplete 90
        Invoke-Command -ComputerName $ooschild -ScriptBlock {Start-Process "$using:oossetup" -ArgumentList "/config C:\OOSconfig.xml" -Wait}
        #Read-Host "Is the install finished(check on OOS in task manager whether setup.exe or msiexec are still running)?"
        Write-Progress -Activity "Rebuilding $ooschild" -Status 'Restarting OOS machine...' -PercentComplete 93
        Invoke-Command -ComputerName $ooschild -ScriptBlock { Restart-Computer -force}
        do{}
        while(!(Test-Connection -Cn $ooschild -BufferSize 16 -Count 1 -ea 0 -quiet))
        start-sleep -seconds 180
        
        Write-Progress -Activity "Rebuilding $ooschild" -Status 'Finished...' -PercentComplete 100
        

}

foreach($ooschild in $ooschildren){
Rebuild
}

#Rebuilding Master
Write-Progress -Activity "Rebuilding Farm" -Status 'Starting...' -PercentComplete 70
$oossetup=Read-host -prompt "Input path to OOS setup.exe on $oosmaster (no quotation marks!)"
$checksetup= read-Host "Is this $oossetup the correct Office server install path (y/n)?" 
if ($checksetup -eq 'y'){ Write-Host "Confirmed Office Online Server setup.exe path" -ForegroundColor Green}
else {
    $OOSsetup=read-host -prompt "Input the correct Office server install path" 
}
Write-Progress -Activity 'Rebuilding Farm' -Status 'Creating OOS setup config...' -PercentComplete 30
$oosconfig="C:\oosconfig.xml"
#create OOS setup config
invoke-command -ComputerName $oosmaster -ScriptBlock {New-item $using:oosconfig -ItemType file}
invoke-command -ComputerName $oosmaster -ScriptBlock {Set-content $using:oosconfig '<Configuration>
    <Logging Type="verbose" Path="C:\tmp" Template="OOServerSetup(*).log"/>
    <Display Level="none" CompletionNotice="no" />
    <INSTALLLOCATION Value="C:\Program Files\Microsoft Office Online Server\"/>
    <Setting Id="USINGUIINSTALLMODE" Value="0"/>
    <Setting Id="SETUPTYPE" Value="CLEAN_INSTALL"/>
    <Setting Id="SETUP_REBOOT" Value="Never"/>
</Configuration>'
}
#SP Part
Write-Progress -Activity 'Rebuilding Farm' -Status 'Remove Sharepoint bindings...' -PercentComplete 40
Remove-SPWOPIBinding -all -confirm

#OWA/OOS part
#Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND } -credential USERNAME
Write-Progress -Activity 'Rebuilding Farm' -Status 'Exporting current farm config...' -PercentComplete 50
Invoke-Command -ComputerName $oosmaster -ScriptBlock { Get-officewebappsfarm } > farmconfig.txt
Write-Progress -Activity 'Rebuilding Farm' -Status 'Removing the OOS machine from the farm...' -PercentComplete 60
Invoke-Command -ComputerName $oosmaster -ScriptBlock { Remove-officewebappsmachine }
Write-Progress -Activity 'Rebuilding Farm' -Status 'Getting Office installed products and uninstalling...' -PercentComplete 70
$products = Get-WmiObject -Class win32_product -ComputerName $oosmaster -Filter "Name like '%office%'"
foreach($product in $products){Write-host "$product.name is uninstalling"; $product.Uninstall()}
Write-Progress -Activity 'Rebuilding Farm' -Status 'Restarting OOS machine...' -PercentComplete 80
Invoke-Command -ComputerName $oosmaster -ScriptBlock { Restart-Computer -Force}
do{}
while(!(Test-Connection -Cn $oosmaster -BufferSize 16 -Count 1 -ea 0 -quiet))
start-sleep -seconds 180
Write-Progress -Activity 'Rebuilding Farm' -Status 'Installing OOS...' -PercentComplete 90
Invoke-Command -ComputerName $oosmaster -ScriptBlock {Start-Process "$using:oossetup" -ArgumentList "/config C:\OOSconfig.xml" -Wait}
#Read-Host "Is the install finished(check on OOS in task manager whether setup.exe or msiexec are still running)?"
Write-Progress -Activity 'Rebuilding Farm' -Status 'Restarting OOS machine...' -PercentComplete 93
Invoke-Command -ComputerName $oosmaster -ScriptBlock { Restart-Computer -force}
do{}
while(!(Test-Connection -Cn $oosmaster -BufferSize 16 -Count 1 -ea 0 -quiet))
start-sleep -seconds 180
Write-Progress -Activity 'Rebuilding Farm' -Status 'Rebuilding farm...' -PercentComplete 95
Invoke-Command -ComputerName $oosmaster -ScriptBlock $script
Write-Progress -Activity 'Rebuilding Farm' -Status 'Testing /hosting/discovery...' -PercentComplete 97
$IE=new-object -com internetexplorer.application
$IE.navigate2("$internalURL"+"/hosting/discovery")
$IE.visible=$true
Write-Progress -Activity 'Rebuilding Farm' -Status 'Recreating Sharepoint bindings...' -PercentComplete 99
New-SPWOPIBinding -ServerName $oosmaster -AllowHTTP
(Get-SPSecurityTokenServiceConfig).AllowOAuthOverHttp
Write-Progress -Activity 'Rebuilding Farm' -Status 'Finished...' -PercentComplete 100

#rejoin machines
foreach($ooschild in $ooschildren){
Invoke-Command -ComputerName $ooschild -ScriptBlock {new-officewebappsmachine -machinetojoin $using:oosmaster}
}
}

Function OCSItool{
param([string]$tool = "unknown")

if($tool -eq "logs"){ Logsmenu}
elseif($tool -eq "csi"){CSIlog}
elseif($tool -eq "op"){opgenerate}
elseif($tool -eq "setcsi"){CSIlog -option setvar}


}

Export-ModuleMember -Function OMtool
Export-ModuleMember -Function opgenerate
Export-ModuleMember -Function ooscollector
Export-ModuleMember -Function rebuild
Export-ModuleMember -Function rebuildmulti
Export-ModuleMember -Function installoos
Export-ModuleMember -Function CSIlog
Export-ModuleMember -Function OCSItool