Powershell compare size of local file to file on sharepoint - powershell

I am using the current code to download a file from a sharepoint...
$webClient = New-Object System.Net.WebClient
$webClient.UseDefaultCredentials = $true
$webClient.DownloadFile($sharepointPathFile, $localPathFile) | Out-Null
But what if I wanted to check if the file is already at the local location and the size matches or is different? How would I do this using powershell?
Update
This was the closest I could get...
$url = $sharepointPathFile
$clnt = [System.Net.WebRequest]::Create($url)
$resp = $clnt.GetResponse()
$fileSize = $resp.ContentLength
Write-Host $fileSize
But I am getting the following error:
Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."
At C:\Scripts\Tests\testCheckUpdatedSearchFiles.ps1:345 char:2
+ $resp = $clnt.GetResponse()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
I have full read and download rights, so is there something else not going right here?

I'm not sure if the "GetResponse" method will return exactly what you're looking for. And depending on the ContentLength, you may want to explicitly define your types. I would try something like this:
$webClient = New-Object System.Net.WebClient
$webClient.OpenRead("path/to/file")
[Int64]$fileSize = $webClient.ResponseHeaders["Content-Length"]
Write-Host $fileSize

Related

"Invalid URI: The hostname could not be parsed" + "The requested URI is invalid for this FTP command" when downloading using WebClient in PowerShell

