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

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.

Related

Invoke-WebRequest not working through OneDrive

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.

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)

Remote execution of powershell script with autosys - to interact with IE _ComObject

I have a Powershell script designed to automate the testing of some URLs. The script invokes an Internet Explorer session as such:
$IEProcess = Start-Process -FilePAth 'C:\Program Files (x86)\Internet Explorer\iexplore.exe' -ArgumentList "-private $url"
$Shell = New-Object -ComObject Shell.Application
$IE = $Shell.Windows()
It then proceeds to use the $IE object to navigate to different URLs, perform various checks against the pages HTML document bodies and take screenshots of the resulting web pages. Ultimately, the IE session is ended and the screenshots are distributed via email as attatchments.
The script works perfectly when logged onto the virtual host-machine with a service account, that has the necessary permissions for single-sign-on to the various URLs. However, the requirement is for the script to run remotely as a scheduled task, on a daily basis. There should be no interaction by a user other than opening the resulting email.
I have had a job set up in AutoSys to execute the script remotely, once a day, with highest privileges; but the script fails to complete as expected. Specifically, the .Windows() method fails passing the following error message:
Exception calling "Windows" with "0" argument(s): "The server process could not
be started because the configured identity is incorrect. Check the username an
d password. (Exception from HRESULT: 0x8000401A)"
I have to give the job of scheduling the task in autosys to someone else (for security reasons) but I trust that they have configured the job with the appropriate username/password for the necessary service account.
Some online sources suggest to me that the issue might lie with trying to use _ComObjects remotely, and that instances of Internet Explorer require an interactive user session with a UI - which isn't standard config for an autosys batch job.
I have had my autosys-guy add a flag to the job description which should make the user session "interactive", but the script still doesn't execute correctly.
I'm looking for some general insight on the topic and hopefully a solution to get the job running. If anyone cares to help me out I would greatly appreciate it! :)
I also have a more focussed question:
Even if I managed to get a handle on the IE session, should I expect the screenshots that the script takes to fail since the screen buffer for the VM has no monitor to render to? - The images are bitmap and refer to the IE shell objects "position", "width" and "height" for capture placement/dimensions; which presumably mean nothing without a screen resolution??
Thank you for taking the time to read my issue!

Login-AzureRMAccount returns "Unknown User Type"

As the title implies, I'm trying to automate the login process to AzureRMAccount in Powershell.
I've scoured every forum on every website I can find trying every different combination of solutions (well, except for the solution that works).
Every time I try to automate the login process of my Azure account, I get an error :
Unknown User Type
Yes, I have full access to this account, and Yes it does work fine if I omit all of the parameters and allow for the interactive username/password dialog.
I have even tried to simply call the Get-Credentials and push those into a variable and then call Login-AzureRMAccount using those creds....no luck.
Add-AzureRMAccount fails too.
Any help would be appreciated.
I did find one solution, although it's not the most secure solution as it generates a disk file putting your information at risk if someone were to gain access to the machine on which the Powershell code is executing.
You can login interactively and immediately export the context to a json file, Then at a later time, import that json file as the current context to bypass login
PS C:\> Save-AzureRmContext -Profile (Add-AzureRmAccount) -Path C:\test.json
then in a later session, Import the context
PS C:\> Import-AzureRmContext -Path C:\test.json
Again, this is not the most secure option but it did achieve the results. I would love to have a more secure option.

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.