How to transfer files from remote server to SharePoint Shared Documents folder - powershell

I am by no means a programmer, but I was approached with the task of moving files from a specific date range from a remote server to a SharePoint site. Currently the script that I've been working on just displays the files within the specific date range, but doesn't copy those files from the remote server to the SharePoint site. If anyone can take a look at this and let me know what I'm missing I would greatly appreciate it!
$URL = "designated url"
#First time tasks
#Install-Module SharePointPnPPowerShellOnline
$Creds = get-credential
Add-PnPStoredCredential -Name $URL -Username $Creds.Username -Password $Creds.Password
import-module SharePointPnPPowerShellOnline -Verbose
Connect-PnPOnline $URL -UseWebLogin
#Enter the name of the folder at the end of PAMInvoices
$Files = Get-ChildItem "filepath"
if ($Files = {$_.lastwritetime -gt ‘1/1/11’ -AND $_.lastwritetime -lt ‘1/1/15’})
{foreach($File in $Files)
{#$File = $Files[0]
Add-PnPFile -Folder $URL -Path $File.FullName}}

Related

Powershell script searching files on domain

Very new to powershell and AD, so apologies if this post has an obvious answer. I have done some research and I am still not finding the answers I am looking for. My script is below for reference.
I have created a simple powershell script that will run on an admin vm i have setup on my domain. I have a separate SQL vm running a backup script that consume a lot of storage over time. I am trying to run this very simple script. My question is, do I need to modify this script in order to store it on my admin vm but have it run on my sql vm? Or can i leave the path as is and just set up in AD task scheduler. I have tried targeting the FQDN and the IP, but it doesn't seem to be working either way.
$backups_file = 'E:\blahBlahBla\SQL\Backups' or
$backups_file = '<IP_ADDRESS>\E:\blahBlahBla\SQL\Backups' or
$backups_file = '<FQDN>E:\blahBlahBla\SQL\Backups'
$backup_file_exist = (Test-Path -Path $backups_file)
if ($backup_file_exist){
# Verifies the folder exists
Write-Output -InputObject "This folder exists"
# returns all the files in the folder.
Get-ChildItem -Path $backups_file
# Deletes all files in the folder that are older that 7 days.
Get-ChildItem -Path $backups_file -Recurse | Where-Object {($_.LastWriteTime -lt (Get-
Date).AddDays(-7))} | Remove-Item
}
else
{
Write-Output -InputObject "Unable to access this directory."
}
Thanks.
well all your $backups_file solutions seems wrong to me.
If you want excess a directory on a Remote system, it has to be at least a fileshare or a administrative share like \\computer\e$\folder\folder\
But why using file shares or something like that when you just simple can connect to a Powershell Session on the Remote Host? here is a example.:
$mySQLServer = "Server1.domain.name", "server2.domain.name"
$backupFolder = "E:\blahBlahBla\SQL\Backups"
foreach ($server in $mySQLServer)
{
$session = New-PSSession -ComputerName $server #maybe -cred if needed
Invoke-Command -Session $session -ArgumentList $backupFolder -ScriptBlock {
param(
$directoy
)
if ($backup_file_exist)
{
# Verifies the folder exists
Write-Output -InputObject "This folder exists"
# returns all the files in the folder.
Get-ChildItem -Path $directoy
# Deletes all files in the folder that are older that 7 days.
Get-ChildItem -Path $directoy -Recurse | Where-Object { ($_.LastWriteTime -lt (Get-Date).AddDays(-7))
} | Remove-Item
}
}
Remove-PSSession
}
Good Luck!

How to remove OneDrive folder using PowerShell

