Russ Maxwell has written, another, totally, awesome script! This one will help you when you’re troubleshooting an issue. Here’s a link to the original post
It works with SharePoint 2010-2016
It allows you to reproduce the issue with logging dialed up to Verbose.
If you want VerboseEx then edit line 239 from
set-sploglevel -TraceSeverity Verbose
to
set-sploglevel -TraceSeverity VerboseEx
You could also do a find and replace for Verbose and replace with VerboseEx and that way you’ll see the script tell you that it is setting VerboseEx, versus Verbose.
Then to use this bad boy, just save it into a .ps1, call the ps1 from an administrative powershell session, and it will convert the powershell session to a SharePoint Management Shell before it executes.
e.g c:\scripts .\AwesomeLogCorrelatorScript.ps1
When prompted for a path to store the logs, give the script a local path on your server (e.g. d:\troubleshootingSharePoint)
Then reproduce the issue, while leaving the powershell window open. If the issue is happening during powershell, go ahead and open a new powershell window. Finally, after you have re-pro’d your issue, come back to the window where you first called Russ’s script and press 1, followed by the “Enter” key.
After you’re done, you can use ULS viewer to look at the logs that are now in your d:\troubleshootingSharePoint directory.
Only one warning: if you’re not sure if you are using the default logging levels, you should run get-sploglevel before using this script. If you are using custom logging levels, they’ll be reset to the OOB levels, which are trace severity of medium for all logging area’s.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
<# ============================================================== // // Microsoft provides programming examples for illustration only, // without warranty either expressed or implied, including, but not // limited to, the implied warranties of merchantability and/or // fitness for a particular purpose. // // This sample assumes that you are familiar with the programming // language being demonstrated and the tools used to create and debug // procedures. Microsoft support professionals can help explain the // functionality of a particular procedure, but they will not modify // these examples to provide added functionality or construct // procedures to meet your specific needs. If you have limited // programming experience, you may want to contact a Microsoft // Certified Partner or the Microsoft fee-based consulting line at // (800) 936-5200 [Call: (800) 936-5200] . // // For more information about Microsoft Certified Partners, please // visit the following Microsoft Web site: // https://partner.microsoft.com/global/30000104 // // Author: Russ Maxwell (russmax@microsoft.com) // // ———————————————————- #> ##Version 1.5 – Date 3-23-17## [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue Start-SPAssignment -Global ############# ##Variables## ############# $global:ulsPath = (get-spdiagnosticconfig).LogLocation $global:servs = (get-spfarm).servers | ?{$_.Role -ne "Invalid"} $global:keepCount = 0 ################################# #Function to set custom log path# ################################# function custLogPath() { $global:ulsPath = $global:ulsPath -replace ":", "$" $localPath = "\\" + $env:ComputerName + "\" + $global:ulsPath $resTemp = get-childitem -path $localPath | ?{$_.Extension -eq ".log"} return $resTemp } ######################################### #InnerFunction to retrieve and copy logs# ######################################### function innerGrabULS($temsrv) { $partPath = "\\" + $srv.name + "\" + $global:ulsPath + "\*.*" $srvInclude = $srv.name + "*.log" $sortedFiles = get-childitem -path $partPath -Include $srvInclude | sort-object LastWriteTime -descending $filePath = $sortedFiles[0].FullName $fileName = $sortedFiles[0].Name try { Copy-Item -Path $filePath -Destination $temdestPath $fulldestPath = $temdestPath + "\" + $fileName if(Test-Path $fulldestPath) {$Global:keepCount++} else { Write-Host "Unable to copy latest ULS file from the following Server: " + $srv.Name -ForegroundColor Yellow Write-Host "You will need to go retrieve the latest ULS log manually" -ForegroundColor Yellow Write-Host } } catch [Exception] { Write-Host “Exception Caught: ” $_.Exception -ForegroundColor Red Write-Host "Error attempting to copy file from Server: " + $srv.Name -ForegroundColor Red Write-Host } } ##################################### #Function to retrieve and copy logs# ##################################### function grabULS($temdestPath) { #$srvCount = $temSrvs.count if($global:servs.gettype().tostring() -eq "System.Object[]") { $srvCount = $global:servs.count foreach($srv in $global:servs) {innerGrabULS $srv} } elseif($global:servs.gettype().tostring() -eq "Microsoft.SharePoint.Administration.SPServer") { #We're dealing with a single server farm# innerGrabULS $srvCount = 1 } if($keepCount -eq $srvCount) {return, 1} else {return, 2} } ######################### ##Function to clear log## ######################### function clearLog($temfinalRes) { if($temfinalRes -eq 1) { Write-Host "Latest ULS Logs have been copied" -ForegroundColor Green Write-Host "Resetting Log Level back to Default" Clear-SPLogLevel } else { Write-Host "Either no files or a partial of ULS log files copied" -ForegroundColor Yellow Write-Host "Please inspect the destination directory for more details" Write-Host "Resetting Log Level back to Default" Clear-SPLogLevel } } ###################### ##Script Starts Here## ###################### ###################### #Get Destination Path# ###################### Write-Host "Enter a folder path where you want the ULS files copied" $outputDir = Read-Host "(For Example: c:\logs\)" if(test-path -Path $outputDir) {Write-Host} else { Write-Host "The path you provided could not be found" -foregroundcolor Yellow Write-Host "Path Specified: " $outputDir -ForegroundColor Yellow Write-Host $outputDir = Read-Host "Enter a folder path where you want the ULS files copied (For Example: c:\logs\)" $checkPath = test-path $outputDir if($checkPath -ne $true) { Write-Host "Path was not found - Exiting Script" -ForegroundColor Yellow Return } else {Write-Host "Path is now valid and will continue"} } ######################################## #Get SharePoint Servers and SP Version## ######################################## $spVersion = (Get-PSSnapin Microsoft.Sharepoint.Powershell).Version.Major if((($spVersion -ne 14) -and ($spVersion -ne 15) -and ($spVersion -ne 16))) { Write-Host "Supported version of SharePoint not Detected" -ForegroundColor Yellow Write-Host "Script is supported for SharePoint 2010, 2013, or 2016" -ForegroundColor Yellow Write-Host "Exiting Script" -ForegroundColor Yellow Return } if($spVersion -eq 14) { $defPathTemp = "%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\14\Logs\" if($global:ulsPath -eq $defPathTemp) { $global:ulsPath = "\c$\" + "program files\common files\microsoft shared\web server extensions\14\logs" $localPath = "\\" + $env:ComputerName + $global:ulsPath $resTemp = get-childitem -path $localPath | ?{$_.Extension -eq ".log"} } else {$resTemp = custLogPath} } elseif($spVersion -eq 15) { $defPathTemp = "%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\15\Logs\" if($global:ulsPath -eq $defPathTemp) { $global:ulsPath = "\c$\" + "program files\common files\microsoft shared\web server extensions\15\logs" $localPath = "\\" + $env:ComputerName + $global:ulsPath $resTemp = get-childitem -path $localPath | ?{$_.Extension -eq ".log"} } else {$resTemp = custLogPath} } elseif($spVersion -eq 16) { $defPathTemp = "%CommonProgramFiles%\Microsoft Shared\Web Server Extensions\16\Logs\" if($global:ulsPath -eq $defPathTemp) { $global:ulsPath = "\c$\" + "program files\common files\microsoft shared\web server extensions\16\logs" $localPath = "\\" + $env:ComputerName + $global:ulsPath $resTemp = get-childitem -path $localPath | ?{$_.Extension -eq ".log"} } else {$resTemp = custLogPath} } if($resTemp -eq $null) { Write-Host "ULS Log directory is invalid or no log files are present. Exiting Script" -foregroundcolor Red Return } ############################ #crank SPLogging to Verbose# ############################ try { Write-Host "Turning up Diagnostic Logging to Verbose" set-sploglevel -TraceSeverity Verbose Write-Host "Verbose Logging now Enabled" } catch [Exception] { Write-Host “Exception Caught: ” $_.Exception -ForegroundColor Red Write-Host Write-Host "Error attempting to set ULS Tracing to Verbose" -ForegroundColor Red #Clearing Just in Case# Clear-SPLogLevel -ErrorAction SilentlyContinue Write-Host "Exiting Script" -ForegroundColor Red Return } ################################### #create new ULS Log on all servers# ################################### if($global:servs.gettype().tostring() -eq "System.Object[]") { foreach($srv in $global:servs) { Write-Host "Creating New ULS Log on Server: " $srv $service = get-service -ComputerName $srv.Name -Name "SPTraceV4" if($service){$service.ExecuteCommand(129)} } } elseif($global:servs.gettype().tostring() -eq "Microsoft.SharePoint.Administration.SPServer") { Write-Host "Creating New ULS Log on Server: " $global:servs $service = get-service -ComputerName $srv.Name -Name "SPTraceV4" if($service){$service.ExecuteCommand(129)} } Write-Host "New ULS Logs Created for every SharePoint Server in the farm" -ForegroundColor Green Write-Host ###################################### #Reproduce the Issue and Collect Logs# ###################################### Write-Host "Keep this window open and reproduce the issue!" Write-Host "After reproducing issue press 1 and enter key!" Write-Host $val = Read-Host if($val -eq 1) { Write-Host "Copying files into Destination Provided" $finalRes = grabULS $outputDir clearLog $finalRes Write-Host "Operation Complete - Exiting Script" -ForegroundColor Green Invoke-Item $outputDir } else { Write-Host "You pressed a different key" -ForegroundColor Yellow Write-Host Write-Host "Try Again: After reproducing issue press 1 and enter key!" Write-Host $val = Read-Host if($val -eq 1) { Write-Host "Copying files into Destination Provided" $finalRes = grabULS $outputDir clearLog $finalRes Write-Host "Operation Complete - Exiting Script" -ForegroundColor Green Invoke-Item $outputDir } else { Write-Host "This is the second attempt and a wrong key was entered" -foregroundcolor Yellow Write-Host "Resetting Logging Level back to default and exiting script"-ForegroundColor Yellow Clear-SPLogLevel Return } } Stop-SPAssignment –Global |