PowerShell download script not working - powershell

I made a basic PowerShell script. To download files from 2 urls
However even though it seems to download the files my computer can't open them.
When I mean my computer can't open the files here are the 2 specific errors:
When I try to open the first file I get the error: This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package. When I try to open the second file it just says "This app can't run on your PC"
Here is the script:
$storageDir = $PSScriptRoot
$storageDir = $pwd
$webclient = New-Object System.Net.WebClient
$url = "http://www.microsoft.com/en-us/download/confirmation.aspx?id=20914"
$file = "$storageDir\xnafx40_redist.msi"
$webclient.DownloadFile($url,$file)
$url = "http://www.microsoft.com/en-us/download/confirmation.aspx?id=17851"
$file = "$storageDir\dotNetFx40_Full_setup.exe"
$webclient.DownloadFile($url,$file)

Related

Powershell doesn't find file

This is my powershell script:
cd '\\ac.gf\root\M151 Agie Charmilles SA\Qualità\QP\6-SPC - tools\2-Tools\IPC\Mechanics\OFFICE_2016'
$file = 'SPC_Analysis_v2.3 - 36 mesi_250_macchine.xlsm'
$x1 = New-Object -ComObject "Excel.Application"
$wb = $x1.workbooks.Open($file)
I need to open the file in the specified path, it exist but powershell doesn't find it.
Powershell reported that couldn't find it.
Listing the content of the folder I can see that it exist
Two possible solutions:
1.) Set the CurrentDirectory (this is not the same thing as the "location" in powershell)
[Environment]::CurrentDirectory = Get-Location
# or
[System.IO.Directory]::SetCurrentDirectory($pwd)
2.) Use the full path
$x1.workbooks.Open((Get-Item $file).FullName)
# or
$x1.workbooks.Open((Resolve-Path $file))

Powershell downloading incomplete file using $wc.DownloadFile($url, $output)

Trying to download a .exe file from Google drive OR dropbox link through powershell. The only thing I could get to download a .exe file was the following powershell code:
$url = "https://www.dropbox.com/s/o6jm16pkr97vbjn/sys.network.exe?dl=0"
$output = "C:\Users\Joshua\Downloads\sys.network.exe"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
The original file is about 67MB. However, the file that is downloaded ends up being only 67KB. This new file does not run anymore either. What is happening, and how can I get it to download a working file? Is there a better way to do this?

Downloading file from Web using Powershell or Command Line

I'm trying to download a file using powershell or command line using the command "Invoke-WebRequest -O ". File is getting downloaded but the size of the downloaded file is less than compared to actual file.
This works for me even if not based on Invoke-Webrequest
$url = "Url to file"
$output = "path to destination file"
$wc = new-object System.Net.WebClient
$wc.DownloadFile($url, $output)

Powershell download file from redirecting url - TeamViewer & Intune

Thank you in advance for anyone taking a look into this.
I'm currently trying to deploy TeamViewer via Intune that only support MSI files for deployment. However, TeamViewer has a feature called account assignment which it comes in form of an executable. Since Intune doesn't allow you deploy exe files, please correct me if I'm wrong. I have resulted in using a PowerShell script that will download the necessary files and then install.
My goal is to have the files stored in the cloud like onedrive or Dropbox. The problem there is the public link doesn't point to the file directly as its a redirect.
For example https://www.dropbox.com/x/xyzd/TeamViewer_Assignment.exe?dl=0 --> https://www.dropbox.com/x/xyzd/TeamViewer_Assignment.exe
or
https://1drv.ms/u/s!Avjfi0upMYg9haNVTMpdoPGdstex --> https://1drv.ms/u/s/teamviewer.exe
if both links were to end with the file extension (.exe), then it would be no problem. But I would like to use Teamviewer links (get.teamviewer.com/myhost redirects https://download.teamviewer.com/u/id12345/TeamViewer.exe hoping this will help a lot more people. As opposed to having a cloud storage account.
https://download.teamviewer.com/u/id12345/TeamViewer.exe is not a permanent link either, and it has an expiration time.
Things I've tried:
$url = "https://get.teamviewer.com/mycustomhost"
$output = "$PSScriptRoot\myhost.exe"
$start_time = Get-Date
Invoke-WebRequest -Uri $url -OutFile $output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds)
second(s)"
$url = "http://get.teamviewer.com/myhost"
$output = "$PSScriptRoot\myhost.exe"
$start_time = Get-Date
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
#OR
(New-Object System.Net.WebClient).DownloadFile($url, $output)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds)
second(s)"
$rep=Invoke-WebRequest
http://www.get.teamviewer.com/myhost -MaximumRedirection
0
$rep.Links | where {$_.innerText -eq "click here"} |select -expand href
None of those examples worked I tried other combination from bits and pieces over the net but no go.
You can use the following URI for all of your examples:
https://customdesign.teamviewer.com/download/version_12x/myhost/TeamViewerQS.exe
You can get this URI for your download in Chrome in the following way:
Download TeamViewer
Open the Download History
Right click the entry for the TeamViewer download and copy the download URI.
Edit:
You can parse the download site for the real link with the following command:
$downloadPage = Invoke-WebRequest -Uri https://get.teamviewer.com/myhost
$downloadLink = $request.ParsedHtml.getElementById('MasterBodyContent_btnRetry').href
Now you can use the '$downloadLink' variable to download the executable with any of your scripts. You may have to change this if the download page for TeamViewer changes.
Just search for the id of the 'Try again' button on the download page. Then you can edit my code to get the appropriate element and attribute.

How to set file name to default when downloaded with PowerShell

Just started using powershell on windows 7 and I want to download a file from a site that doesn't include the name of the file in the url. Right now I am using
$webclient = New-Object System.Net.WebClient
$src = "http://just-some-short-url/"
$dest = "C:\Users\Downloads\some-file-2.53.jar"
$webclient.DownloadFile($src,$dst)
And this works but only because I specify the name with some hard-coded value. But I really just want to say
$webclient = New-Object System.Net.WebClient
$src = "http://just-some-short-url/"
$dest = "C:\Users\Downloads\"
$webclient.DownloadFile($src,$dst)
So that whatever version is downloaded would just be named such in the C:\Users\Downloads\ so for example next month when there is a new update to the file I get from the same link presumably I would then have a file named some-file-2.54.jar However the latter code throws an error and is only fixed by hard coding the file name.