Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Importing PowerShell Module
- Import-Module Microsoft.Online.SharePoint.PowerShell
- #Authenticating User to SharePoint Online
- $username = "username"
- $Adminurl = "TenantAdminSite"
- $siteColUrl = "0365 site"
- #Logging Method for errors
- $DATE = get-date
- $LogPath = "LogPath"
- $LogFileName = "LogFileName.log"
- $FilePath = $LogPath +"" + $LogFileName
- $logFileCreated = $False
- function write-log([string]$label, [string]$logMsg)
- {
- if($logFileCreated -eq $False)
- {
- write-host "Creating log file..."
- if((Test-Path -path $LogPath) -ne $True)
- {
- write-host"Provide proper values to LogPath folder" -ForegroundColor Red
- }
- else
- {
- Add-Content -Path $FilePath -Value $logHeader
- $script:logFileCreated = $True
- write-host "Log file created..."
- [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
- Add-Content -Path $FilePath -Value $info
- }
- }
- else
- {
- [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
- Add-Content -Path $FilePath -Value $info
- }
- }
- try
- {
- Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
- Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.UserProfiles.dll"
- Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
- #Bind to site collection
- $password = Read-Host -Prompt "Enter password" -AsSecureString
- $Context = New-Object Microsoft.SharePoint.Client.ClientContext($siteColUrl)
- $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$Password)
- $Context.Credentials = $Creds
- Write-Host "Successfully connected.." -ForegroundColor Green
- #Retrieve lists
- $web = $Context.Web
- $List = $Context.Web.Lists.GetByTitle("ListName")
- $Users = $Context.Web.SiteUsers
- $Context.Load($Users)
- $Context.Load($web)
- $Context.Load($List)
- $Context.ExecuteQuery()
- $Directory = (Split-Path $MyInvocation.mycommand.path)
- Write-Host $Directory
- $csvFile = Import-Csv $Directorytest.csv
- ____________________________________________________________________________
- foreach($line in $csvFile)
- {
- $Url = "";
- $Title = "";
- $userString = "";
- $userEmail = $line.EmailAddress
- Foreach ($User in $Users)
- {
- if($User.Email -ne "")
- {
- if($User.Email -eq $userEmail)
- {
- $userString = "{0};#{1}" -f $User.ID, $User.LoginName
- $Url = $line.URL
- $Title = $line.Title
- break;
- }
- }
- }
- if($userString -ne "")
- {
- #Adds an item to the list
- $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
- $Item = $List.AddItem($ListItemInfo)
- $Item["Author"] = $userString
- $Item["Editor"] = $userString
- $Item["Urlvalue"] = $Url
- $Item["Title"] = $Title
- $Item.Update()
- $Context.ExecuteQuery()
- }
- else
- { $userEmail = $userEmail + " Not found in the Site"
- write-log "Error: " $userEmail
- }
- }
- }
- catch
- {
- write-log "Error: " $_.Exception.Message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement