Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. If (!$cred) {$cred = get-credential -UserName "$ENV:Username@$ENV:UserDomain.com" -Message "Enter your office 365 login"}
  2.  
  3. function Get-AuthenticationCookie($context)
  4. {
  5. $sharePointUri = New-Object System.Uri($context.site.Url)
  6. $authCookie = $context.Credentials.GetAuthenticationCookie($sharePointUri)
  7. if ($? -eq $false) #https://ss64.com/ps/syntax-automatic-variables.html
  8. {
  9. return $null
  10. }
  11. $fedAuthString = $authCookie.TrimStart("SPOIDCRL=".ToCharArray())
  12. $cookieContainer = New-Object System.Net.CookieContainer
  13. $cookieContainer.Add($sharePointUri, (New-Object System.Net.Cookie("SPOIDCRL", $fedAuthString)))
  14. return $cookieContainer
  15. }
  16.  
  17. function Get-SharepointContext
  18. {
  19. Param(
  20. [Parameter(Mandatory = $true)]
  21. $siteUrl,
  22. [Parameter(Mandatory = $false)]
  23. $cred)
  24.  
  25. If (!$cred) {$cred = get-credential -UserName "$ENV:Username@$env:USERDNSDOMAIN" -Message "Login"}
  26.  
  27. [string]$username = $cred.UserName
  28. $securePassword = $cred.Password
  29.  
  30. [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
  31. [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.ClientContext")
  32. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
  33. $ctx.RequestTimeOut = 1000 * 60 * 10;
  34. $ctx.AuthenticationMode = [Microsoft.SharePoint.Client.ClientAuthenticationMode]::Default
  35. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
  36. $ctx.Credentials = $credentials
  37. Return $ctx
  38. }
  39.  
  40. function Add-SharepointListEntry
  41. {
  42. #example
  43. #Add-SharepointListEntry -cred $cred -clientName $DestinationPages
  44.  
  45. Param(
  46. [Parameter(Mandatory = $true)]
  47. $cred,
  48. [Parameter(Mandatory = $true)]
  49. $sitename,
  50. $siteUrl = "https://$env:Userdomain.sharepoint.com/$sitename",
  51. [Parameter(Mandatory = $true)]
  52. $ListName,
  53. $SharepointData
  54. )
  55. [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
  56.  
  57. # Bind to site collection
  58. $Context = Get-SharepointContext -siteUrl $siteUrl -cred $cred
  59.  
  60. # Get List
  61. $List = $Context.Web.Lists.GetByTitle($ListName)
  62. $Context.Load($List)
  63. $Context.ExecuteQuery()
  64.  
  65. # Create Single List Item
  66. $ListItemCreationInformation = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
  67. $NewListItem = $List.AddItem($ListItemCreationInformation)
  68.  
  69. #construct the entry to insert
  70. $NewListItem["Title"] = $SharepointData.Title #Client Name
  71. $NewListItem["Description"] = $SharepointData.Title
  72.  
  73. #These objects should pass right through
  74. $NewListItem["Client"] = $SharepointData.Client
  75. $NewListItem["Author"] = $SharepointData.Author
  76. $NewListItem["Initials"] = $SharepointData.Author
  77. $NewListItem["Created"] = $SharepointData.Created
  78.  
  79. $NewListItem.Update()
  80. $Context.ExecuteQuery()
  81. }
  82.  
  83. Function Get-SharepointListData
  84. {
  85. #example
  86. #Get-SharepointListData -cred $cred
  87. Param(
  88. [Parameter(Mandatory = $true)]
  89. $cred,
  90. [Parameter(Mandatory = $true)]
  91. $sitename,
  92. $siteUrl = "https://$env:Userdomain.sharepoint.com/$sitename",
  93. [Parameter(Mandatory = $true)]
  94. $ListName
  95. )
  96.  
  97. [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
  98. # Bind to site collection
  99. $Context = Get-SharepointContext -siteUrl $siteUrl -cred $cred
  100.  
  101. #Retrive the List
  102. $List = $Context.web.Lists.GetByTitle($ListName)
  103.  
  104. #Get All List Items
  105. #reference https://gallery.technet.microsoft.com/office/How-to-do-a-CAML-Query-6f5260cf
  106. $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
  107. $ListItems = $List.GetItems($Query)
  108. $context.Load($ListItems)
  109. $context.ExecuteQuery()
  110.  
  111. # Turn item into a catch array
  112. $ListItemCollection = @()
  113.  
  114. ForEach ($item in $ListItems)
  115. {
  116. $propertiesValues = New-Object PSObject
  117. $currentItem = $item
  118. $item.FieldValues.Keys | Where {$_ -ne "MetaInfo"} | ForEach {Add-Member -InputObject $propertiesValues -MemberType NoteProperty -Name $_ -Value $currentItem[$_]}
  119. $ListItemCollection += $propertiesValues
  120. Remove-Variable propertiesValues
  121. }
  122. Return $ListItemCollection
  123. }
  124.  
  125. Function Set-SharepointListData
  126. {
  127. Param(
  128. [Parameter(Mandatory = $true)]
  129. $cred,
  130. [Parameter(Mandatory = $true)]
  131. $sitename,
  132. $siteUrl = "https://$env:userdomain.sharepoint.com/$sitename",
  133. [Parameter(Mandatory = $true)]
  134. $ListName,
  135. [Parameter(Mandatory = $true)]
  136. [int]$Index,
  137. [Parameter(Mandatory = $true)]
  138. $Time
  139. )
  140. [Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
  141.  
  142. # Bind to site collection
  143. $Context = Get-SharepointContext -siteUrl $siteUrl -cred $cred
  144.  
  145. # Get List
  146. $List = $Context.Web.Lists.GetByTitle($ListName)
  147. $Context.Load($List)
  148. $Context.ExecuteQuery()
  149.  
  150. # Select Single List Item
  151. $ListItem = $List.Items.GetById($index)
  152.  
  153. $ListItem["Created"] = $time
  154. $ListItem.Update();
  155. $Context.ExecuteQuery();
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement