0

Issue Summary

  • Working at a client site
  • They want weekly status reports uploaded to SharePoint.
  • I have Win10, PowerShell and URL of the SP directory

Question

Do you know a way to automate the uploading process?

What I want to do is fill in my status report and save it as I accomplish tasks.

Every Monday I would start a new document in a specific directory, like this: mystatus_010118.doc, etc.

I want Windows scheduler to scan that dir and upload the report to the correct URL every Friday afternoon without my intervention.

I can do simple things in PS.

I assume PS is the best approach.

Can you help me code a solution?

2
  • Do you have access to the SP-Instance or only to a local machine?
    – Oskar
    Commented Apr 6, 2018 at 8:48
  • I do have access to SP. Not as an admin - only as a user.
    – Marinaio
    Commented Apr 9, 2018 at 12:53

2 Answers 2

1

Below script will upload files from local to SharePoint. Get the powershell scheduled in task scheduler.

    if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
    {
         Add-PSSnapin Microsoft.SharePoint.PowerShell
    }

    Function UploadFiles($web,$path,$docLibrary)
    {
        $files = Get-ChildItem $path
        foreach ($file in $files) 
        {  

           if($file.GetType().Name -eq "DirectoryInfo") 
           {
                #Item Is Folder
                UploadFiles $web $file.FullName $docLibrary
           }
           else
           {
                #Item is File
                $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

                #Add file
                $folder =  $web.getfolder($docLibraryUrlName)

                write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
                $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
                write-host "Success"

                #Close file stream
                $fileStream.Close();
           }

        }
    }
    $webUrl = "WEBSITE URL"
    $docLibraryName = "Documents"
    $docLibraryUrlName = "Shared%20Documents"
    $localFolderPath = "D:\FolderWhichContainsAllFiles\"
    $web = Get-SPWeb $webUrl
    $docLibrary = $web.Lists[$docLibraryName]
    UploadFiles $web $localFolderPath $docLibrary
    $web.Dispose()
1
  • Wow! Thank you. More than I bargained for! Question: I filled in the $webUrl value as the complete URL to my folder. $docLibraryName = "mystatus_010118.doc". But what is the value of $docLibraryUrlName = "Shared%20Documents"?
    – Marinaio
    Commented Apr 6, 2018 at 13:52
0

This would be another way to upload the files with PowerShell to SharePoint. It uses the System.Net.WebClient to authenticate and upload the files. It works only on document libraries but you don't need admin access to the SP-Server (it uses your windows account user name).

$uploadUrl = "URLOFTHESPDIRECTORY"

# Stores the password in the environment as secured string
if(!$env:securePassword) {
  $securePassword = Read-Host "password" -AsSecureString
  $env:securePassword = ConvertFrom-SecureString $securePassword
}

$securePassword = ConvertTo-SecureString -String $env:securePassword
$credentials = New-Object System.Management.Automation.PSCredential($env:username, $securePassword)

$webclient = New-Object System.Net.WebClient
$webclient.Credentials = $credentials

# Uploads all the files in this folder
$files = Get-ChildItem "UPLOADPATH"
foreach($file in $files) {
  Write-Host Upload file $file.Name to SP directory -ForegroundColor Green
  $webclient.UploadFile($uploadUrl + "/" + $file.Name, "PUT", $file.FullName)
} 

If you want to automate this script you need to verify that the password is stored in the environmental parameters permamently or you store the password in a file (check: https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-1/ ).

To automate it, you use the task scheduler in windows: https://blogs.technet.microsoft.com/heyscriptingguy/2012/08/11/weekend-scripter-use-the-windows-task-scheduler-to-run-a-windows-powershell-script/

2
  • Thanks. I am testing your solution. BTW That pdq.com link has some great information on it that I am testing, as well.
    – Marinaio
    Commented Apr 10, 2018 at 18:19
  • Yeah the link is great with the complete overview how to work with credentials in PowerShell. However, did you tried my solution?
    – Oskar
    Commented Apr 12, 2018 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.