Issues with PnP.Powershell Get-PnPfile - powershell

I have been getting the following error while using the cmdlet Get-PnPFile.
Get-PnPFile : The property or field 'ServerRelativeUrl' has not been
initialized. It has not been requested or the request has not been
executed. It may need to be explicitly requested. At line:1 char:1
Get-PnPFile -Url $fileURL -Path $downloadPath -Filename Leavers.CSV - ...
+ CategoryInfo : WriteError: (:) [Get-PnPFile], PropertyOrFieldNotInitializedException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.GetFile
The code in question is bellow:
$siteURL = "https://contoso.sharepoint.com/sites/ExampleSite"
$fileURL = "/sites/ExampleSite/Path/To/The/File.csv"
$downloadPath = "C:\Temp"
Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
Get-PnPFile -Url $fileURL -Path $downloadPath -Filename File.CSV -AsFile
The odd part is that it has previously worked and even now seems to work intermittently but more often than not it seems to error and I haven't been able to identify a pattern.
The Connect-PnPOnline command runs without error but doesn't seem to work correctly the majority of the time. I have been using Get-PnPSite to check the connection to the SharePoint site but it doesn't seem to be populating after the connection is made as shown bellow.
PS C:\Windows\system32> Get-PNPsite
Url CompatibilityLevel
--- ------------------
On the odd occasions when the Get-PnPFile command works the two fields above are populated correctly.
I have been googling this issue and have found some other people who have had success adding a variable to the start of either the Get-PnPFile cmdlet or the Connect-PnPOnline cmdlet Like so:
$connection = Connect-PnPOnline -Url $siteURL -Credentials (Get-Credential)
$file = Get-PnPFile -Url $fileURL -Path $downloadPath -Filename File.CSV -AsFile
But this hasn't worked for me. I have also tried down and upgrading the version of PnP.Powershell From 1.8.0 to 1.8.11 and back down to 1.7.0 but this also hasn't had any effect.
Hope someone out there has some answers! Thanks in advance

Related

how to delete a folder using invoke command in powershell