I'm trying to remove a user OneDrive folder using PowerShell but I'm not seeing any sucess even though I've been searching around internet so I would be really appreciated if I can get any help or suggestion.
so Just for testing purpose, I'm trying to delete my a folder in my own OneDrive called "Testing" and I wanted to delete everything in there including subfolders and files.
Connect-SPOService -Url https://company-admin.sharepoint.com
$OneDriveURLs = Get-SPOSite -IncludePersonalSite $true -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"
foreach($OneDriveURL in $OneDriveURLs)
{
Connect-SPOService -Url https://company-admin.sharepoint.com
Connect-PnPOnline -Url $OneDriveURLs
Remove-PnPFolder -Name "Google Drive" -Folder "Testing"
}
Your cmdlet format is not correct, you should follow the structure, more about it in Microsoft Docs
You would need to change <username> to the name the personal drive.
Change your format to :
$drive= https://company-admin.sharepoint.com/personal/<username>
$folder = 'testing'
Remove-PnPFolder -Name $drive -Folder $folder
If that does not work, as an alternative you can try the following powershell module OneDrive and use:
Remove-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -path "/Upload"
You can read more about the module on their GitHub page.

Download file from SharePoint through Powershell

I am trying to download a file from a SharePoint site through PowerShell. The objective of this script is to take the SharePoint file and apply it to Outlook for employee signatures. We already have created the employee signatures that we want, we just need to deploy those .htm files to the employee devices. I am running this script through Office 365 Endpoint Manager (no longer Intune) to deploy to my end users. The Script will create the .htm files locally on a machine, however when opening the .htm file it takes me to the site url, not providing me with the actual email signature - almost like the command is only copying and not actually downloading. Any thoughts on how to add onto this script to download the respective file from the SharePoint site, where the email signatures are stored? Or is there another, easier option that I am not thinking about that could work better? Still a little new to some SharePoint functions and getting Powershell to communicate with Sharepoint, so please bear with me. Thank you for your help.
Used this code to help build the script: https://github.com/marcusburst/scripts/blob/master/Powershell/Exchange/AutomaticOutlookSignature.ps1
Made adjustments to help fit my needs.
# Checks if outlook profile exists - we only want it to run on people who have a profile so they don't get an annoying profile popup #
$OutlookProfileExists = Test-Path
"C:\Users\$env:Username\AppData\Local\Microsoft\Outlook"
if ($OutlookProfileExists -eq $true) {
Write-Host "User Outlook profile exists.. continuing.." -ForegroundColor Yellow
# Signature Variables #
$ExternalSignatureName = 'External-Signature.htm'
$SigSource = 'https://SharePointSiteURLL'
$RepliesForwardsSignatureName = 'Replies-Forwards-Signature.htm'
$SigSource = 'https://SharePointSiteURL'
# Environment variables #
$AppData = $env:appdata
$SigPath = '\Microsoft\Signatures'
$LocalSignaturePath = $AppData + $SigPath
# Copy file #
If (!(Test-Path -Path $LocalSignaturePath)) {
New-Item -Path $LocalSignaturePath -Type Directory
}
# Check signature path #
if (!(Test-Path -path $LocalSignaturePath)) {
New-Item $LocalSignaturePath -Type Directory
}
# Copy signature templates from domain to local Signature-folder #
Write-Host "Copying Signatures" -ForegroundColor Green
Invoke-WebRequest -URI $Sigsource -Outfile "$LocalSignaturePath\$ExternalSignatureName"
Invoke-WebRequest -URI $SigSource -OutFile "$LocalSignaturePath\$RepliesForwardsSignatureName"
# Set as Default Signature #
If (Test-Path HKCU:'\Software\Microsoft\Office\16.0') {
Write-host "Setting signature for Office 2019"-ForegroundColor Green
Write-host "Setting signature for Office 2019 as available" -ForegroundColor Green
If ((Get-ItemProperty -Name 'First-Run' -Path HKCU:'\Software\Microsoft\Office\16.0\Outlook\Setup' -ErrorAction SilentlyContinue))
{
Remove-ItemProperty -Path HKCU:'\Software\Microsoft\Office\16.0\Outlook\Setup' -Name 'First-Run' -Force
}
If (!(Get-ItemProperty -Name 'External-Signature' -Path HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -ErrorAction SilentlyContinue))
{
New-ItemProperty -Path HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'Ext-Signature' -Value $ExternalSignatureName -PropertyType 'String' -Force
}
If (!(Get-ItemProperty -Name 'Replies-Forwards-Signature' -Path HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -ErrorAction SilentlyContinue))
{
New-ItemProperty -Path HKCU:'\Software\Microsoft\Office\16.0\Common\MailSettings' -Name 'Replies-Forwards-Signature' -Value $RepliesForwardsSignatureName -PropertyType 'String' -Force
}
}
# Removes files from recent items in file explorer #
Write-host "Cleaning up recent files list in File Explorer.." -ForegroundColor Yellow
Get-ChildItem -Path "$env:APPDATA\Microsoft\Windows\Recent" -File | Sort-Object LastWriteTime -Descending | Remove-Item
Get-ChildItem -Path "$env:APPDATA\Microsoft\Windows\Recent\AutomaticDestinations" -File | Sort-Object LastWriteTime -Descending | Remove-Item
}
I think i ran into this issue a while back. I think your script it fine, you just have to make sure there's a valid token in your blob storage.

Is there a way to update the "Created By" and "Created" fields on a SharePoint Folder using PNP

I'm migrating a folder from my local drive to SharePoint Library, and i want to be able to modify the created by fields using powershell. is this possible?
if not is there another alternative in which this can be done?
I am able to do this with add-pnpfile for files but not for folders. something in the nature of
$object = #{}
$object.Add("Modified",'1/1/2019')
$object.Add("Editor" ,"jennie.Doe#email.com")
$object.Add("Author" ,"Mac.walkers#email.com")
Add-PnPFile -Path 'C:/users/Newfolder/test.docx' -Folder 'Test/NewFolder' -Values $object
where $object is the object holding the properties and values i need to update
Sample demo:
Key point is getting the folder item and then updating.
Connect-PnPOnline -Url $siteURL -Credentials $PSCredentials
$user=Get-PnPUser | ? Email -eq "user#tenant.onmicrosoft.com"
$folderName="NewFolder"
$library=Get-PnPList -Identity MyDoc4
Add-PnPFolder -Name $folderName -Folder $library.RootFolder.Name
$folderURL=[string]::Format("{0}/{1}", $library.RootFolder.ServerRelativeUrl,$folderName)
$folder=Get-PnPFolder -Url $folderURL
Set-PnPListItem -List "MyDoc4" -Identity $folder.ListItemAllFields -Values #{Editor=""+$user.Id+"";Modified="7/24/2019"}

DPM - Powershell Script to get list of files available for backup

I am working on a DPM powershell script to get the list of files/folders available for backup in a particular directory. More precisely, i need to get the list of folders under the directory D:\inetpub\vhosts\ (i.e all vhosts). I have been trying to write a script using DPM powershell cmdlets and this is what i have come up with.
$searchpath = 'D:\inetpub\vhosts'
$so=New-SearchOption -FromRecoveryPoint $today -ToRecoveryPoint $tomorrow -SearchDetail filesfolders -SearchType contains -Location $searchpath -SearchString "*" -ErrorAction SilentlyContinue
$ri=Get-RecoverableItem -Datasource $datasource -SearchOption $so -ErrorAction SilentlyContinue
foreach($file in $ri)
{
echo $file.userFriendlyName
}
But i was not able to get all the directories. After some research i found out that New-SearchOption can at maximum return 250 searches. In my use the number of folders is minimum 1500. Is there any way of getting all the files. Any help would really be appreciated.
Have you tried using the pipeline. There might be paging features built in the cmdlet only available using the pipeline. Try the code below:
$searchpath = 'D:\inetpub\vhosts'
New-SearchOption -FromRecoveryPoint $today -ToRecoveryPoint $tomorrow -SearchDetail filesfolders -SearchType contains -Location $searchpath -SearchString "*" -ErrorAction SilentlyContinue |
Get-RecoverableItem -Datasource $datasource -ErrorAction SilentlyContinue | For-EachObject {
$_.userFriendlyName
}