Invoke-WebRequest not working through OneDrive - powershell

I have a document residing on OneDrive. The permissions are that anyone with the link can view/edit.
I have written a very short script to download the file and place it onto my desktop. However, once the file is downloaded, I open it to be greeted with a message saying that it is unreadable/corrupt. This happens with every file type I test including .ZIP folders. Here is my code:
Invoke-WebRequest -Uri "https://longobscureonedrivelink/personal/sharefile_removedforsecurity/Documents/folder/folder2/testy.pdfdownload=1" -OutFile "C:\Users\Billy\Desktop\testy.pdf"
My thinking is that it's an authentication issue but seeing as anyone with the link can view/edit I'm not sure.
I have tried adding -Credential Get-Credential and imputing the actual Microsoft account who owns the OneDrive's username and password but still no luck.
Any help is greatly appreciated.

Related

Backup a user's OneDrive for Business account using PowerShell

Our company is implementing a process where we automatically backup specific users' Exchange mailbox and OneDrive contents during the offboarding process.
My idea was pretty simple: using the cmdlets for the Compliance center to retrieve both and schedule deletion after an x period of time.
The problem is the e-mail backup is working perfectly, but for some reason OneDrive is not.
I created a test account with some e-mails in its mailbox and a added some files to its OneDrive, but for some reason it's only saving the mailbox contents.
Here's my code:
# For running on Azure runbooks
$Creds = Get-AutomationPSCredential -Name "testcred"
Connect-IPPSSession -Credential $Creds
New-ComplianceSearch -Name "test-name" -ExchangeLocation "test.user#company.com" -AllowNotFoundExchangeLocationsEnabled $true -SharePointLocation "https://company-my.sharepoint.com/personal/test_user_company_com/" | Start-ComplianceSearch
The search then pops up on https://compliance.microsoft.com/contentsearch and can be exported if necessary to access.
Problem is when I export it the contents of the OneDrive just simply don't show up even though in the compliance center it does seem to register the request for the site:
That's great, but then after extracting the contents to my PC no OneDrive contents come with:
The Exchange folder contains the .pst file with mailbox contents, but no OneDrive files to be found.
Am I missing something here?
Edit: I should mention we use a 100% cloud Office 365 environment.
Edit2: Just as an additional proof, here's the content inside the OneDrive in the preview section of the Compliance's Content Search:
I landed here when searching about another glitch on exporting Content Search OneDrive files.
Using Compliance Admin Center/Content Search I am able to export OneDrive files, but I have to explicitly set the user's URL (xxxx-my.sharepoint.com/personal/yyyy in my case).
(BTW, my problem is that the export has files with extensions such pdf_1E98101, msg_61c3b09 and so on... I am creating a question for this)

Using PowerShell to Download Google Sheet Data to Local CSV - Credential Issues

For a few months now, I've been utilizing a PowerShell script that automatically downloads my Google Sheet as a CSV. Something changed about a week ago - the file returned by the script is now html, which directs me to my Gmail login. I'm not sure if my credentials were previously cached or something, but I haven't been able to resolve this. If I click the URL manually, it successfully downloads as a CSV (as expected). But I have been unsuccessful in updating my PowerShell script (or anything else) to automate the process once again.
My PowerShell script:
$client = new-object System.Net.WebClient
$enddate = (Get-Date).tostring("yyyyMMdd")
$client.DownloadFile("https://docs.google.com/spreadsheets/d/XXXXXXXXXXXXX/export?format=csv"
,"C:\Users\<local_location>\" +$enddate+ "_File.csv")
If you click on the link manually, likely you browser will have some cookies from Google with a refresh token. Try opening the export link in an incognito browser window and you will likely see the same HTML as received by Powershell. If the Spreadsheet is shared with anyone with the link, then the Powershell script should work. If it isn't then the PowerShell script will need to be authorized before.

Sharepoint Check-In Document using Powershell

Does anyone know why the Set-PnPFileCheckedIn cmdlet below does nothing when executed? The file doesn't get checked in. I'm logging in with the same user that has the file checked out.
I'm not even seeing an error message. The script just runs through.
$SiteUrl = "https://orgsite.com/sites/TestSite"
$fileRelativePath = "/sites/TestSite/Documents/File.pdf"
Connect-PnPOnline -Url $SiteUrl -UseWebLogin
Set-PnPFileCheckedIn -Url $fileRelativePath
Per my test, this works for me.
Please make sure the relative url of the file is right, I assume the fileRelativePath shuold be "/sites/TestSite/Shared Documents/File.pdf".

Powershell Handling cookie popup without using invoke-webrequest

I am attempting to get a script working that does not use invoke-webrequest. The problem I am having is that when I run the script a popup prompt occurs, the popup consists of the following message;
"Windows Security Warning
To allow this website to provide information personalized for you, will you allow it to put a small file (called a cookie) on your computer?"
with yes no response from user
The code I am executing is the following:
$ParsedHTML = New-Object -com "HTMLFILE"
$webresponse = New-Object System.Net.WebClient
$webresponse.Headers.Add("Cookie", $CookieContainer.GetCookieHeader($url))
$result = $webresponse.DownloadString($url)
$ParsedHTML.IHTMLDocument2_write($webresponse)
$ParsedHTML.body.innerText
The main problem with this code is that the $url I am using part of the weblink checks to see if cookies are enabled and this code causes a returned value of disabled.
My question, is there a way to handle the cookie request without changing the output response from the test url site.
Note: This script will be automating a process over hundreds of remote computers and thus having an unhandled popup will just prevent the script from running.
I found the answer in another SO question Using Invoke-Webrequest in PowerShell 3.0 spawns a Windows Security Warning
add the parameter -UseBasicParsing
Technet https://technet.microsoft.com/en-us/library/hh849901.aspx notes that the parameter stops the DOM processing. and has the caveat "This parameter is required when Internet Explorer is not installed on the computers, such as on a Server Core installation of a Windows Server operating system."
So, you mileage may vary.

Powershell invoke-webrequest downloading HTML not actual file

I am trying to download a google spreadsheet via an invoke-webrequestin powershell.
My link looks something like this and I can confirm that if I go to the link it prompts me to download the file I want...
$url = https://docs.google.com/spreadsheets/d/XXXXXXXXXXXXXXXX/export?format=csv
I have tried downloading the file via 2 ways:
Invoke-WebRequest $url -OutFile $saveLocation
(New-Object System.Net.WebClient).DownloadFile($url, $saveLocation)
Both of these just download the HTML for the page, and not the actual file.
The spreadsheet I am downloading is 100% public so it is not like I need to be logged into a google account to view it.
I made the test and I can quite safely say that your file is not public.
I have reproduced your issue before making my test sheet public, and I could not afterwards.
Tested with this :
$url = "https://docs.google.com/spreadsheets/d/1OOc5aiG3hh8mrGF4p7NJXKI8KqBSkS2ZjsBXtb4qpz8/export?format=csv"
Invoke-WebRequest $url -OutFile "C:\test.csv"