Sharepoint online - MoveFile - pnp.powershell Status Updates - powershell

I'm trying to do folder/files moves from one sharepoint site to another, and I can't seem to get the Receive-PnPCopyMoveJobStatus to work in my powershell script
Right now the only way I can monitor a file/folder move is to monitor the source's recycling bin for changes. I'd like to be able to get progress either on demand, or consistently in an open powershell. Ideally I'd like to see a percentage sign, and even a verbose option.
Here's what I have put together:
#Config Variables
$SiteURL = "https://<site>.sharepoint.com/
$SourceFolderURL= "sites/<site name>/<document library name>/<Folder or file location>"
$TargetFolderURL = "sites/<site name>/<document library name>"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -interactive
#Sharepoint Copy/Move operation feedback
$Test = "Move-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Overwrite -noWait"
$jobStatus = "Receive-PnPCopyMoveJobStatus -Job $Test"
if($jobStatus.JobState -eq 0)
{
Write-Host "Job finished"
}
referencing:
https://learn.microsoft.com/en-us/powershell/module/sharepoint-pnp/receive-pnpcopymovejobstatus?view=sharepoint-ps
I haven't been successful in running this. When I fill in the site names, library names, and actual site, I get a result that looks like it completes, but with no feedback, nor do the files move. So essentially nothing happens.
What does work for the file/folder move is the following:
#Config Variables
$SiteURL = "https://<site>.sharepoint.com/"
$SourceFolderURL= "sites/<site name>/<document library>/<folder name>"
$TargetFolderURL = "sites/<site name>/<document library>"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -interactive
#sharepoint online powershell move folder
Move-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -overwrite
#Read more: https://www.sharepointdiary.com/2017/06/how-to-move-folder-in-sharepoint-online.html#ixzz7Bkp822M1
Any help on correcting this script, or other methods on seeing the progress of Copy or Move operations on SharePoint would be appreciated!

The source url should be like "<document library>/<file name>"
Please try following script
$SiteURL = "https://<site>.sharepoint.com/sites/<sitesname>
$SourceFolderURL= "<document library name>/<file name>"
$TargetFolderURL = "<document library name>/<target file name>"
$job = Move-PnPFile -SiteRelativeUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Overwrite
$jobStatus = Receive-PnPCopyMoveJobStatus -Job $result
if($jobStatus.JobState == 0)
{
Write-Host "Job finished"
}

