The PS1 needs to download an XML file from a remote HTTP server and save it on a network share.
Summary of the script is as follows
$timer = (Get-Date -Format yyyyMMdd_hhmmss)
$storageDir = "\\10.16.99.99\what\ever\Folder"
$filename1 = "John_" + "$timer.xml"
$webclient = New-Object System.Net.WebClient
$url1 = "http://whateverURL/John.xml"
$file1 = "$storageDir\$filename1"
$webclient.DownloadFile($url1,$file1)
$source = "\\10.16.99.99\what\ever\Folder"
$target = "\\10.16.99.99\what\ever\TargetFolder"
$files = get-childitem $source -Recurse
foreach ($file in $files){
if ($file.LastWriteTime -ge (get-date).AddMinutes(-10)){
$targetFile = $target + $file.FullName.SubString($source.Length)
New-Item -ItemType File -Path $targetFile -Force
Copy-Item $file.FullName -destination $targetFile
Start-Sleep -s 3
}
}
When I run the script, I get following error through PowerShell CMD
Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient
request."
At D:\StagisEE\GlobalListing\GL_download.ps1:318 char:3
+ $webclient.DownloadFile($url1,$file1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
Therefore I added the following lines to see what the variables are that will be used in the webClient command
Write-Host $url1
Write-Host $file1
Which gave me the following, additional, results
http://whateverURL/John.xml
\\10.16.99.99\what\ever\Folder\John_20191224_103412.xml
My question is why that the webClient is failing while the variables seems to be ok?
Or is he struggling due to the network location?
Related
Im new to scripting and powershell so bare with me.
My goal is to connect to Sharepoint online, and download the latest file uploaded to the a specific site.
Currently I am able to Connect to Sharepoint with Connect-PnPonline, then i use PnPListItem to query the file i need, but i am stuck at the last step to download the file.
here is my code:
cls
$SPOsite="https://example.sharepoint.com/sites/example"
$TargetFile="C:\example"
Connect-PnPOnline -Url $SPOsite -ClientId xxxxx-xxxxx-xxxxx-xxxx -ClientSecret secretsecret112233
$ListItems = Get-PnPListItem -List Documents | select -last 1
$ctx= Get-PnPContext
foreach ($item in $ListItems)
{
$file = $item.file
$fileversions = $file.Versions
$ctx.load($file)
$ctx.load($fileversions)
$ctx.ExecuteQuery()
Write-Host $file.Name,$fileversions.VersionLabel
}
Get-PnPFile -Url "https://example.sharepoint.com/sites/example" -Filename $ListItems -Path $TargetFile -AsFile
Error message that i get is
Get-PnPFile : File Not Found.
At \\cifs-share.example.org\Usershome\user\Desktop\PowerShell\dwnldSPfiles.ps1:20 char:1
+ Get-PnPFile -Url "https://example.sharepoint.com/sites/example" -Fi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (:) [Get-PnPFile], ServerException
+ FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Files.GetFile
Try this:
$SharePointFolderPath = "Shared Documents"
$Files = Get-PnPFolderItem -FolderSiteRelativeUrl $SharePointFolderPath -ItemType File
foreach($File in $Files) {
if($File.Name -eq "file name"){
Get-PnPFile -Url $File.ServerRelativeUrl -Path c:\Temp -FileName $File.Name -AsFile
}
}
Url should be the ServerRelativeUrl to the file which you want to download.
I am a beginner at powershell script and I am stuck
I am trying to upload 2 xml files with the same name to FTP, but I am getting the following error
Exception calling "UploadFile" with "2" argument(s): "An
exception occurred during a WebClient request." At
C:\powershell\ExternalWidgets\upload-to-ftp.ps1:51 char:16
+ $webclient.UploadFile($uri.AbsoluteUri, $LocalPath)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException`
Here is the script:
#SET CREDENTIALS
$credentials = new-object System.Net.NetworkCredential($user, $pass)
cd $target
$DirectoryContent = Get-ChildItem -Directory
foreach($item in $DirectoryContent){
cd $item
$currentFolderItems = Get-ChildItem
foreach( $subfolderItem in $currentFolderItems)
{
if ($subfolderItem.name -eq "package.json") {
$subItem = Get-ChildItem -Directory
foreach($subItem in $subItem)
{
if ($subItem.name -eq "coverage") {
$files = Get-ChildItem -Path $subItem
$n = 0
foreach ($file in $files)
{
#rename coverage file
if($file.Name -eq "cobertura-coverage.xml") {
$newFileName=$file.Name.Replace("cobertura-coverage.xml","cobertura-coverage$n.xml")
Rename-Item $file -NewName $newFileName
$newFileName
}
$n++
}
}
}
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object
System.Net.NetworkCredential($user,$pass)
$uri = New-Object System.Uri($ftp + "/" + $folder)
#Delete files from FTP
# $fileUploaded =($uri + "\" + $newFileName)
# $fileUploaded
# Remove-Item $fileUploaded
$LocalPath =($target + "\" + $item + "\" + "coverage" + "\" +
$newFileName).Replace(".\", "").Replace('\', '/')
$webclient.UploadFile($uri, $LocalPath)
#cd..
}
}
cd ..
}
I also get an error when I want to rename one of the files. What step am I missing? Thank you
I am new to PowerShell and don't have much of a programming background, just trying to use below power shell script to install few softwares. The script throws error while executing for WinSCP software.
Error message
Program 'WinSCP-5.13.1-Setup.exe' failed to run: The file or directory is corrupted and unreadableAt line:1 char:1
+ D:\softwares\WinSCP-5.13.1-Setup.exe /
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
At line:1 char:1
+ D:\softwares\WinSCP-5.13.1-Setup.exe /SP
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
Script:
$source = 'D:\softwares'
If (!(Test-Path -Path $source -PathType Container)) {New-Item -Path $source -ItemType Directory | Out-Null}
$packages = #(
#{title='7zip Extractor';url='http://downloads.sourceforge.net/sevenzip/7z920-x64.msi';Arguments=' /qn';Destination=$source},
#{title='Notepad++ 7.5.6';url='https://notepad-plus-plus.org/repository/7.x/7.5.6/npp.7.5.6.Installer.exe';Arguments=' /Q /S';Destination=$source}
#{title='WinScp 5.13.1';url='https://winscp.net/download/WinSCP-5.13.1-Setup.exe';Arguments=' /';Destination=$source}
)
foreach ($package in $packages) {
$packageName = $package.title
$fileName = Split-Path $package.url -Leaf
$destinationPath = Join-Path $package.Destination $fileName
If (!(Test-Path -Path $destinationPath -PathType Leaf)) {
Write-Host "Downloading $packageName"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($package.url,$destinationPath)
#Start-Sleep -s 10
}
#Once we've downloaded all our files lets install them.
foreach ($package in $packages) {
$packageName = $package.title
$fileName = Split-Path $package.url -Leaf
$destinationPath = Join-Path $package.Destination $fileName
$Arguments = $package.Arguments
Write-Output "Installing $packageName"
Invoke-Expression -Command "$destinationPath $Arguments"
}
}
https://winscp.net/download/WinSCP-5.13.1-Setup.exe is not a download URL. It's a download page. Your code downloads an HTML document, not an executable binary.
You actually cannot download a binary automatically from WinSCP site. It's designed to prevent an abuse of its bandwidth, for uses like yours.
The URL above gets you an executable file only because the HTML, that it returns, includes a JavaScript code that redirects your browser to one-time download URL. Of course, you can implement the same (get the HTML and find/generate the download URL using the data it returns). But it's an abuse.
I have a simple script here to archive logs that begin with the name "Archive" then delete those files leaving only the archive.
cd L:\
$Source = Get-ChildItem L:\ | Where{$_.Name -match "^Archive.*\.evtx$"} |Get-ChildItem -name
$CurrentDate = get-date -Format M.d.yyyy
$Destination = "$CurrentDate.zip"
Compress-Archive -Path $Source -destinationpath $Destination
rm L:\$Source
However, I receive the below error when the script runs:
Exception calling "Write" with "3" argument(s): "Stream was too long." At
C:\windows\system32\windowspowershell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:809
char:29
+ ... $destStream.Write($buffer, 0, $numberOfBytesRead)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : IOException
Any suggestions?
Compress-Archive relies upon the Microsoft .NET Framework API System.IO.Compression.ZipArchive to compress files, the maximum file size that you can compress by using Compress-Archive is currently 2 GB. This is a limitation of the underlying API.
Please see more at : here
i would recommend using 7z.exe portable. here is my example script
$TempFolderName = "I:\TempLogs2\"
$source = $TempFolderName+'\Archive'
$destination = $TempFolderName+'\Archive'
$azcopylogs = Get-ChildItem $source -Include *.log -Recurse
foreach ($s in $azcopylogs) {
$azcopylogPath = $s.FullName
$azcopylogName = $s.Name
C:\Tools\7Z\7z.exe a -t7z $azcopylogPath'.7z' -r $azcopylogPath
;if (Test-Path $azcopylogPath'.7z') { Remove-Item $azcopylogPath -force }
}
I have a script that I am trying to get to sort through a directory and open all the zip files and store the text files all to one directory. Here is the code:
#Script to open zip files in tree
New-Item E:\Files -type directory
Get-ChildItem -Path E:\SNL_Insurance\* -Recurse -Exclude "*.md5"|
ForEach-Object {
$file = $_
write-host $file;
$destination = "E:\Files"
$shell = New-Object -com shell.application
$zip = $shell.NameSpace($file) |
foreach($item in $_.items()){
$shell.Namespace($destination).copyhere($item)
}
}
I think I almost have it, but keep getting this error (any elaboration on piping would be helpful):
Unexpected token 'in' in expression or statement.
At E:\Expand-ZIPFile.ps1:14 char:19
+ foreach($item in <<<< $_.items()){
+ CategoryInfo : ParserError: (in:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
EDIT:
Ahh... thanks for that distinction. I made your edits but after each of my "write-host" checks to see the file name I get the following error:
`You cannot call a method on a null-valued expression. At E:\Expand-ZIPFile.ps1:14 char:30 + foreach($item in $zip.items <<<< ()){ + CategoryInfo : InvalidOperation: (items:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
EDIT2: So the original code does copy files to a new directory but also copies the whole zip file over. I tried to add an if statement to only copy files that are .txt but the code just steps through each directory without copying anything. If you have any idea that would be appreciated as I have exhausted all my ideas. Here is the code:
new-Item E:\Files -type directory
Get-Childitem -path E:\SNL_Insurance\* -recurse -exclude "*.md5" |
Foreach-object {
$file = $_
write-host $file;
$destination = "E:\Files"
$shell = new-object -com shell.application
$zip = $shell.NameSpace($file.Fullname)
foreach($item in $zip.items()){
if ($item.Extension -eq ".txt") {
$shell.Namespace($destination).copyhere($item)
}
}
}
You're confusing foreach with ForEach-Object loops. The former neither read from nor do they write to pipelines. Also, $file is a FileInfo object, not a path. The NameSpace method expects a path string, so you need to use $file.FullName.
Replace
$zip = $shell.NameSpace($file) |
foreach($item in $_.items()){
$shell.Namespace($destination).copyhere($item)
}
with
$zip = $shell.NameSpace($file.FullName)
foreach($item in $zip.items()){
$shell.Namespace($destination).copyhere($item)
}