How to automatically validate a popup that normally asks for a manual action?

Hello,
I had a script that worked fine alone with Task scheduler, basically it ran chrome, made him call an url which downloaded an excel file and saved it automatically in the Downloads directory.

A GPO has just been applied which sets to YES the parameter “Ask where to save each file before downloading” in chrome (and Edge too). This now triggers a popup to show up, which needs manual action.

So I wanted to know how, in powershell, I could simulate the click on the “Save” button of the “Save As” popup that appears now ? The window directly opens in the good directory Downloads but if there is also a way to make sure about this directory by setting it before validating the save, that would be even better.

Thanks for your help.

This is basically the part of the script that calls chrome, that I have now :

$pathToChrome = ‘C:\Program Files\Google\Chrome\Application\chrome.exe’

$tempFolder = ‘–user-data-dir=’ + $env:Temp + ‘\temp_chrome_sc_request’

$startmode = ‘–default’$startPage = ‘https://instancelambda.service-now.com/nav_to.do?uri=%2Fsc_request_list.do%3FXLSX%26sysparm_<sysparmTypeOrField=value>%26sysparm_fields=reassignment_count,location,number,opened_at,short_description,requested_for,priority,request_state,u_business_service,business_service,calendar_duration,sla_due,made_sla,u_other_service,assignment_group,assigned_to,sys_updated_on,sys_updated_by,contact_type,closed_by,sys_created_on,sys_created_by’

$downloadpath = ‘C:\Users' + $env:USERNAME +’\Downloads'

$app=Start-Process -FilePath $pathToChrome -ArgumentList $tempFolder, $startmode, $startPage -PassThru -WindowStyle Minimized

Have you investigated downloading the file using Invoke-WebRequest versus the browser method? Clicking on buttons is more of an RPA capability than something in Powershell as you would need to do a SendKeys or something and typically a script becomes less stable. The setting applied is a browser setting, so investigate methods to download directly.

Downloading Files with PowerShell and Invoke-WebRequest – Arcane Code

3 Likes

Rob!! Welcome back sir!

1 Like

Hello,
When I try to use Start-BitsTransfer, it creates a 0 kb-size file and when I try to use Invoke-WebRequest, I get a 33kb-size file when the file I should get is like 12Mb.
I don’t know if you paid attention to my URL but it is not directly a link to a file, it is a link in servicenow that when called, generates a file and downloads it. Servicenow uses SSO authentication method.
Do you have an idea of which additional paremeter I should use ?
I have tried adding -UseDefaultCredentials to Invoke-WebRequest but I get the same result (33kb-size file).
I have tried adding $Cred = Get-Credential and -Credential $Cred to Start-BitsTransfer but I also get the same result (0kb-size file). Get-Credential also generates a popup to enter my credential, which I don’t want to do, I want to the script to use the actual credential of the user running the script.
Thanks for your help

Just visiting. :wink: Some research led here and thought I would paruse the forums.

ServiceNow (SNOW) is a widely used platform, so try a specific search to see if you get lucky. Is it possible to get what you are downloading from the API, that will be the most direct and stable solution (if you have access). Complex step-by-step like log into website, navigate to page, click on button, get redirection link, download file, etc. can be done with Powershell, but it can be painful and difficult to code for. Additionally, anytime you are using a web interface, DOM, with parsing, a website can change and break your code. The API is the preferred method to get data from a system. Here is some other links:

asp.net - Use Powershell to download attached excel file from Servicenow ticket - Stack Overflow

As far as credentials, you will likely need to provide credentials as Powershell won’t log you in via SSO. Concentrate on getting it working and then you can look into storing credentials encrypted that can only be decrypted by the user on the computer where encrypted.

1 Like

Another option is to request for the policy to be removed if you have a valid business case or you could change the setting in Chrome (assuming you have permissions) as part of the script, perform the download to subvert security. When policy applies, it would turn the setting back on or you could put it back.

Indeed, I know about the SNOW APIs but I unfortunaltely don’t have access to them, thus this workaround…
I will try the script you provided in your link (thank you) but I saw that it asks for a password. And I don’t have a service account password, so when I leave the company, I don’t want the password stored to be mine (even if encrypted, because it will be disabled). So do you know a way to retrieve the credentials of the user running the script ?

The user running the script would put in their snow password.

No, SNOW uses SSO, so when it is using the browser, it is using the windows credentials of the user running the browser (SSO then)

Whatever account logs in whether sso or not, they enter that password.

Yes, we enter a passsword when opening the windows session, so it is this credential that is needed to then access SNOW. So when this windows session of a server is done once, it then stays on for days. My script, which will be launch once per day from this server, needs to download the new excel export without the need of a human entering again a credential.