Skip to content

Instantly share code, notes, and snippets.

@TheBigBear
Last active May 12, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 11, 2021

OK I recorded a session using Selenium IDE.
Here is the output. How can I turn this into a powershell "$ie=New-Object -comobject InternetExplorer.Application" script ?

file-request-upload.side:

{
  "id": "96552676-c750-469c-a91e-0374a3ce3816",
  "version": "2.0",
  "name": "file request upload",
  "url": "https://theraus-my.sharepoint.com",
  "tests": [{
    "id": "c9020202-9bec-4c63-8047-e80b0d58f240",
    "name": "file request upload",
    "commands": [{
      "id": "63afe57b-3993-4190-b17e-90f19f02c9c7",
      "comment": "",
      "command": "open",
      "target": "/personal/eset_reports_teams_theraus_org/_layouts/15/onedrive.aspx?p=26&s=aHR0cHM6Ly90aGVyYXVzLW15LnNoYXJlcG9pbnQuY29tLzpmOi9nL3BlcnNvbmFsL2VzZXRfcmVwb3J0c190ZWFtc190aGVyYXVzX29yZy9FajNuNDJKOC1sZElyWlR1cUN6X0NlNEJtV0tTZE9iMDdndGFYN0lhUEZMdHlR&originalPath=aHR0cHM6Ly90aGVyYXVzLW15LnNoYXJlcG9pbnQuY29tLzpmOi9nL3BlcnNvbmFsL2VzZXRfcmVwb3J0c190ZWFtc190aGVyYXVzX29yZy9FajNuNDJKOC1sZElyWlR1cUN6X0NlNEJtV0tTZE9iMDdndGFYN0lhUEZMdHlRP3J0aW1lPUxOZlAyMmtVMlVn",
      "targets": [],
      "value": ""
    }, {
      "id": "94454a05-a102-44a1-9d49-f63571579c29",
      "comment": "",
      "command": "setWindowSize",
      "target": "1054x814",
      "targets": [],
      "value": ""
    }, {
      "id": "240c173a-b001-4999-9488-6f8241ab63b8",
      "comment": "",
      "command": "click",
      "target": "id=id__0",
      "targets": [
        ["id=id__0", "id"],
        ["css=#id__0", "css:finder"],
        ["xpath=//span[@id='id__0']", "xpath:attributes"],
        ["xpath=//div[@id='appRoot']/div/div[2]/div/div/div/div/div/div/div/div[2]/div[2]/div/button/span/span/span", "xpath:idRelative"],
        ["xpath=//span/span/span", "xpath:position"]
      ],
      "value": ""
    }, {
      "id": "d3ea4eb3-08a6-4cc5-ab63-66131fab20bb",
      "comment": "",
      "command": "type",
      "target": "css=.od-fileRequest-input",
      "targets": [
        ["css=.od-fileRequest-input", "css:finder"],
        ["xpath=//input[@type='file']", "xpath:attributes"],
        ["xpath=//div[@id='appRoot']/div/div[2]/div/div/div/div/div/div/div/div[2]/div[2]/div/button/span/input", "xpath:idRelative"],
        ["xpath=//input", "xpath:position"]
      ],
      "value": "C:\\fakepath\\html-body.html"
    }, {
      "id": "0ed6bf0c-ade7-4fed-963b-5f554e5654a5",
      "comment": "",
      "command": "click",
      "target": "id=TextField11",
      "targets": [
        ["id=TextField11", "id"],
        ["css=#TextField11", "css:finder"],
        ["xpath=//input[@id='TextField11']", "xpath:attributes"],
        ["xpath=//div[@id='appRoot']/div/div[2]/div/div/div/div/div/div/div/div[2]/div/div[4]/div/div/div/div/input", "xpath:idRelative"],
        ["xpath=//div/input", "xpath:position"]
      ],
      "value": ""
    }, {
      "id": "57fa3a8a-3f97-4ceb-8d70-ab8edda5b488",
      "comment": "",
      "command": "type",
      "target": "id=TextField11",
      "targets": [
        ["id=TextField11", "id"],
        ["css=#TextField11", "css:finder"],
        ["xpath=//input[@id='TextField11']", "xpath:attributes"],
        ["xpath=//div[@id='appRoot']/div/div[2]/div/div/div/div/div/div/div/div[2]/div/div[4]/div/div/div/div/input", "xpath:idRelative"],
        ["xpath=//div/input", "xpath:position"]
      ],
      "value": "firstname-selenium"
    }, {
      "id": "b8d304d0-22f5-48e2-a440-8febac5c42b5",
      "comment": "",
      "command": "click",
      "target": "id=TextField14",
      "targets": [
        ["id=TextField14", "id"],
        ["css=#TextField14", "css:finder"],
        ["xpath=//input[@id='TextField14']", "xpath:attributes"],
        ["xpath=//div[@id='appRoot']/div/div[2]/div/div/div/div/div/div/div/div[2]/div/div[4]/div/div[2]/div/div/input", "xpath:idRelative"],
        ["xpath=//div[2]/div/div/input", "xpath:position"]
      ],
      "value": ""
    }, {
      "id": "bcd44847-9338-4b34-af8d-e45331666379",
      "comment": "",
      "command": "type",
      "target": "id=TextField14",
      "targets": [
        ["id=TextField14", "id"],
        ["css=#TextField14", "css:finder"],
        ["xpath=//input[@id='TextField14']", "xpath:attributes"],
        ["xpath=//div[@id='appRoot']/div/div[2]/div/div/div/div/div/div/div/div[2]/div/div[4]/div/div[2]/div/div/input", "xpath:idRelative"],
        ["xpath=//div[2]/div/div/input", "xpath:position"]
      ],
      "value": "lastname-selenium"
    }, {
      "id": "e00db223-308f-4631-bf83-632af4adba07",
      "comment": "",
      "command": "click",
      "target": "id=id__17",
      "targets": [
        ["id=id__17", "id"],
        ["css=#id__17", "css:finder"],
        ["xpath=//span[@id='id__17']", "xpath:attributes"],
        ["xpath=//div[@id='appRoot']/div/div[2]/div/div/div/div/div/div/div/div[2]/div[2]/div/button/span/span/span", "xpath:idRelative"],
        ["xpath=//div[2]/div/button/span/span/span", "xpath:position"]
      ],
      "value": ""
    }]
  }],
  "suites": [{
    "id": "35317506-87b0-4a5d-a743-205e93af02c7",
    "name": "Default Suite",
    "persistSession": false,
    "parallel": false,
    "timeout": 300,
    "tests": ["c9020202-9bec-4c63-8047-e80b0d58f240"]
  }],
  "urls": ["https://theraus-my.sharepoint.com/"],
  "plugins": []
}

@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