Copy Folder Remote to Remote (No sharing path) - powershell

We are writing powershell script that will make backup on remote server. I am looking for some code reference that can copy my folder from remote to remote machine .
Trying with below sample code but "ToSession" is working with powershell 4.0.
We are writing powershell script (windows) that will run in separate server and create backup in remote machine(s).
Copy-Item –Path $sourcePath -Filter *.* -Recurse -Destination $targetPath -ToSession $session

Related

How to copy files from one directory to another in the same PS session

I want to copy files from a remote session (a VM) and paste them in a different directory in the same session.
The Copy-Item command only allows me to copy from local machine to a remote session and vice versa.
I tried using -FromSession and -ToSession commands but they are mutually exclusive.
As you want to copy files from folderA to folderB on a Windows remote machine you can do:
#Define the actions to execute on the remote computer
$scriptBlock = {
move-item -Path [path] -Destination [path]
}
#Run the command on the remote computer
invoke-command -ComputerName [dnsHostName] -ScriptBlock $scriptBlock

Powershell Copy-Item does not keep folderstructure like intended

I try to use powershell to deploy a build (Visual Studio) to multiple remote server.
The problem is, that the powershellscript does not copy the folders like I intendet to.
I use the following powershell command to copy the published website to the destinations.
Copy-Item -Path D:\Websites\$(ProjectName)\ -Recurse -Destination \\%RemoteServer%\D$\Websites\$(ProjectName)\ -Container -Exclude *.config -force
When the folder on the remoteserver does not exist, everything works fine.
But if the folder exists, all files are copied into a new subfolder instead of overwriting the existing ones.
So in the first execution the destination is corrected and looks like this:
D:\Websites\TestWebsite\ {all files}
After a second copy, the destination of the copy is:
D:\Websites\TestWebsite\Publish\ {all files}
The variables ProjectName and RemoteServer are in both executions the same.

Remote installation of exe using powershell

I have a script to copy a file to remote servers C:\temp and also to execute the exe file remotely. But the problem here is, it is copying .exe file to c:\temp of the remote machine, but not executing the exe.
Can anyone help me to correct here ?
ForEach ($Computer in Get-Content C:\scripts\servers.txt)
{
Copy-Item -Path '\\serverA\c$\scripts\Firefox Setup 52.0.1.exe' -Destination \\$Computer\c$\temp\;
Invoke-Command -AsJob -ComputerName $Computer -ScriptBlock { & "C:\temp\Firefox Setup 52.0.1.exe -ms" }
}
Try this,
Step by step breif explation is given for Installing-remote-software using Powershell

Excluding a file while using Copy-Item

I'm having a few issues while copying desktop files as a part of the back up process from local folder to server.
My script automatically creates a backup folder on the server and I just realised when it creates the back up folder windows automatically creates a desktop.ini file, and as desktop.ini is a system file the script failed to write another desktop.ini from desktop to the backup folder in server.
My initial code was :
Copy-Item ("$Global:localBackupRestoreLocation\Desktop\$file") ("$Global:remoteBackupRestorePath\$nameOfbackupDirectory") ($recursive="true")
But I was to use the exclude in that line and I know exclude does not work recursively and I have to use Get-ChildItem.
Try something like this
$Exclude = #('desktop.ini')
copy-item -Path $Source -Destination $Dest -Exclude $Exclude

Copy-Item with remote session - how to reference remote variables?

I'm using Copy-Item with the -ToSession parameter to copy some local files to a remote machine. Is there a straightforward way to make the cmdlet use variables from the remote session, instead of the local one? (I imagine I can always just output the variable in the remote session and read it locally, but that's a bit unwieldy...)
Specifically, I just want to copy the files into $env:USERPROFILE directory.
$session = New-PSSession ...
Copy-Item ".\some-file.txt" -ToSession $session -Destination $env:USERPROFILE