Advertisement
private775

[PS] SharePoint - check server connection using client model

Oct 9th, 2018
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $currentDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition);
  2.  
  3.  
  4. [System.Reflection.Assembly]::LoadFrom("$($currentDir)\bin\Microsoft.SharePoint.Client.dll")|Out-Null 2>&1
  5. [System.Reflection.Assembly]::LoadFrom("$($currentDir)\bin\Microsoft.SharePoint.Client.Runtime.dll")|Out-Null 2>&1
  6.  
  7. function CheckSpConnection {
  8.     param(
  9.     [string]$webUrl,
  10.     [string]$expectedTitle,
  11.     [string]$desc
  12.     )
  13.     try {
  14.         Write-Host -ForegroundColor Yellow "Testing '$($desc)': $($webUrl)"
  15.         $context = new-object Microsoft.SharePoint.Client.ClientContext($webUrl)
  16.         # $context.Credentials = New-Object System.Net.NetworkCredential("madziap","xxxxxx","domgen")
  17.         $w = $context.Web;
  18.         $context.Load($w)
  19.         $context.ExecuteQuery();
  20.         if($expectedTitle -eq $w.Title){
  21.             Write-Host -NoNewline -ForegroundColor Green "OK: "
  22.             Write-Host -ForegroundColor White "The connection to server '$($webUrl)' was successfull"
  23.         } else {
  24.             Write-Host -NoNewline -ForegroundColor Red "ERR: "
  25.             Write-Host -ForegroundColor White "The connection to server was successfull but the site title is invalid, expected '$($expectedTitle)', got '$($w.Title)'"
  26.         }
  27.     }
  28.     catch {
  29.         $ex = $_.Exception
  30.         Write-Host -NoNewline -ForegroundColor Red "ERR: "
  31.         Write-Host -ForegroundColor White $ex.Message
  32.     }
  33. }
  34.  
  35. CheckSpConnection "http://localhost/xxx" "XXX Title" "XXX SITE TEST DESC"
  36. CheckSpConnection "http://localhost/yyy" "YYY Title" "YYY SITE TEST DESC"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement