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
Related
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#")
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.
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
I'm trying to create a new server audit on a WinServer 2008 R2 with the following PowerShell Script.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$server = "SM1111" #change to desired instance
$instance = "S111"
$auditName = "$instance"+"TestAudit"
$auditDir = 'F:\Microsoft SQL Server\'+$instance+'AuditTestLogsNew\'
$srv = new-Object ('Microsoft.SqlServer.Management.Smo.Server') -argumentlist $instance
$newAudit = New-Object Microsoft.SqlServer.Management.Smo.Audit($srv, "$auditName")
$newAudit.DestinationType = [Microsoft.SqlServer.Management.Smo.AuditDestinationType]::File
$newAudit.FilePath = $auditDir
$newAudit.MaximumRolloverFiles = 10
$newAudit.MaximumFileSize = 100
$newAudit.QueueDelay = 1000
$newAudit.Create()
$newAudit.Enable()
However the following line always fails:
$newAudit = New-Object Microsoft.SqlServer.Management.Smo.Audit($srv, "$auditName")
I get the following error message:
New-Object : Exception calling ".ctor" with "2" argument(s): "SetParent failed for Audit 'S111TestAudit'. "
At MYFOLDER\Documents\Auditing_Test\CreateAudit.ps1:9 char:13
+ $newAudit = New-Object Microsoft.SqlServer.Management.Smo.Audit($srv, "$auditNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
I've been googling a lot, but still haven't found anything that might solve the problem, since I don't quite understand what raises the error to begin with.
I have full administrator privileges.
Any help would be appreciated!
You are missing the server name for your instance. Your variable $srv is not pointing to an actual server instance.
$server = "SM1111" #change to desired instance <- This isn't doing anything
$instance = "S111"
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server -argumentlist "$server\$instance"
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)