Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #Importing PowerShell Module
  2. Import-Module Microsoft.Online.SharePoint.PowerShell
  3.  
  4.  
  5. #Authenticating User to SharePoint Online
  6. $username = "username"
  7. $Adminurl = "TenantAdminSite"
  8. $siteColUrl = "0365 site"
  9.  
  10. #Logging Method for errors
  11. $DATE = get-date
  12. $LogPath = "LogPath"
  13. $LogFileName = "LogFileName.log"
  14.  
  15. $FilePath = $LogPath +"" + $LogFileName
  16. $logFileCreated = $False
  17. function write-log([string]$label, [string]$logMsg)
  18. {
  19.  
  20. if($logFileCreated -eq $False)
  21. {
  22. write-host "Creating log file..."
  23. if((Test-Path -path $LogPath) -ne $True)
  24. {
  25. write-host"Provide proper values to LogPath folder" -ForegroundColor Red
  26. }
  27. else
  28. {
  29. Add-Content -Path $FilePath -Value $logHeader
  30. $script:logFileCreated = $True
  31. write-host "Log file created..."
  32. [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
  33. Add-Content -Path $FilePath -Value $info
  34. }
  35. }
  36. else
  37. {
  38. [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
  39. Add-Content -Path $FilePath -Value $info
  40. }
  41. }
  42.  
  43. try
  44. {
  45. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
  46. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.UserProfiles.dll"
  47. Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  48.  
  49.  
  50. #Bind to site collection
  51. $password = Read-Host -Prompt "Enter password" -AsSecureString
  52. $Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteColUrl)
  53. $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$Password)
  54. $Context.Credentials = $Creds
  55. Write-Host "Successfully connected.." -ForegroundColor Green
  56.  
  57.  
  58.  
  59. #Retrieve lists
  60. $web = $Context.Web
  61. $List = $Context.Web.Lists.GetByTitle("ListName")
  62. $Users = $Context.Web.SiteUsers
  63. $Context.Load($Users)
  64. $Context.Load($web)
  65. $Context.Load($List)
  66. $Context.ExecuteQuery()
  67.  
  68. $Directory = (Split-Path $MyInvocation.mycommand.path)
  69. Write-Host $Directory
  70. $csvFile = Import-Csv $Directorytest.csv
  71. ____________________________________________________________________________
  72. foreach($line in $csvFile)
  73. {
  74. $Url = "";
  75. $Title = "";
  76. $userString = "";
  77. $userEmail = $line.EmailAddress
  78. Foreach ($User in $Users)
  79. {
  80. if($User.Email -ne "")
  81. {
  82. if($User.Email -eq $userEmail)
  83. {
  84. $userString = "{0};#{1}" -f $User.ID, $User.LoginName
  85. $Url = $line.URL
  86. $Title = $line.Title
  87. break;
  88. }
  89. }
  90. }
  91. if($userString -ne "")
  92. {
  93. #Adds an item to the list
  94. $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
  95. $Item = $List.AddItem($ListItemInfo)
  96. $Item["Author"] = $userString
  97. $Item["Editor"] = $userString
  98. $Item["Urlvalue"] = $Url
  99. $Item["Title"] = $Title
  100. $Item.Update()
  101. $Context.ExecuteQuery()
  102. }
  103. else
  104.  
  105. { $userEmail = $userEmail + " Not found in the Site"
  106. write-log "Error: " $userEmail
  107. }
  108. }
  109.  
  110. }
  111. catch
  112. {
  113. write-log "Error: " $_.Exception.Message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement