Connect to SFTP using PowerShell and download files - powershell

I am aware that there is a similar question asked previously but the solutions primarily focus on using WinSCP. I'm fairly new to SFTP and PowerShell as well, and I need help downloading files from an SFTP server onto my application server.
I believe that the Posh-SSH module can help with establishing an SFTP connection, but most code I have seen until now involves uploading files onto the SFTP server, not downloading from... would appreciate any advice!

Just use Get-SFTPItem cmdlet (Get-SFTPFile in older versions):
$remotePath = "/remote/path/file.ext"
$localPath = "C:\local\path\file.ext"
Get-SFTPItem -SessionId $sessionId -Path $remotePath -Destination $localPath
WinSCP will work too though:
Download files from SFTP server using PowerShell

Related

Hello do i'm trying to code a powershell script that upload and overwrite a folder to an other folder

So I'm basically new to powershell coding and I don't really know if I'm doing this right - just started a few days ago.
My main goal is to send a folder to a ftp server and overwrite the folder that's already on the server due to daily upload. Sorry but I'm a bit of a nerd and would like a few recommendations if I'm doing this well or not. Thank you
I tried to code something using powershell and a batch file .
And it actually work's but localy on my pc ( copy the folder and paste it in wanted folder) But it just won't work when trying to do this on a remote ftp server that's saved on my pc.
script:
[string]$sourceDirectory = "C:\test\*"
[string]$destinationDirectory = "C:\Users\c0ld\Desktop/receive "
Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $destinationDirectory
exit
Bat script:
#ECHO OFF
PowerShell.exe -Command "& 'C:/test.ps1'"
PAUSE
So this works when working on actual local folder, but when trying to do this on a network FTP folder it's actually not working.
First make sure you have the correct permissions to access the server.
Then remember(this got me a couple times) that instead of c:\path, you would use the admin share to access a remote pc, which is \remotecomputer\c$\path.

PowerShell - Trouble copying items remotely with invoke-command and copy-item

I have hundreds of computers on domain I am responsible for and I want to copy a file from a file server to their hard drives locally. If I script this to initiate the copy from my (separate) computer it works just fine via something like;
Copy-Item -LiteralPath \\FILESERVER01\Share\Files\Win7_64\File_to_copy.txt -Destination \\WORKSTATION01\c$\Users\USER\Desktop\ -Force }
However I'd like the script to initiate a copy from the file server to the workstation on the workstation via invoke-command so this isn't done on my machine using something (I assume is) like this:
Invoke-Command -ComputerName WORKSTATION01 -ScriptBlock { Copy-Item -LiteralPath \\FILESERVER01\Share\Files\Win7_64\File_to_copy.txt -Destination \\WORKSTATION01\c$\Users\USER\Desktop\ -Force }
But doing this consistently gives me the error citing the source as the issue - Cannot find path '\FILESERVER01\Share\Files\Win7_64\File_to_copy.txt' because it does not exist. I have access to this network shared path from all machines (via Domain Admin rights), so I believe this is something like a syntax error or problem with the way the UNC path is being resolved but despite reviewing several topics on this I just can't determine what I'm doing wrong with this since I'm using the same UNC paths in the same commands outside the invoke-command cmdlet (and from a PowerShell session on WORKSTATION01) with no issues.
I've tried reviewing and attempting solutions from this link, and also this link. But I can't tell if its a slightly different use case or if I just cant figure out how to adjust the syntax to make it work for me but the Cannot find path error persists
I've tried using New-PSDrive, prefixing the UNC path with "FileSystem:" and even taking the explicitly shared folders out entirely and using only the full path using administrative shares but I just seem to be missing something.
Can anyone shed some light on what I'm missing here?

Extracting .exe file using powershell

I am trying to extract .exe file using powershell without any other tools.
I tried to use System.IO.Compression.ZipFile, but that works only for .zip files..
$zip_file = Get-Item ("C:\Users\00WORK\gs\gs.exe")
$destination = Get-Item ("C:\Users\tuna")
[System.IO.Compression.ZipFile]::ExtractToDirectory($zip_file,$destination)
Also tried this, but without any success
start-process C:\Users\Downloads\gs.exe -Argumentlist "/a"
Tried also this but once again without any succes
$shell = new-object -com shell.application
$zip = $shell.NameSpace(“C:\Users\00WORK\gs\gs.exe”)
foreach($item in $zip.items())
{
$shell.Namespace(“C:\Users\tuna”).copyhere($item)
}
Thanks for help.
If you have tried
start-process C:\Setup.exe -Argumentlist "/a"
Then it is not possible using powershell, this command is completely dependant on how the file was packaged, if all else fails I personally would use a utility like 7-Zip, but as you said you would not like to use utilities.
I had this same problem recently as well. Dewi's answer would not have worked for me. I solved it like this:
mv gs.exe gs.zip
Expand-Archive -Path gs.zip
Optionally, you can rename it back when you're done, if you need the exe for other purposes.
mv gs.zip gs.exe
Note that you'll need PowerShell 5 or newer to use Expand-Archive. Thankfully, this is available as a download all the way back to Win 7 as Microsoft KB3191566.
Yeah well powershell doesn't recognize all compression algorithms. I am trying to do this with an HP driver pack file which comes in a self extracting .exe file. Even if you change the filename to a .zip it will not extract the files and instead throws a New-Object exception error.

How to copy the files from local system to the remote server by using powershell script

I'm running the command which i have shown you the image and its giving that error
Please suggest me on this
Actually am trying to copy the build from local system to the remote server by using powershell scirpt
and i want to copy the files from on remote server to the other remote server by using local system
please provide me solutions for this and what are the requirements for this
You can use the copy-item
copy-item -path "Your\path\to\folderorfile" -destination "\\servernameorip\dest\location\"
if you have different credentials on the destination server, you can either trick windows by creating a local account using same name and password on your mcopy with different credentials

Powershell Get-ChildItem doesn't work properly on IIS directory

I was going to write up a simple alias 'iis' to invoke the IIS Manager, which is 'C:\Windows\System32\inetsrv\InetMgr.exe'
set-alias iis "OpenIIS.ps1"
and in the OpenIIS.ps1 I have
$item = "C:\Windows\system32\inetsrv\InetMgr.exe"
invoke-item -path $item
This doesn't work. The error I get is "The system cannot find the file specified"
In fact, just doing a Get-ChildItem on the inetsrv won't show the InetMgr.exe (no difference with -Force switch)
Get-ChildItem C:\Windows\system32\inetsrv\*.exe -force
Obviously I can see it in Explorer and I can launch it using cmd, but not with Powershell it seems. Also, Powershell is running as Administrator.
What is going on?
As a workaround I tried creating a link to the file and then invoking that link from Powershell. I now get a 'NotSpecified' Win32Exception.
I have originally used 64 bit Powershell, but get the same result on the x86 Powershell (both run as Administrator)
Are you at the elevated PowerShell prompt? Some system files may not show up unless you use -Force parameter with Get-ChildItem.
I think evidently the file InetMgr.exe is not there as when I do a get-childitem in the mentioned directory,it lists the "InetMgr.exe" there.
This may not be the problem with Get-ChildItem or the Alias you created but instead with ur IIS Server.