2015-02-24 90 views
2

我在服務器A(開發盒)創建遠程Powershell會話到服務器B(IIS服務器)。從服務器C(建機)的腳本複製到服務器B.PowerShell複製項遠程機器複製到錯誤的位置

我得以通過(我認爲)的多跳憑證問題,除了副本複製到錯誤的位置上服務器B.

這是應該複製到d:\ wwwroot \ HelloWorld,而是複製到c:\ users \ me \ Documents \ HelloWorld。

所有服務器都是Win2012r2,並且在使用PowerShell v3的域上。

服務器B運行:

winrm set winrm/config/service/auth '@{CredSSP="true"}' 

在服務器A上運行:

winrm set winrm/config/client/auth '@{CredSSP="true"}' 

這裏是我的腳本:

$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force 
$cred= New-Object System.Management.Automation.PSCredential ("mydomain\me", $password) 
$sesh = new-pssession -computername "ServerB" -credential $cred -Authentication CredSSP 

$sitePath = "D:\wwwroot\HelloWorld" 
Invoke-Command -Session $sesh -ScriptBlock { 
    Copy-Item -path "\\ServerC\Builds\HelloWorld\HelloWorld.1\_PublishedWebsites\HelloWorld" -Destination $sitePath -Recurse -Force 
} 

爲什麼沒有聽我的目的地?

+1

'幫助about_Remote_Variables' – PetSerAl 2015-02-24 20:47:41

+0

https://technet.microsoft。 com/en-us/library/jj149005.aspx – jessehouwing 2015-02-24 20:50:51

回答

3

閱讀remote variable documentation。您的$Sitepath在本地定義,並未在遠程腳本塊中定義。

因此,無論使用在一個局部變量帶來的語法:

$sitePath = "D:\wwwroot\HelloWorld" 
Invoke-Command -Session $sesh -ScriptBlock { 
    Copy-Item -path "\\ServerC\Builds\HelloWorld\HelloWorld.1\_PublishedWebsites\HelloWorld" 
       -destination $Using:sitePath -Recurse -Force 
} 

或者在腳本塊定義它:

Invoke-Command -Session $sesh -ScriptBlock { 
    $sitePath = "D:\wwwroot\HelloWorld" 
    Copy-Item -path "\\ServerC\Builds\HelloWorld\HelloWorld.1\_PublishedWebsites\HelloWorld" -Destination $sitePath -Recurse -Force 
} 
+0

不是一個完整的答案....如果$ sitePath是腳本的參數呢?但是,我正在閱讀完整答案的文檔。 – ginalster 2015-02-24 21:02:27

+0

我是那種情況下,你需要使用第一種語法。 – jessehouwing 2015-02-24 21:04:28

+0

對不起,錯過了第一個語法。完美的作品! – ginalster 2015-02-24 21:06:28