I am trying to connect one FTP server using PowerShell like below.
$line = 'MilanRamani.json'
$file = "C:\brivo\json\" + $line
$ftpuri = "ftp://theflowregister\selfregisterflow:Buter239##waws-prod-am2-555.ftp.azurewebsites.windows.net/site/wwwroot/json/" + $line
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftpuri)
$webclient.DownloadFile($uri,$file)
$webclient.Dispose()
Where theflowregister\selfregisterflow is a username, Buter239# is password, waws-prod-am2-555.ftp.azurewebsites.windows.net/site/wwwroot is host and json/ is subfolder.
I am trying to copy one file named MilanRamani.json from FTP and download it at a particular location in the system. but I am getting this error when I execute the above code.
New-Object : Exception calling ".ctor" with "1" argument(s): "Invalid URI: The hostname could not be parsed."
At line:5 char:8
+ $uri = New-Object System.Uri($ftpuri)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId :
ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "DownloadFile" with "2" argument(s): "The requested URI is invalid for this
FTP command."
At line:6 char:1
+ $webclient.DownloadFile($uri,$file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
The # (hash/number sign) has special meaning in URL. If you want to use it explicitly, you have to URL-encode it to %23. You might also have to URL-encode the \ (backslash) as %5C. In general, you can use Uri.EscapeDataString to encode the credentials (and also the filename):
$ftpuri =
"ftp://" +
[Uri]::EscapeDataString("theflowregister\selfregisterflow") + ":" +
[Uri]::EscapeDataString("Buter239#") +
"#waws-prod-am2-555.ftp.azurewebsites.windows.net/site/wwwroot/json/" +
[Uri]::EscapeDataString($line)
An alternative and safer approach is to set the credentials via WebClient.Credentials property, instead of the URL:
$ftpuri =
"ftp://waws-prod-am2-555.ftp.azurewebsites.windows.net/site/wwwroot/json/" +
[Uri]::EscapeDataString($line)
$uri = New-Object System.Uri($ftpuri)
$webclient = New-Object System.Net.WebClient
$webclient.Credentials =
New-Object System.Net.NetworkCredential(
"theflowregister\selfregisterflow", "Buter239#")

Error downloading file from internet using PowerShell

I am recevining an authorization error on the below code. When I change the download location to C:\temp I do not get this error. See below code and error.
I need to download to the files to the D drive as it is a shared folder
Changing to the C drive worked but I need it to be downloaded to the D drive
$File64 = "https://go.microsoft.com/fwlink/?LinkID=121721&arch=x64"
$Location64 = "D:\temp\mpam-fe64Bit.exe"
$File32 = "https://go.microsoft.com/fwlink/?LinkID=121721&arch=x86"
$Location32 = "D:\temp\mpam-fe32Bit.exe"
$Client = New-Object System.Net.WebClient
$Client.DownloadFile($File64, $Location64)
$Client.DownloadFile($File32, $Location32)
The error I receive is below
Exception calling "DownloadFile" with "2" argument(s): "The remote
server returned an error: (401) Unauthorized." At
C:\PSScripts\DownloadDefs.ps1:9 char:1
+ $Client.DownloadFile($File32, $Location32)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : WebException
I tried adding the below code but it still does not work
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential($user,$pwd)
$credCache.Add($source, "Basic", $creds)
$wc.Credentials = $credCache
Any help at all would be greatly appreciate

How to copy file from remoter server (which is on another domain) to local?

I want to copy a file from remoter server(which is on another domain) to local
I am new to powershell and got this code from tech forum, however its not working
$Source = "\\xx.xxx.xxx.xx\Users\test\test_1.txt"
$Dest = "D:\Demo\"
$Username = "domainname\username"
$Password = "xxx"
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$WebClient.DownloadFile($Source, $Dest)
getting below error
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:9 char:1
+ $WebClient.DownloadFile($Source, $Dest)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Pretty sure your problem is you need to specify the full file path. Not just a target folder. Looking at WebClient.DownloadFile we can see
fileName String
The name of the local file that is to receive the data.
So perhaps all you need to do is...
$Source = "\\xx.xxx.xxx.xx\Users\test\test_1.txt"
$Dest = [io.path]::Combine("D:\Demo\", Split-Path $source -Leaf)
It was probably denied access to write to a folder.

Saving Video Data To File From POST request InputStream in PowerShell

I am having trouble quite figuring out how to save video data sent to me via a HttpListener in PowerShell. I have the following which I believe is just sending it back to the requester but I'm having trouble just saving it into an MP4 file.
$req = $request
$body = $req.InputStream
$reader = New-Object System.IO.StreamReader ($body, $req.ContentEncoding)
$msg = $reader.ReadToEnd()
$reader.Close()
[byte[]] $buffer = [System.Text.Encoding]::UTF8.GetBytes($msg)
$res.ContentLength64 = $buffer.Length
$res.StatusCode = 200
$res.OutputStream.Write($buffer, 0, $buffer.Length)
$res.Close()
Thank you for your time eveyrone!
Update:
I have been able to make files with this, though for some reason in examples they're using 8192 sized byte but PowerShell says it's too big. With this I get zero length files, no errors that I can tell.
$path = "c:\matthew3.mp4"
$file = New-Object System.IO.FileStream $path,CreateNew
[byte]$bytes = 255
[int]$bytes_read = 0
while ( $bytes_read = $request.InputStream.Read($bytes, 0, $bytes.length) > 0 )
{
$file.Write($bytes, 0, $bytes_read)
}
Actually I did get an error:
Exception calling "GetBytes" with "1" argument(s): "Array cannot be null.
Parameter name: chars"
At line:45 char:1
+ $buffer = [System.Text.Encoding]::UTF8.GetBytes($content)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Exception calling "Write" with "3" argument(s): "Value cannot be null.
Parameter name: buffer"
At line:47 char:1
+ $response.OutputStream.Write($buffer, 0, $buffer.Length)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
So the main developer for our company Nick pointed me in the right direction here.
The main thing is that for the FileStream object I needed to add the Write flag, and use the CopyTo method on the InputStream and then close both of them:
$file = New-Object System.IO.FileStream $path,CreateNew,Write
$context.Request.InputStream.CopyTo($file)
$file.Close()
$context.Request.InputStream.Close()

Powershell FTP - "The remote server returned an error: (550) File unavailable

I am trying to copy a single file to an FTP server. I found this code online, but cannot get it to work. I get the following error message:
Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (550) File unavailable (e.g., file
no access)."
At C:\bin\Put-FTP.ps1:22 char:1
+ $webclient.UploadFile($uri, $File)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Any idea why this may be occuring? I've checked the destination FTP server and this user has full access to write, read, delete and list contents.
Thanks!
$File = "C:\bin\emp1.xlsx"
$ftp = "ftp://user:password#ftp.server.com/User/emp1.xlsx"
"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Uploading $File..."
$webclient.UploadFile($uri, $File)