Skip to content

Instantly share code, notes, and snippets.

@nddipiazza
Created October 23, 2017 14:04
Show Gist options
  • Select an option

  • Save nddipiazza/7b66f22b599969c2f73ee7580f1ff3f9 to your computer and use it in GitHub Desktop.

Select an option

Save nddipiazza/7b66f22b599969c2f73ee7580f1ff3f9 to your computer and use it in GitHub Desktop.
This is a powershell script that can query sharepoint to get the Account and Name values for a user given the domain\username format of the user.
$sharepointSite = "http://102.15.12.153"
$loginUserName = "some-username"
$domain = "THEDOMAIN" # must be upper case
$userNameToQueryFor = "USERNAME-TO-QUERY-FOR" # must be upper case
$passwordSecure = Read-Host "What is $loginUserName's password?" -AsSecureString
$credential = New-Object –TypeName "System.Management.Automation.PSCredential" –ArgumentList $loginUserName, $passwordSecure
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $loginUserName,$passwordSecure)))
$response = Invoke-RestMethod -Method Get -Uri "$sharepointSite/_vti_bin/listdata.svc/UserInformationList?`$filter=substringof(%27$domain%5C$userNameToQueryFor%27%2Ctoupper(Account))%20eq%20true%20and%20ContentType%20eq%20%27Person%27" -Headers @{Authorization = "Basic $base64AuthInfo" } -Credential $credential -ContentType "text/xml"
$ns = @{d="http://schemas.microsoft.com/ado/2007/08/dataservices"}
$Account = Select-Xml -Xml $response -XPath '//d:Account' -Namespace $ns
$Account | Foreach {$_.Node}
$Name = Select-Xml -Xml $response -XPath '//d:Name' -Namespace $ns
$Name | Foreach {$_.Node}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment