Copy file remotely with PowerShell

后端 未结 5 574
失恋的感觉
失恋的感觉 2020-11-28 03:03

I am writing a PowerShell script that I want to run from Server A. I want to connect to Server B and copy a file to Server A as a backup.

If that can\'t be

相关标签:
5条回答
  • 2020-11-28 03:15

    From PowerShell version 5 onwards (included in Windows Server 2016, downloadable as part of WMF 5 for earlier versions), this is possible with remoting. The benefit of this is that it works even if, for whatever reason, you can't access shares.

    For this to work, the local session where copying is initiated must have PowerShell 5 or higher installed. The remote session does not need to have PowerShell 5 installed -- it works with PowerShell versions as low as 2, and Windows Server versions as low as 2008 R2.[1]

    From server A, create a session to server B:

    $b = New-PSSession B
    

    And then, still from A:

    Copy-Item -FromSession $b C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt
    

    Copying items to B is done with -ToSession. Note that local paths are used in both cases; you have to keep track of what server you're on.


    [1]: when copying from or to a remote server that only has PowerShell 2, beware of this bug in PowerShell 5.1, which at the time of writing means recursive file copying doesn't work with -ToSession, an apparently copying doesn't work at all with -FromSession.

    0 讨论(0)
  • 2020-11-28 03:24

    None of the above answers worked for me. I kept getting this error:

    Copy-Item : Access is denied
    + CategoryInfo          : PermissionDenied: (\\192.168.1.100\Shared\test.txt:String) [Copy-Item], UnauthorizedAccessException>   
    + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
    

    So this did it for me:

    netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes
    

    Then from my host my machine in the Run box I just did this:

    \\{IP address of nanoserver}\C$
    
    0 讨论(0)
  • 2020-11-28 03:27

    Use net use or New-PSDrive to create a new drive:

    New-PsDrive: create a new PsDrive only visible in PowerShell environment:

    New-PSDrive -Name Y -PSProvider filesystem -Root \\ServerName\Share
    Copy-Item BigFile Y:\BigFileCopy
    

    Net use: create a new drive visible in all parts of the OS.

    Net use y: \\ServerName\Share
    Copy-Item BigFile Y:\BigFileCopy
    
    0 讨论(0)
  • 2020-11-28 03:35

    Simply use the administrative shares to copy files between systems. It's much easier this way.

    Copy-Item -Path \\serverb\c$\programs\temp\test.txt -Destination \\servera\c$\programs\temp\test.txt;
    

    By using UNC paths instead of local filesystem paths, you help to ensure that your script is executable from any client system with access to those UNC paths. If you use local filesystem paths, then you are cornering yourself into running the script on a specific computer.

    This only works when a PowerShell session runs under the user who has rights to both administrative shares.

    I suggest to use regular network share on server B with read-only access to everyone and simply call (from Server A):

    Copy-Item -Path "\\\ServerB\SharedPathToSourceFile" -Destination "$Env:USERPROFILE" -Force -PassThru -Verbose
    
    0 讨论(0)
  • 2020-11-28 03:37

    Just in case that the remote file needs your credential to get accessed, you can generate a System.Net.WebClient object using cmdlet New-Object to "Copy File Remotely", like so

    $Source = "\\192.168.x.x\somefile.txt"
    $Dest   = "C:\Users\user\somefile.txt"
    $Username = "username"
    $Password = "password"
    
    $WebClient = New-Object System.Net.WebClient
    $WebClient.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
    
    $WebClient.DownloadFile($Source, $Dest)
    

    Or if you need to upload a file, you can use UploadFile:

    $Dest = "\\192.168.x.x\somefile.txt"
    $Source   = "C:\Users\user\somefile.txt"
    
    $WebClient.UploadFile($Dest, $Source)
    
    0 讨论(0)
提交回复
热议问题