Skip to content

Instantly share code, notes, and snippets.

@TheBigBear
Last active May 12, 2021 14:24
Show Gist options
  • Save TheBigBear/1d2369d3b353a452bb3b76a805b24d3d to your computer and use it in GitHub Desktop.
Save TheBigBear/1d2369d3b353a452bb3b76a805b24d3d to your computer and use it in GitHub Desktop.
scripting ms o365 'file request' using powershell to use a 'any' anonymous link to upload a file to onedrive folder
Can anybody help me write a powershell function that takes three parameters
and uploads a specified file usnig the file request URL.
'firstname' 'lastname' and 'file'to stuff in the web file upload request.
MS 'file request' uploads are explained here:
https://support.microsoft.com/en-us/office/create-a-file-request-f54aa7f8-2589-4421-b351-d415fc3b83af
And one such 'test' file request is here:
https://theraus-my.sharepoint.com/:f:/g/personal/eset_reports_teams_theraus_org/Ej3n42J8-ldIrZTuqCz_Ce4BmWKSdOb07gtaX7IaPFLtyQ
Please upload any ps code you come up with as a file to make this an easily scripted task.
PS: One obvious requirement is that your MS o365 org (or specifically your tenant) do have the anonymous 'any' link sharing enabled.
https://docs.microsoft.com/en-gb/sharepoint/turn-external-sharing-on-or-off
@TheBigBear
Copy link
Author

TheBigBear commented May 12, 2021

OK, now I have a first (almost) working version. It automates most of the steps. But I have not yet figured out how to tell the IE comobject what file to upload .

o365-file-request-upload.ps1:
# o365-file-request-upload.ps1
#
# this script uploads a specified file to a o365 any (=anonymous) sharing link
# there is a Selenium IDE recording of the steps we are trying to automate
# https://floobits.com/PoshCode/PowerShell.Slack.com/file/file%20request%20selenium%20recording
# and there is also a github GIST tracking progress on this 'simple' powershell web interaction task
# https://gist.github.com/TheBigBear/1d2369d3b353a452bb3b76a805b24d3d
#

# create a comobject
$ie = new-object -comobject InternetExplorer.Application

# true makes IE visible
$ie.visible=$true 
$ie.ToolBar = $false
$ie.StatusBar = $false
$ie.MenuBar = $false
$ie.AddressBar = $false
$ie.Resizable = $true

# navigate to file request sharing link
$ie.Navigate("https://theraus-my.sharepoint.com/:f:/g/personal/eset_reports_teams_theraus_org/Ej3n42J8-ldIrZTuqCz_Ce4BmWKSdOb07gtaX7IaPFLtyQ")

# give IE time to start-up
while($ie.busy){Start-Sleep 10}

# click button to send files
$button1=$ie.Document.getElementByID("id__0")
$button1.click()


# choose file to upload
# right now this needs to be done manually 
# as I have NOT yet firgured how to pass this to IE from ps

#
# some working notes for me
#
# css .od-fileRequest-input
#
# https://www.computerbase.de/forum/threads/powershell-ie-com-object-enter-in-input-box-druecken-evtl-js-jquery.1707094/
#
# var field = document.getElementById("textareaid");
# var keyEvent = document.createEvent("KeyboardEvent");
#        keyEvent.initKeyboardEvent('keydown', true, true, window, 'Enter', null, null, false, null);
#        field.dispatchEvent(keyEvent);
#

while ($ie.Busy -eq $true)
{
Start-Sleep -Milliseconds 1000;
}

# min 10 Seconds
Sleep -Seconds 10

$firstname="FN machine1"
$ie.Document.getElementByID("TextField11").value = "$firstname" 

while ($ie.Busy -eq $true)
{
Start-Sleep -Milliseconds 1000;
}

# min 3 Seconds
Sleep -Seconds 3

$lastname="LN machine1"
$lastname=$ie.Document.getElementByID("TextField14").value = "$lastname" 

while ($ie.Busy -eq $true)
{
Start-Sleep -Milliseconds 1000;
}

# min 3 Seconds
Sleep -Seconds 3

$button2=$ie.Document.getElementByID("id__17")
$button2.click()

while ($ie.Busy -eq $true)
{
Start-Sleep -Milliseconds 1000;
}

# min 10 Seconds
Sleep -Seconds 10

while ($ie.Busy -eq $true)
{
Start-Sleep -Milliseconds 1000;
}

$ie.Quit()


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment