Last active
November 1, 2017 21:53
-
-
Save joerodgers/f63c9006fbe692f55cc1d71c4f8ace01 to your computer and use it in GitHub Desktop.
This script can be used a synthetic read-only transaction against an Excel workbook in a document library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-ExcelServices | |
{ | |
[cmdletbinding()] | |
param | |
( | |
[parameter(Mandatory=$true)][System.Uri]$Uri, | |
[parameter(Mandatory=$false)][System.Management.Automation.PSCredential]$Credential, # if you don't want to connect as the current user | |
[parameter(Mandatory=$false)][int]$CacheThreshold = 5 # minutes | |
) | |
begin | |
{ | |
$correlationId = $null | |
} | |
process | |
{ | |
$responseText = $null | |
try | |
{ | |
$reqeust = [System.Net.HttpWebRequest][System.Net.HttpWebRequest]::Create( $url ) | |
if( -not $Credential ) | |
{ | |
$reqeust.Credentials = [System.Net.CredentialCache]::DefaultCredentials | |
} | |
else | |
{ | |
$reqeust.Credentials = $Credential | |
} | |
$response = $reqeust.GetResponse() | |
try | |
{ | |
$correlationId = $response.Headers.GetValues( "SPRequestGuid" ) | |
$stream = $response.GetResponseStream() | |
$reader = New-Object System.IO.StreamReader( $stream ) | |
$responseText = $reader.ReadToEnd() | |
Write-Verbose -Message "CorrelationId: $correlationId" | |
} | |
finally | |
{ | |
if( $stream ) { $stream.Dispose() } | |
if( $reader ) { $reader.Dispose() } | |
if( $response ) { $response.Dispose() } | |
} | |
} | |
catch | |
{ | |
Write-Error "Error making request to $Uri. Exception: $($_.Exception)" | |
return $false | |
} | |
if( $responseText -match "Correlation ID" ) | |
{ | |
Write-Error "SharePoint returned an exception. Correlation ID: $correlationId" | |
return $false | |
} | |
if( $responseText ) | |
{ | |
$parsedResponseDate = Get-Date | |
try | |
{ | |
$xml = [xml]$responseText | |
$namespaceManager = New-Object System.Xml.XmlNamespaceManager( $xml.NameTable ) | |
$namespaceManager.AddNamespace("x", "http://schemas.microsoft.com/office/2008/07/excelservices/rest" ) | |
$node = $xml.SelectSingleNode( "//x:range/x:row[1]/x:c/x:fv", $namespaceManager ) | |
$workbookResponseDate = $node.'#text' | |
Write-Verbose -Message "Service Response Date Value: $workbookResponseDate" | |
if( $workbookResponseDate -and [DateTime]::TryParse( $workbookResponseDate, [ref] $parsedResponseDate ) ) | |
{ | |
$dateDifference = (Get-Date) - $parsedResponseDate | |
Write-Verbose -Message "Parsed Date Value: $($parsedResponseDate.ToString('MM/dd/yyyy hh:mm'))" | |
Write-Verbose -Message "Current Date Time: $(Get-Date -Format 'MM/dd/yyyy hh:mm')" | |
if( $dateDifference.TotalMinutes -lt $CacheThreshold ) | |
{ | |
Write-Verbose -Message "Difference: $([Math]::Round( $dateDifference.TotalMinutes, 2 )) minutes" | |
return $true | |
} | |
} | |
} | |
catch | |
{ | |
Write-Error "Error parsing Excel Services Response. Exception: $($_.Exception)" | |
} | |
} | |
else | |
{ | |
Write-Error "No response text was returned from Excel Services." | |
} | |
return $false | |
} | |
end | |
{ | |
} | |
} | |
# sample format to let SharePoint decide if it should use WOPI or Excel Services to fetch the data from the workbook | |
$url = "https://sharepoint.contoso.com/sites/teamsite/_vti_bin/ExcelRest.aspx/Shared Documents/HealthCheck.xlsx/Model/Ranges('HealthCheck')?`$format=atom" | |
# sample format to use WOPI to fetch the data from the workbook | |
$url = "https://sharepoint.contoso.com/sites/teamsite/_layouts/15/WopiFrame.aspx?sourcedoc=https://sharepoint.contoso.com/sites/teamsite/Shared Documents/HealthCheck.xlsx&action=rest&framemode=proxy&wdRestPart=Model/Ranges('HealthCheck')&wdRestQuery=`$format=atom" | |
# sample format to always use Excel Services to fetch the data from the workbook | |
$url = "https://sharepoint.contoso.com/sites/teamsite/_layouts/15/XlRestInternal.aspx?id=https://sharepoint.contoso.com/sites/teamsite/Shared%20Documents/HealthCheck.xlsx&action=rest&framemode=proxy&wdRestPart=Model/Ranges('HealthCheck')&wdRestQuery=`$format=atom" | |
Test-ExcelServices -Uri $url -Verbose | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment