Getting below error on running PowerShell script - powershell

I am using one PowerShell script to download content (a zip file) from a website which requires credentials to get authenticated for the website. Also, have used proxy and proxy credentials since it is an authenticated proxy. I am getting an error which I have pasted below.
I have tried giving the proxy and proxy credentials but no luck.
I have bypassed the execution policy using Powershell -ExecutionPolicy Bypass.
When I try accessing the website from my browser then I am able to access the zip file.
Please find the code snippet below:
#Path to Save data
$tipath = "C:\Somepath"
$localpath = "\somefile.zip"
$timepath = "\Zip_file.txt"
$unzippath = "\Someotherpath"
$storepath = $tipath + $localpath
$remotefilepath = $tipath + $timepath
$unzipfolder = $tipath + $unzippath
# Create Folder If Not Exists #
$FolderToCreate = $tipath
if (!(Test-Path $FolderToCreate -PathType Container)) {
New-Item -ItemType Directory -Force -Path $FolderToCreate
}
#Downloading File #
$username = "username"
$password = "Password" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username, $password)
$secPasswd = "password123" | ConvertTo-SecureString -AsPlainText -Force
$wc = New-Object System.Net.WebClient
$wc.Credentials = New-Object System.Net.NetworkCredential("domain\user", $secPasswd)
# get remote file info
Invoke-WebRequest -Proxy "http://x.x.x.x:8080" -ProxyCredential $myCreds -Uri "https://somewebsite/checksum.txt" -OutFile $remotefilepath -Credential $cred
$remotefile = Get-Content -Path $remotefilepath
Write-Host Remote File Hash : $remotefile
# if the file exists already
if (Test-Path $storepath) {
# get local file info
$localfile = (Get-FileHash $storepath -Algorithm SHA256).Hash
Write-Host Local File Hash : $localfile
# if the remote file is newer than the local file
if ($remotefile -ne $localfile) {
Invoke-WebRequest -Proxy "http://x.x.x.x:8080" -ProxyCredential $myCreds -Uri "https://somewebsite/checksum.txt" -OutFile $remotefilepath -Credential $cred
Invoke-WebRequest -Proxy "http://x.x.x.x:8080" -ProxyCredential $myCreds -Uri "https://somewebsite/somezipfile.zip" -OutFile $storepath -Credential $cred
This code should check the hash and download directly download the zip file from the website and unzip it.
If it can download in the zip file then the later part I can troubleshoot.
PS C:\Users\Administrator\Desktop> powershell -ExecutionPolicy ByPass -File C:\Users\Administrator\Desktop\mypowershell.ps1
powershell : Invoke-WebRequest : .?? ??? ?????? ???????? ??? ????? ?????
At line:1 char:1
+ powershell -ExecutionPolicy ByPass -File C:\Users\Administrator\Desktop\mypowershell.p ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Invoke-WebReque...??? ????? ?????:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
The site you are trying to access contains content that is prohibited.
If you believe the website you are trying to access does not contain any such
content, please click here.
Above is the error I am getting.

Related

Powershell uploading files to SharePoint : Exception calling "ExecuteQuery" with "0" argument(s) error

I want to upload all .csv files from a C:/ to SharePoint. The script runs up to $Ctx.ExecuteQuery() and returns this error. I am able to run other PowerShell scripts on the site, and am the site owner, I suspect it is something other than access privileges.
Where do I start looking ?
Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
+ $Ctx.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Get-ChildItem "c:\users\xxx\downloads" -Filter *.csv | % {
$eachFile =Get-ChildItem "c:\users\xxxx\downloads\" -Filter *.csv
$eachFile | ForEach-Object{
#parameters
$SiteURL = "https://xxx.sharepoint.com/sites/nrg"
$LibraryName="Audit History"
$UserName = "xxxx#x.onmicrosoft.com"
$Password = "xx"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$Ctx.Credentials = $Credentials
#Get the Library
$Library = $Ctx.Web.Lists.GetByTitle($LibraryName)
#Get the file from disk
$FileStream = ([System.IO.FileInfo] (Get-Item $OutputFile)).OpenRead()
#Get File Name from source file path
$SourceFileName = Split-path $eachFile -leaf
#sharepoint online upload file powershell
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $SourceFileName
$FileUploaded = $Library.RootFolder.Files.Add($FileCreationInfo)
#powershell upload single file to sharepoint online
$Ctx.Load($FileUploaded)
$Ctx.ExecuteQuery()
#Close file stream
$FileStream.Close()
write-host "File has been uploaded!"
}
}
Solution was to remove the Credentials to before the ForEach-Object.

How to do looping FTP uploads and downloads with powershell

I have a project here that I am trying to automate. I have some load tests i need to run on a machine and during that time I need to have a constant in and out of FTP traffic. I have been trying to write a Powershell script to do it but have been coming up short. I can get the file to download every time but the upload always fails. I also need to make sure my looping mechanism is correct.
I need this to download and upload a single file until I tell it to stop. Here is the script.
$FTPServer = '172.20.2.124'
$FTPUsername = 'ftp'
$FTPPassword = 'ftpTransfers'
$FTPSecurePassword = ConvertTo-SecureString -String $FTPPassword -asPlainText -Force
$FTPCredential = New-Object System.Management.Automation.PSCredential($FTPUsername,$FTPSecurePassword)
$Path = '/'
$LocalPath = 'C:\Downloads'
#Open session
Set-FTPConnection -Credentials $FTPCredential -Server $FTPServer -Session MySession -UseBinary -KeepAlive
$Session = Get-FTPConnection -Session MySession
#download file
Get-FTPItem -Path /File1.mxf -LocalPath $LocalPath -Overwrite -Session $Session | out-null
#Upload the file
Add-FTPItem -LocalPath $LocalPath/File1.mxf -Path $Path -Overwrite -Session $Session | out-null
do {
response = Read-Host "repeat?"
}
while ($response -eq "Y")
This is the error I get whenever I try to do an upload with the script. I have tried to figure out how to close the stream but I have not had any luck.
Add-FTPItem : Exception calling "GetRequestStream" with "0"
argument(s): "Object reference not set to an instance of an object."
At C:\Powershell Scripts\FTPUploadandDownload.ps1:22 char:1
+ Add-FTPItem -LocalPath $LocalPath/File1.mxf -Path $Path -Overwrite -Session ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-FTPItem
Any help would be appreciated, I am fairly new to this level of powershell.

Having trouble include all child items when moving files from local machine to fileserver using Powershell

I want to upload these local files from system to the fileserver but if i try to use Get-ChildItem I get this message:
Cannot convert argument "address", with value: "System.Object[]", for
"Downl convert the "System.Object[]" value of type "System.Object[]"
to type "Syste At C:\Users\Administrator\Desktop\MOVEFILES2.ps1:23
char:1
+ $WebClient.DownloadFile($Source, $Dest)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Here is the working script. I want to modify so that it pulls from that path and not having to specify the file name.
#UPLOAD FILES FROM SOURCE TO File server
# Specify the path to the documents you want to upload from the local machine...
$Source = "C:\Users\Administrator\Desktop\SourceFolder\Goober.docx"
$Dest = "\\10.112.4.111\xx\xxxxx\xxxx xxxxx\xxxx\Goober.docx"
# Specify Username and Password
$Username = "domain\user"
$Password = "xxxxxxxxxxx"
# Generate System.Net.WebClient object
$WebClient = New-Object System.Net.WebClient
$WebClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$WebClient.DownloadFile($Source, $Dest)
I am unable to test this because of the credential thing, but i think this is a simplified version of what you're trying to achieve.
It should Copy all the items in the Sourcefolder to the Destination, Bassicly it maps a drive and then copies it all and then removes the Drive.
$sourceFolder = "C:\Users\Administrator\Desktop\SourceFolder"
$Destination = "\\10.112.4.111\xx\xxxxx\xxxx xxxxx\xxxx"
$secpw = ConvertTo-SecureString "Password" -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential("Username",$secpw)
New-PSDrive -Name "Dest" -PSProvider FileSystem -Root $dest -Credential $creds
Copy-Item $sourceFolder "Dest:\" -Recurse
Remove-PSDrive -Name "Dest"

using PsFtp to upload a file to FTP Powershell

I've been wracking my brain on this issue and can't seem to fix it. I'm trying to upload a file to FTP using PSFTP.
The script I'm using:
#------------------------------------------------------
#local variables
$ftp_server = "SERVERNAME"
$ftp_path = "/FTPPATH/PATH"
$local = "C:\ftp\"
$local_in = Join-Path $local "In"
$local_out = Join-Path $local "Out"
$session = "my_ftp_session"
# set up credentials object
$username = "FFandP"
$password = Get-Content "$local_out\Credentials.txt" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
Set-FTPConnection -Server $ftp_server -Credentials $cred -Session $session -KeepAlive -confirm -UseBinary
Get-ChildItem -Path $local_out |
% {
$ftp_file = "$ftp_path/$($_.Name)" # determine item fullname
Add-FTPItem -Path $ftp_file -LocalPath $_.FullName -Session $session -
}
# -------------------------------------------------
And the error I receive:
Add-FTPItem : Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (550) File
unavailable (e.g., file not found, no access)."
At line:22 char:1
+ Add-FTPItem -Path $ftp_file -LocalPath $_.FullName -Session $session
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-FTPItem
I've tried running the Add-FTPitem command by itself, but I get the same error.
I can upload to the FTP using FileZilla. I have also tried removing the variables and using hard-coded paths; I get the same error.
Any ideas?
The answer in #Josh's comment solved it for me. Run Add-FTPItem with the -Overwrite parameter.
Add-FTPItem -Path $remotePath -LocaPath $myPath -Overwrite
It took me a moment to figure out this problem, but here is my solution (I had the same problem).
When using Add-FTPItem the -Path parameter must not include the filename itself.
Add-FTPItem
-Path "ftp://SomeServer/SomeDir/"
-LocalPath "C:\SomeFilename.ext"
-Session $session
So in your example it should be:
Add-FTPItem -Path $ftp_path -LocalPath $_.FullName -Session $session
The filename will be added to the remote FTP path. In case you don't want to have the same name you must either rename the file locally first or remotely after.
Change block
Get-ChildItem -Path $local_out | %{ .... }
to one line
Get-ChildItem -Path $local_out | Add-FTPItem -Path $ftp_path

Powershell BitsTransfer does not complete

I'm sorry to keep asking about Powershell, my script-foo is not what it needs to be.
I'm writing a BitsTransfer .PS1 to automate the weekly download of an ASCII file.
It never seems to complete and reach a status of "Transferred" and seems stalled in a "Transferring" state. I can see a TMP file in my -Destination folder, with my ASCII data in it.
When I manually download the target file and compare it to the TMP file, they're the same size and appear to have the same first and last records. I assume the download is done.
If I manually run Get-BitsTransfer | Complete-BitsTransfer, the TMP file disappears but still no -Destination file.
My script is nothing sophisticated...
$date= Get-Date -format yyMMdd
$ntispasswd = ConvertTo-SecureString "*******" -AsPlainText -Force
$ntiscreds = New-Object System.Management.Automation.PSCredential ("*******", $ntispasswd)
$jobdescriptor = "DMFWA" + $date
$dmfpath = "C:\DMF"
# -Source https://dmf.ntis.gov/dmldata/weekly/WA$date `
Import-Module BitsTransfer
Start-BitsTransfer `
-DisplayName $jobdescriptor `
-Priority High `
-ProxyUsage Override `
-ProxyList mckwebfilt1:3128 `
-RetryInterval 60 `
-TransferType Download `
-Source https://dmf.ntis.gov/dmldata/weekly/WA130322 `
-Destination $dmfpath\TestWA$date.txt `
-Authentication Basic `
-Credential $ntiscreds `
-Asynchronous
$job = Get-BitsTransfer $displayname
While($Job.Jobstate -ne 'Transferred'){
$job
Start-Sleep -s 1
}
Complete-BitsTransfer $job
Can anybody help me understand what I'm doing wrong?
You did it the right way:
Import-Module BitsTransfer
Start-BitsTransfer -Source $url -Destination $output -Asynchronous
Get-BitsTransfer | Complete-BitsTransfer
Possible failures
the destination paramater is wrong$dmfpath\TestWA$date.txt
there are more than 60 BitTransfers running, end them with Get-BitsTransfer | Remove-BitsTransfer
I used too much time on BitsAdmin trying a transfer a file which was never finished because the file length was not given from the server.
Start-BitsTransfer : Die Dateigröße wurde vom Server nicht zurückgegeben.
Möglicherweise enthält die URL dynamischen Inhalt. Der
Inhaltslängenheader ist in der Server-HTTP-Antwort nicht verfügbar.
In Zeile:1 Zeichen:1
+ Start-BitsTransfer http://***/file c:\users\***\file.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Start-BitsTransfer],Exception
+ FullyQualifiedErrorId :
StartBitsTransferCOMException,Microsoft.BackgroundIntelligentTransfer.
Management.NewBitsTransferCommand
After trying the same from powershell with Start-BitsTransfer it was the same behavior.
This solution is really awesome and fixed also my problem! Thank you!
$request = New-Object System.Net.Webclient
$passwd = ConvertTo-SecureString "**" -AsPlainText -Force
$request.Credentials = New-Object System.Management.Automation.PSCredential ("**", $passwd)
$request.Downloadstring("https://my full target url")
Look at the last example on this web page about using Start-BitsTransfer and see if that helps.
This turned out to be a "Royal PITA".
Thank you Kieth, for the hint.
I couldn't get BitTransfer to work properly and resorted to something like this..
$request = New-Object System.Net.Webclient
$passwd = ConvertTo-SecureString "**" -AsPlainText -Force
$request.Credentials = New-Object System.Management.Automation.PSCredential ("**", $passwd)
$request.Downloadstring("https://my full target url")