Error in download file using powershell - powershell

Am trying to download sqlserver express 2012 on my EC2 instance.following sample given here.
this is my script:
$storageDir = "C:\download"
$webclient = New-Object System.Net.WebClient
$url = "https://www.microsoft.com/en-us/download/confirmation.aspx?id=29062"
$file = "$storageDir"
$webclient.DownloadFile($url,$file)
C:\download is where i want the downloaded file to be save.
but i keep getting this error:
Exception calling"DownloadFile" with "2" argument(s):"An exception occurred
during a webClient request."
At line:1 chart:1
+$wc.DownloadFile($url,$output)
+$webclient.DownloadFile($url,"file)
+CategoryInfo :NotSpecified:(:)[], MethodInvocationException
Can anyone please tell me what am i doing wrong.
I have tried to download from my pc and copying to aws vis rdc but takes hours

The second parameter to the DownloadFile method is the path to the filename you want to use for saving the file - not the directory. You would need to change your line to:
$file = "$storageDir\SQLEXPRE_x64_ENU.exe"
Reference: https://msdn.microsoft.com/en-us/library/ez801hhe.aspx

Related

Unable to convert XML to JSON using [newtonsoft.Json.JsonConvert]::SerializeXmlNode method in powershell

I am trying to convert the content of a xml file to json using powershell.
When I call to the method, the powershell returns the error as below:
Method invocation failed because [Newtonsoft.Json.JsonConvert] does not contain a method named 'SerializeXmlNode'.
At line:1 char:1
+ $jsonText = [newtonsoft.Json.JsonConvert]::SerializeXmlNode($xmlDoc)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
I also checked the newtonsoft website below but the SerializeXmlNode method seemed to not have been deprecated:
newtonsoft - SerializeXmlNode
Kindly check my script below:
Import-Module newtonsoft.json
$filename = "E:\Udemy\Complete Guide to XML For Microsoft Developers\Lecture 5\XMLClass_Downloads_2021_03_28\IntroSamples\Flight01.xml"
#[xml]$xmlDoc = [IO.File]::ReadAllText($filename)
[xml]$xmlDoc = Get-Content $filename
Write-Host $xmlDoc.OuterXml
Write-Host "------------------------------------------------------------"
$jsonText = [Newtonsoft.Json.JsonConvert]::SerializeXmlNode($xmlDoc)
How can I convert the xml to json using SerializeXmlNode method or any of its equivalent ?
Kind regards,
Ken
Download the latest Json dll from here and unzip it somewhere after you have right-clicked the zip file, selected 'Properties' and unblocked it.
Suppose you have chosen path 'C:\Program Files\NewtonSoft' to store the unzipped folder, then put this at the top of your script:
$jsonDllPath = 'C:\Program Files\NewtonSoft\Json130r1\Bin\net40\Newtonsoft.Json.dll'
Add-Type -Path $jsonDllPath
$filename = 'E:\Udemy\Complete Guide to XML For Microsoft Developers\Lecture 5\XMLClass_Downloads_2021_03_28\IntroSamples\Flight01.xml'
# load the xml file. This way, you are ensured to get the file encoding correct
$xmlDoc = [System.Xml.XmlDocument]::new()
$xmlDoc.Load($filename)
Write-Host $xmlDoc.OuterXml
Write-Host "------------------------------------------------------------"
# this should now work:
$jsonText = [Newtonsoft.Json.JsonConvert]::SerializeXmlNode($xmlDoc)
Depending on the XML, if it has an xml declaration line or not, you may want to use [Newtonsoft.Json.JsonConvert]::SerializeXmlNode($xmlDoc.DocumentElement)

File opened that is not a database file (but it is)

I'm trying to pull information from an SQLite database using a Powershell script from a Redgate article.
#I've installed the 64 bit System.Data.SQLite ADO.NET data provider in this directory
Add-Type -Path "C:\Program Files (x86)\SQLite.NET\bin\x64\System.Data.SQLite.dll"
#I just create a connection to my existing database
$con = New-Object -TypeName System.Data.SQLite.SQLiteConnection
# I then give it a simple connection string
$con.ConnectionString = "Data Source=C:\Users\name\Desktop\fax.sqlite"# CHANGE THIS
#and open the connection
$con.Open()
#We'll just start by creating a SQL statement in a command
$sql = $con.CreateCommand()
$sql.CommandText = "select column from table;"
# we now execute the SQL and return the results in a dataset
$adapter = New-Object -TypeName System.Data.SQLite.SQLiteDataAdapter $sql
#we create the dataset
$data = New-Object System.Data.DataSet
#and then fill the dataset
[void]$adapter.Fill($data)
#we can, of course, then display the first one hundred rows in a grid
#(1..100)|foreach{$data.tables[0].Rows[$_]}|out-gridview #-Title 'Authors and books'
I viewed the database using SQLite DB Browser (didn't need a password to open). However, whenever I use this script I get the below error:
Exception calling "Open" with "0" argument(s): "File opened that is
not a database file file is encrypted or is not a database" At
C:\Users\name\Desktop\query sqlite db.ps1:9 char:1
$con.Open()
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SQLiteException
Assuming you have all the libraries in place, to import the assemblies you need to use the Add-Type command:
Add-Type -Path "C:\Program Files\System.Data.SQLite\2010\bin\System.Data.SQLite.dll"
To connect to the database using the ADO.NET protocol, you need to create a SQLiteConnection object with the proper connection string:
$connection_details = New-Object -TypeName System.Data.SQLite.SQLiteConnection
$connection_details.ConnectionString = "Data Source=C:\database\test.db"
Your $con.ConnectionString = "Data Source=C:\Users\name\Desktop\fax.sqlite" is having issue. It is expecting a proper DB connection but instead it is getting a string statement.
I got the same error when running a 32-bit dll on a 64-bit system.
If you are running a 64-bit Windows system then download sqlite-netFx46-static-binary-x64-2015-1.0.116.0.zip from https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
and extract the contents to C:\Program Files\System.Data.SQLite\2010\bin\
Then run this command once or place it in your PowerShell script before making your connection object:
Add-Type -Path "C:\Program Files\System.Data.SQLite\2010\bin\System.Data.SQLite.dll"

Use powershell to download unknown files from a remote https url

I'm trying to download files from a remote url to a local folder. The filenames that exist on the remote side will never be known. Similar to how you would just use: copy c:\downloadfiles*.* c:\dump
For now let's assume the url is open to everyone (no credentials needed):
$localPath = "C:\dump"
$url = "https://remoteserver/folder"
$WebClient = New-Object "System.Net.WebClient"
$WebClient.DownloadFile($url, $localPath)
But I just keep getting:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
I realize that you probably can't download with HTTP/HTTPS using wildcard but perhaps we can parse a remote file from "https://remoteserver/folder/FileList.txt" and use wildcard in there somehow, even if it's just by filetype *.jpg.

powershell System.Net.WebClient.DownloadFile(String, String) throws cryptic errors

This is my first ever question on stack exchange, so I ask forbearance in the face of breaking community etiquette. If I can clarify any part of my question, I would be more than happy to do so for you.
I'm learning powerShell for my internship, and currently I'm working on a script that pulls a .csv file from a sharepoint site. My aim is to take the file downloaded, do stuff, and then put it back on the site. Before I can do the other things, I'm trying to download the file, and my root issue lies in .net.webclient's .downloadfile() method. At execution I keep seeing a vague exception that I can't find information for anywhere on the web or among my colleagues. Below I will include the snippet involved, and the error message. Below that I will give you an Idea of troubleshooting steps I've taken.
--------code
## Download summary file from Sharepoint downloads new serverlist
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls
$webclient = New-Object -TypeName System.Net.WebClient
$webclient.Credentials = $spCreds ## defined earlier with import-clixml - also tried get-credential
$webclient.DownloadFile($URIaddress, $DownloadPath) ## defined earlier: $DownloadPath = "c:\temp\Summary.csv"
--------exception
Exception calling "DownloadFile" with "2" argument(s): "An exception
occurred during a WebClient request."
At line:13 char:5
+ $webclient.DownloadFile($URIaddress, $DownloadPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
--------action taken
The credentials I provide should be valid because I am able to download the contents of $URIaddress when I enter the URI into my browser.
I've been able to coax .downloadFileAsync(string, string) into creating a file at the specified $downloadPath, but the download never seems to commence, and the file stays empty. When try to .DownloadFile() into a random variable ($foo), $foo | gm reveals to be null.
If any of you have ideas I'd love to give them a shot!
Indeed quite cryptic.
Have a look at the exception and the inner exception these might provide you with more insights on what happened:
$Exception = $error[0].Exception
$Exception.InnerException

PowerShell: how to move a file from a remote computer to a network share

I am using PowerShell to move a file from a network share to a remote computer (which I do not have admin rights on). The source path on the network share is \\share_computer\some_folder\file1.txt. The destination path to the file on the remote computer is \\remote_computer\d$\another_folder.
A simple Move-Item $from $to doesn't work. I get a PermissionDenied message when I try to access the network share. However, I have confirmed that I can access the shared file via something like
`$data = Get-Content "\\share_computer\some_folder\file1.txt"
$var = $data[0]`
I then tried the following:
$src = "\\share_computer\some_folder\file1.txt"
$dest = "\\remote_computer\d$\another_folder"
$username = "my_username"
$password = "my_password"
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.NetworkCredential($username, $password)
$WebClient.DownloadFile($src, $dest)
PowerShell is throwing the following error:
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
I don't know why PowerShell is throwing this error. Assuming the above is the correct technique to move the file, what do I need to do to correct it? Or, if the above is the incorrect technique, what should I do?