OCSItool.psm1
#OCSI Tool Insiders <# Predefine connection settings #> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy() [System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials $Global:bolRunningAsAdmin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).Groups -match "S-1-5-32-544") <# Defining control variable for permission checks #> Function admincheck{ If ($Global:bolRunningAsAdmin -eq $true) { write-host "Admin rights confirmed" -ForegroundColor Green } Else{Write-host "This module needs admin privileges. Please try opening powershell as an admin and try again." -ForegroundColor Red; [void][System.Console]::ReadKey($true);exit} } Function OCSItool{ param ([string]$tool = "unknown") admincheck if($tool -eq "logs"){ Logsmenu} elseif($tool -eq "csi"){CSIlog} elseif($tool -eq "op"){opgenerate} elseif($tool -eq "setcsi"){CSIlog -option setvar} $menu=Read-Host -Prompt " Main Menu ========================================= 1. OP Generate Tool 2. Log Collectors 3. OOS Farm Rebuilder 4. Install OOS 5. Quit ======================================== " Clear-Host Switch($menu){ 1{$choice = OPGenerate} 2{$choice = Logsmenu} 3{$choice = Rebuild} 4{$choice = installoos} 5{$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=OCSItool} 4{$choice="QUIT"} } $choice } Function CSIlog{ param([string]$options="unknown") #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=OCSItool} } $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" -Recurse 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" -Recurse Add-Type -assembly "system.io.compression.filesystem" [io.compression.zipfile]::CreateFromDirectory("C:\MSlogs", "C:\MSlogs.zip") #call menu menu2 } if($options -eq "setvar"){log} 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 OCSItool } 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 OCSItool } 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)) do { Write-Host "waiting for computer restart..." sleep 3} until((Test-Connection -Cn oos -BufferSize 16 -Count 1 -ea 0 -quiet) -eq "true" ) 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)) do { Write-Host "waiting for computer restart..." sleep 3} until((Test-Connection -Cn oos -BufferSize 16 -Count 1 -ea 0 -quiet) -eq "true" ) 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 OCSItool } Export-ModuleMember -Function OCSItool Export-ModuleMember -Function opgenerate Export-ModuleMember -Function ooscollector Export-ModuleMember -Function rebuild Export-ModuleMember -Function installoos Export-ModuleMember -Function CSIlog |