Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically save in Powershell

What I am doing is running this and it brings up the website and it is a donwload. Once It shows up, I get prompted to save it. I was wondering how I can write code to automatically save this file? enter image description here

 $URL = "http://ps2pw027012/Reports/Reserved.ReportViewerWebControl.axd?ReportSession=bc5nij45tlov50byl2dsxgqk&Culture=1033&CultureOverrides=False&UICulture=9&UICultureOverrides=Fal
se&ReportStack=1&ControlID=31a059a77a994ddc9f56f61d81f64615&OpType=Export&FileName=Local+Group+Members&ContentDisposition=OnlyHtmlInline&Format=CSV"
 $ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.Navigate($URL);
 while ($ie.Busy -eq $true) { Start-Sleep -Seconds 100; }    #wait for browser idle
like image 359
MountaindewKing Avatar asked Aug 03 '17 15:08

MountaindewKing


People also ask

What does @() mean in PowerShell?

Array subexpression operator @( )Returns the result of one or more statements as an array. The result is always an array of 0 or more objects. PowerShell Copy.


1 Answers

You have a few options for doing this. You don't really need to use a browser object to download a file. Here is a great site on how to download files using Powershell, Use PowerShell to download a file with HTTP, HTTPS, and FTP

PowerShell 3 and later:

Invoke-WebRequest -Uri "https://www.contoso.com/file" -OutFile "C:\path\file"

Powershell 2 has this way of downloading files:

$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile("https://www.contoso.com/file","C:\path\file")

The link above goes into more detail on various ways of downloading and saving files through different methods.

like image 142
Jason Snell Avatar answered Sep 28 '22 06:09

Jason Snell