Forum Discussion

tcboeira's avatar
tcboeira
Brass Contributor
May 23, 2024

PS script for uploading files to Sharepoint Online

Hello friends...

 

This process of uploading files from some local storage to Sharepoint Online seems to be the simplest to be carried out via Powershell Script... However, a command/routine in which the password is posted clearly, in plain text, is not " should be" permanent.

 

Do you have any alternative to this?

 

#Copia Arquivos para o Sharepoint Site
    $FILESP = (Get-ChildItem .\$EXECFOLDER\ -File | Sort-Object LastWriteTime -Descending| Select-Object -First 3).Name
    foreach ($FILESP in $FILESP) {
        Write-Host "Arquivo .......... " -NoNewline 
        Write-Host "" $FILESP  -foregroundcolor Yellow -NoNewline 
        Write-Host " foi carregado/enviado com êxito!!!"
    Write-Host ""

    $WebUrl = "https://organizacao.sharepoint.com/sites/name-site"
        $LibraryName ="library"
        $SourceFile=".\$EXECFOLDER\$FILESP"

        $AdminName ="my-name@domain"
        $AdminPassword = "my password"

        #$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminName,(ConvertTo-SecureString $AdminPassword -AsPlainText -Force))
        $Credential = new-object System.Management.Automation.PSCredential $AdminName, $AdminPassword



        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($WebUrl)
        $Context.Credentials = $Credentials

        $Library = $Context.Web.Lists.GetByTitle($LibraryName)
        $FileStream = ([System.IO.FileInfo] (Get-Item $SourceFile)).OpenRead()
        $SourceFileName = Split-path $SourceFile -leaf

        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.ContentStream = $FileStream
        $FileCreationInfo.URL = $SourceFileName
        $FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)

        $Context.Load($FileUploaded)
        $Context.ExecuteQuery()

        $FileStream.Close()

    #Write-Host ""
    }

 

Contextualizing: A routine/script executes locally, generates some files that must be placed on a website, in a library, on Sharepoint Online

 

Well, I believe this can shed some light

No RepliesBe the first to reply

Resources