2012-05-24 341 views
60

我正在編寫powershell腳本,我想從服務器A運行。 我想連接到服務器B並將文件複製到服務器A作爲備份。使用Powershell遠程複製文件

如果不能做到那麼我想從服務器A連接到服務器B和文件複製到另一個目錄服務器B.

我看到他們Copy-Item命令,但我不明白如何給它一個計算機名稱。

我本來以爲我可以做類似

Copy-Item -ComputerName ServerB -Path C:\Programs\temp\test.txt -Destination (not sure how it would know to use ServerB or ServerA) 

我怎樣才能做到這一點?

+3

要使用複製項目,您必須使用UNC路徑,如「\\ ServerB \ C $ \ Programs \ temp \ test.txt」 –

回答

67

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.

這隻能當用戶下誰有權帶來管理股PowerShell會話中運行。 我建議使用服務器B普通網絡共享與只讀訪問到每一個人,只需調用(從服務器A):

Copy-Item -Path "\\\ServerB\SharedPathToSourceFile" -Destination "$Env:USERPROFILE" -Force -PassThru -Verbose 
+7

此方法的一個可能問題是Copy-Item不支持備用憑據(如果您必須以不同的用戶運行該命令)。在這種情況下,需要使用New-PSDrive方法。 –

+1

或者你可以使用'Invoke-Command'。 –

+0

此解決方案僅適用於阻止UNC共享的主機之間沒有防火牆的情況。在這種情況下,正確的解決方案如下('Copy-Item -FromSession')。 – Marc

35

爲什麼不使用net useNew-PSDrive來創建新的驅動器。

新PSDrive來:創建一個新PSDrive來僅在PowerShell中environement可見:

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

NET USE:創建操作系統的所有部分可見的新的驅動器。

Net use y: \\ServerName\Share 
Copy-Item BigFile Y:\BigFileCopy 
13

只需在遠程文件需要你的憑證來獲得訪問的情況下,可以生成使用cmdlet的新物體「複製遠程文件」,像這樣

$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) 

System.Net.WebClient對象或者如果你需要上傳你可以使用一個文件UploadFile

$Dest = "\\192.168.x.x\somefile.txt" 
$Source = "C:\Users\user\somefile.txt" 

$WebClient.UploadFile($Dest, $Source) 
+1

hack detected :) –

+0

@klm_你能解釋一下你的意思嗎? – FastTrack

0

沒有上述答案適合我。不停地收到此錯誤:

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

所以爲我做:

netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes 
從我的主機

然後我在運行框中機只是這樣做\ {nanoserver的IP} \ C $

+1

您有機會遇到共享+文件系統權限問題。請記住,限制性最強的權限會贏,所以即使您有權訪問NTFS文件系統層,如果共享權限限制了您,那麼您將無法寫入。 :) –

+0

這不適合我,仍然得到錯誤 –

39

從PowerShell版本5開始(包含在Windows Server 2016,downloadable as part of WMF 5 for earlier versions中),這可以通過遠程進行。這樣做的好處是,即使無論出​​於何種原因,您都無法訪問股票,它仍然有效。

爲此,啓動複製的本地會話必須安裝PowerShell 5或更高版本。遠程會話確實需要而不是需要安裝PowerShell 5 - 它適用於低至2的PowerShell版本和低於2008 R2的Windows Server版本。

從服務器A,創建一個會話到服務器B:

$b = New-PSSession B 
從A

,然後靜靜的:

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

項目複製到B與-ToSession完成。請注意,在這兩種情況下都使用本地路徑。你必須跟蹤你所在的服務器。

+2

我發現這兩個服務器都沒有必要安裝PS 5。我僅在源服務器(Windows 10)安裝了PS 5的情況下執行了成功的測試。目標是安裝了默認PS的Windows Server 2012 R2($ PSVersionTable.PSVersion報告4)。 –

+2

如果您在源上使用-ToSession,則只需要安裝源5。如果您在目標上使用-FromSession,則只需安裝目標需要的PS 5。 –

+1

當你只安裝了Hypervisor(沒有服務器)時,這也適用,不需要使用會話設置共享! – dashesy