Powershell - Copy files from local machine to server machine [duplicate] - powershell

This question already has answers here:
Copy file remotely with PowerShell
(5 answers)
Closed 4 years ago.
local machine = from where you are executing command
Server machine = where you copy files.
copy (localfile) to server machine

You need to have administrative access on both machines.
Copy-Item -Path "\\\ServerB\SharedFile" -Destination "$Env:USERPROFILE" -Force -PassThru -Verbose
However, I do not recommend using powershell to do this. There are more reliable and easier methods. It is unlikely but this may corrupt a file.

Related

Does Powershell not allow variables in the path for Start-Process? [duplicate]

This question already has answers here:
How do I pass a local variable to a remote `Invoke-Command`? [duplicate]
(2 answers)
Closed 2 months ago.
I am working on a script to install software to remote PCs, and I ran into an issue I have never experienced before. I am trying to pass in the file name to Start-Process as a variable, but for some reason it does not like this, and hangs on the install. It will just sit indefinitely. The log file shows that this creates shows that the process terminated unexpectedly.
Invoke-Command -ComputerName $ComputerName -ScriptBlock { Start-Process msiexec "/i c:\$FileName /lvx* c:\PackageManagement.log" -wait }
$FileName is test.msi which exists on the target computers C: drive as I copied it there.
I have tried using Powershell joins, I have tried using concat, I have tried setting the Start_Process command to it's own variable and using that. It just hangs. The computer is on, connected to the internet. When I replace $FileName in the code above with test.msi directly, it installs just fine.
If you use $using:FileName in place of just $FileName, that will give you what you are looking for on Powershell 3.0 or later.

Connect to SFTP using PowerShell and download files

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

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.

Power shell Script execution from the AD starup scripts [duplicate]

This question already exists:
Power shell Script execution from the AD on the all domain joined machines
Closed 6 years ago.
I have written a very short power shell script, that will download and install the ossec agent's binaries from locally hosted HTTP server and execute it on the respective machines. The binaries are named with respective machines IP address. I want to execute this script on more than 500 hundred machines from my active directory as a start up script but it is not working.
I have tested the script directory from power shell on 5-6 machines and it is working. Can some one please review what am doing wrong ? Here is the simple powershell script.
$user=$env:username
$localIpAddress=((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
powershell -Command "(New-Object Net.WebClient).DownloadFile('http://192.168.10.220/$localIpAddress.exe','C:\Users\$user\Downloads\ossec.exe')"
cd "C:\Users\$user\Downloads"
.\ossec.exe /S
I'd check your ExecutionPolicy settings and possibly see if the file needs "unblocking" - see here for more info.
On a different note, using WMI is a slightly cleaner way of getting your machine's IP address, e.g.:
[String] $strIP = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter 'IPEnabled = "true"' | Select-Object -First 1).IPAddress;

Does Powershell ISE cache files? [duplicate]

This question already has an answer here:
Re-signing, Reloading, and running a test function from a powershell module?
(1 answer)
Closed 6 years ago.
I've been modifying a powershell module on the fly, immediately running afterwards a script that used it and kept not seeing my updates till I restarted powershell ISE. I suppose powershell ISE is caching at least modules. How do I clear the cache or control it?
You can try to Remove-Module and Import-Module, or Import-Module -Force, or Import-Module -Refresh.
Also you can google a bunch of similar topics on SO in like 15 seconds.