Your non-working script contains:
$Test = "Move-PnPFile -SourceUrl $SourceFolderURL -TargetUrl $TargetFolderURL -Overwrite -noWait"
$jobStatus = "Receive-PnPCopyMoveJobStatus -Job $Test"
Both of these variable values are strings. They don't do anything except exist, and do not cause any commands to run. $jobStatus.JobState will always be null and never zero, because JobState is not a property of a string, so the if condition is never met.
If you want to run the commands, don't write them as strings inside double quotes, but just as they are in the documentation you referred to. You can actually run them, and capture their output, with e.g.
$jobStatus = Receive-PnPCopyMoveJobStatus -Job $Test
You can also just copy and paste the example and update the parameter values (though it seems to have the wrong variable name on the second line, which you've already fixed). You may have further errors once the commands are running, which should provide further diagnostic information. Make sure that you are doing all of connecting, requesting the move, and retrieving the status information in the same script.

Related

Provider cannot be found when try to open/access mdb

I'm writing a powershell 5.1 script to query info from a mdb file based on this article. I get this error message when I try to open/access an mdb file:
MDB Open next
Provider cannot be found. It may not be properly installed.
I'm pretty sure I need to adjust my connection info according to what I have installed. This is what I'm doing:
$pathViewBase = 'C:\Data\EndToEnd_view\' #View dir.
$XML_MDB_Dirs = #('\AppText.mdb') #more files later
foreach($mdbFile in $XML_MDB_Dirs)
{
$pathToMdb = Join-Path -Path $pathViewBase -ChildPath $mdbFile
if(Test-Path $pathToMdb)
{
$cn = new-object -comobject ADODB.Connection
$rs = new-object -comobject ADODB.Recordset
Write-Host "MDB Open next" -ForegroundColor DarkCyan
$cn.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = $pathToMdb") ###error this line
Write-Host "Open done" -ForegroundColor DarkCyan
$rs.Open(“SELECT TOP 1 [TableName].[Message Description],
[TableName].[Column1]
FROM [TableName]
WHERE [TableName].[Message Description] = 'ERROR'”,
$cn, $adOpenStatic, $adLockOptimistic)
$rs.MoveFirst()
Write-Host "Message value obtained for ERROR: " $rs.Fields.Item("Name").Value
Break ##########################
}#test-Path
}#foreach
I found this regarding odbc connections, and it seems to say I need to adjust my connection info. Looking at what's installed on my computer, I see this, but I'm a little unclear what I need to adjust my open code to use. Would I need to replace 'Microsoft.Jet.OLEDB.4.0' with 'SQLNCLI11.dll'?
Update:
I checked,
(Get-WmiObject Win32_OperatingSystem).OSArchitecture
and I am running 64 bit powershell, so since I installed accessdatabaseengine_x64.exe access database engine, per #
Mathias R. Jessen below (and rebooted), that's correct. But I still get the same error. I'm not sure if there's something I could check to see if it's installed correctly, or if I need to use SQLNCLI11.0 as the provider instead of Microsoft.Jet.OLEDB.4.0, or if I need to add "using" at the top? Or do I need to check if a powershell module is installed?

Upload file to Sharepoint Online (Microsoft 365) using Powershell (Option 2 - Using Microsoft.SharePoint.PowerShell)

I'm trying to upload a file into a Sharepoint Online (M365) library subfolder, but it keeps giving me errors. I have tried many scripts. This post is about using Microsoft.SharePoint.PowerShell (I have posted questions about other scripts hoping someone can help me with any of them)
This is the code:´
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Write-Host "Adding PSSnapin"
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$url="https://mydomanin.sharepoint.com/sites/mySite/"
Write-Host "Connecting to URL..."
$web=Get-SPWeb -Identity $url
if($web)
{
try
{
$list = $web.Lists.TryGetList("myLibrary/subfolder")
$files = Get-Item -Path "C:\MyFolder\myFile.csv" -Force
foreach ($file in $files)
{
$stream = $file.OpenRead()
$done= $list.RootFolder.Files.Add($file.Name, $stream, $true)
Write-Host $done.Name "Uploaded into the Site" -BackgroundColor Green
}
}
catch
{
$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage
}
}
else
{
Write-Host "Site Doesn't exist"
}
$list.Update()
It fails from the beginning:
Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5
I Have tried to use Install-Module:
Install-Module "Microsoft.Sharepoint.Powershell"
But it fails fo find it:
PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'Microsoft.Sharepoint.Powershell'
I suppose the script will also require for credentials. If you can help me with this, I would be grateful, too
Thanks
Microsoft.Sharepoint.Powershell is for server side scripting only. It depends on a local farm on the same computer.
You should take a look at PnP PowerShell module which wraps client side APIs (CSOM).
That said, you have to understand the differences beetween SSOM (server side object model) and CSOM (client side object model). Two points in particular:
SSOM can do anything on a farm / CSOM only provide a subset of APIs, especially regarding administrative operations
CSOM is mainly a wrapper of HTTP requests. It works in a fashion where you first declare what you need, and then load it. PnP Powershell wraps a part of this complexity, but you shoud really read some docs about the CSOM behavior.
You should ends with something similar to
Import-Module PnP.Powershell
Connect-PnPOnline https://yourtenant.sharepoint.com/sites/yoursite -Interactive
Add-PnPFile c:\myfolder\myfiles.csv -Folder "YourLibrary"
Ref: Add-PnPFile

Programmatically delete the contents of SharePoint 2013 recycle bin

Scenario:
Client would like a PowerShell script wrote to delete the 2nd stage recycle bin for a SharePoint 2013 Site Collection
Client's SharePoint requires a credential authorization on load.
Issue:
I have researched, however in regards to SharePoint 2013, I have yet to find a script example that involves using my credentials. However in the SharePoint Online examples for programmatically deleting a SP recycle bin they use credential authorization.
My attempts to splice these two scripts together have been met with no success.
Question: Would somone be able to assist me by taking a look at my code and letting me know where I am going wrong?
##Variables for Processing
$SiteUrl = "https://MySite/sites/site"
Try {
#Setup the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Context.Credentials = $Credentials
#Get the recycle bin
$Site = $Context.Site
$RecycleBinItems = $Site.RecycleBin
$Context.Load($Site)
$Context.Load($RecycleBinItems)
$Context.ExecuteQuery()
Write-Host "Total Number of Items found Recycle Bin:" $RecycleBinItems.Count
#sharepoint online powershell empty recycle bin
$RecycleBinItems.DeleteAll()
$Context.ExecuteQuery()
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Is there a way to tell when a site is created and ready to use when creating it using powershell?

I'm creating a number of sites using a powershell script. Now, when each of the sites is finished, I want to activate features on it.
My problem is that when I do this, it takes some time before the site is ready. Especially in SharePoint Online it is hard to predict when the site is ready. I've tried using time-loops, but I was wondering if there is a status setting somewhere that I can query instead.
Any thoughts?
Actually we solved the problem. The siteCreationOperation has a property named isComplete. Iterate over this and pick up the boolean for further processing :)
https://msdn.microsoft.com/en-us/library/microsoft.online.sharepoint.tenantadministration.tenant.createsite(v=office.15).aspx
#Create the site using the properties
$tenant.CreateSite($properties) | Out-Null
...
...
$siteCreationOperation = $tenant.CreateSite($properties)
$ctx.Load($siteCreationOperation)
...
...
#Create the site in the tennancy
...
...
do
{
...
...
$ctx.Load($siteCreationOperation)
$ctx.ExecuteQuery()
Write-Host $siteCreationOperation.IsComplete
...
...
}
...
while (!$siteCreationOperation.IsComplete)
...
Here is something that worked for me:
Connect-SPOService -Url $adminUrl -Credential $credentials
while ((Get-SPOSite -Filter "Url -like '*$($properties.Url)*'").Status -ne "Active")
{
Write-Host "." -NoNewline
Sleep -s 10
}
Where admin URL is https://Contonso-Admin.sharepoint.com and the Properties.Url is the site I am looking for, so something like https://Contonso.sharepoint.com/sites/Test1
This will work but you are better testing for the site and then doing another loop to test for the last artifact that creates like a list or a library. I've only tested this on an on premise and not online instance
$site = Get-SPSite <Site here> -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection
if($err)
{
while($err)
{
Write-Host "Waiting for site to be created"
Start-Sleep -seconds 5
$site = Get-SPSite <site here> -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection
}
#while loop for the last artifact that you are waiting for as the site will create but it may not be fully ready
}
Cheers
Truez

Task Scheduler with Powershell Sharepoint ps1

I am facing an issue using the Task Scheduler to run a Sharepoint Powershell script.
I'm using the following in the Task Scheduler :
-Command "& 'C:\Users\crpmcr\Desktop\Upload\Script.ps1'"
This is the resume of my script :
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
New-Item "[PATH]" -type file
$stream = [System.IO.StreamWriter] "[PATH]"
$stream.WriteLine("Message Example")
Try{
$web = Get-SPWeb "[WebApplicationUrl]"
}
Catch{
$stream.WriteLine("Error")
}
$stream.close()
If i remove the line in the try, i get the Message Example line in my new file. But it seems that the line in the try does make everything break. My file is created but it's empty. Even if some text has been added before. Also the rest of my script using the web is not working obviously.
Any idea about what my problem could be ?
Thanks!
If you are running PowerShell from
C:\Windows\SysWOW64\WindowsPowerShell\v1.0
Try changing it to
C:\Windows\System32\WindowsPowerShell\v1.0
And see if that makes any difference.
Solution found. My script was creating a file for logs and when i clicked in it it was empty. So i thought there was an issue but in fact it's because the line GetSP-web take severals seconds on my server. so it blocks the writing while it's looking for the web. 10 seconds later my file had the lines added. Obviously i was too fast and had to wait longer to see the result.