First time asking a question here after using it for a long time.
I'm currently making a powershell script to delete userdata when they left the company for a month.
I already tried deleting the folder using the normal remove-item and this works however this is a very slow process when going over the network.
I then found out about the invoke-command function which can run on a remote computer.
Now i can't seem to get this working.
I keep getting the error that the path is not found.
However it seems like powershell is changing my path.
How can i prevent this from happening?
Cannot find path 'C:\Users\admcia\Documents\P$\PERSONAL\JOBA' because it does not exist.
+ CategoryInfo : ObjectNotFound: (C:\Users\admcia...$\PERSONAL\JOBA:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
+ PSComputerName : ODNDATA
my code is the following:
Note that P$ is the local drive letter on the server.
Also note That $item.SamAccountName is used for creating foldername. (we use Samaccountname as the name of the users folder.
$localPath1 = "P$" + "\PERSONAL\" + $item.SamAccountName
$serverName = "Remotecompter"
Invoke-Command -ComputerName $serverName -ScriptBlock { Remove-Item $using:localPath1 -Force -Recurse -Confirm:$false }
If as seen from your local machine, the drive is \\Remotecomputer\P$\, then for the remote computer (where the code is executed) the path is just P:\.
To combine strings into a path, I would suggest you better use the Join-Path cmdlet rather than concatenating the strings with '+'
Try
$localPath1 = Join-Path -Path 'P:\PERSONAL' -ChildPath $item.SamAccountName
$serverName = "Remotecompter"
Invoke-Command -ComputerName $serverName -ScriptBlock { Remove-Item $using:localPath1 -Force -Recurse -Confirm:$false }
You can use -ArgumentList in Invoke command,
Invoke-Command -ComputerName $serverName -ScriptBlock {
param($localPath1)
Remove-Item $localPath1 -Force -Recurse -Confirm:$false
} -ArgumentList($localPath1)
make sure your path is correct, and if it does not work try to hardcode the path in your code.

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.

How to move folder from local drive to Sharepoint folder using PowerShell?

My sample code:
$Creds = Get-Credential
Add-PnPStoredCredential -Name $URL -Username $Creds.UserName -Password $Creds.Password
Import-Module SharePointPnPPowershellOnline
$URL = "https://myweb.sharepoint.com/sites/abc"
connect-PnPOnline $URL
$SourceFolder = "C:\Users\myLocation"
$TargetFolder = "Shared Documents/Sample Images"
#Get-PnPList
Move-PnPFolder -Folder $SourceFolder -TargetFolder $TargetFolder
I got the following error message:
Move-PnPFolder : File Not Found.
At C:\Users\MyLocation\PowerShell\FileUpload.ps1:14 char:1
+ Move-PnPFolder -Folder $SourceFolder -TargetFolder $TargetFolder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Move-PnPFolder], ServerException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.MoveFolder
I used the module SharePointPnPPowershellOnline for connect with Sharepoint.
I think you can only move a folder from one SharePoint location to another using Move-PnPFolder. It is not supported moving the folder from local. As stated here: https://learn.microsoft.com/en-us/powershell/module/sharepoint-pnp/move-pnpfolder?view=sharepoint-ps
Move a folder to another location in the current web.
Please refer to below articles to upload a folder to SharePoint using PowerShell.
SharePoint Online: Upload a Folder using PowerShell
SharePoint Online: Migrate a Folder with Sub-Folders and Files to SharePoint Online using PowerShell

Move file to Teams SharePoint directory using Powershell

I'm trying to move a file to a Teams Channel\SharePoint directory using powershell. It appears to me that I only need three lines of code for proof of concept. I'm using the -UseWebLogin to get my login information
$Output = Get-ChildItem C:\Temp\ToSharepoint\Test.txt
Connect-PnPOnline -Url "https://My365.sharepoint.com/" –UseWebLogin
Add-pnpfile -path $Output -Folder https://My365.sharepoint.com/:f:/s/MyServicesInc/%20ITCommunications/Forms/AllItems.aspx
I've tried numerous strings for the destination but none of them seem to work. I'm currently receiving this error message,
Add-pnpfile : Access denied.
At line:1 char:1
Add-pnpfile -path $Output -Folder https://My365.sharepoint.com/:f:/ ...
+ CategoryInfo : WriteError: (:) [Add-PnPFile], ServerUnauthorizedAccessException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.AddFile
I am able to upload a file manually so I believe my credentials are not the issue but I don't know that for sure.
Any help would be greatly appreciated! - Thanks
Please use the Url of your teams channel site url under following code:
Connect-PnPOnline -Url
"**https://My365.sharepoint.com/sites/Team1-PrivateChannel38**"
–UseWebLogin
In addition, use a relative folder path like this:
Shared Documents/Private Channel 38/Folder1
Here is my test which works well:
$Output = Get-ChildItem C:\Temp\lulu4.jpg
Connect-PnPOnline -Url "https://tenant.sharepoint.com/sites/Team1-PrivateChannel38" –UseWebLogin
Add-pnpfile -path $Output -Folder "Shared Documents/Private Channel 38/Folder1"
Here's the interface I'm working with and what I'm using. The only way I could get the picture in was to submit this as an answer, sorry about that. I'm still not able to get it to work. I am a member of the group rather than the owner and I'm wondering if that is the issue although I can manually upload files and delete file from sharepoint.
$Output = Get-ChildItem C:\Temp\ToSharepoint\Test.txt
Connect-PnPOnline -Url "https://My365.sharepoint.com/sites/UnitedWeStand" –UseWebLogin
Add-pnpfile -path $Output -Folder "Documents/IT Communications"
Thanks again!
My Interface

Copy files with elevated credentials in PowerShell

Searched and searched, but cannot find an answer that works specifically for what I am looking for.
I want to call a PowerShell script from a logon vbscript that runs on logon. The PowerShell script will copy a file from a server network share to C:\Windows, but I need to embed credentials securely in the PowerShell script because the script will be hosted on a common share. I have already generated the AES key file and the password file I am decrypting with the AES key.
This is what I have so far
$User = "itadmin"
$source = "\\server\cip$\misc-programs\new_inb_install\inb.exe"
$destination = "C:\Windows\"
$PasswordFile = "\\server\cip$\misc-programs\new_inb_install\psw.txt"
$KeyFile = "\\server\cip$\misc-programs\new_inb_install\AES.key"
$key = Get-Content $KeyFile
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -key $key)
Copy-Item $source -Destination $destination -Recurse -Credential $MyCredential
The error I get when I run the code is
> Copy-Item : Cannot retrieve the dynamic parameters for the cmdlet. The FileSystem provider supports credentials only on the New-PSDrive
> cmdlet. Perform the operation again without
> specifying credentials.
> At line:12 char:1
> + Copy-Item $source -Destination $destination -Recurse -Credential $MyC ...
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : InvalidArgument: (:) [Copy-Item], ParentContainsErrorRecordException
> + FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.PowerShell.Commands.CopyItemCommand
I should also mention, the credentials I am specifying I don't need for access to the server. I need to use those credentials to deal with UAC and make sure the end user isn't prompted for the service account creds.
Any help would be greatly appreciated!