PowerShell复制项远程机器复制到错误的位置
我在服务器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
}
为什么没有听我的目的地?
阅读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
}
不是一个完整的答案....如果$ sitePath是脚本的参数呢?但是,我正在阅读完整答案的文档。 – ginalster 2015-02-24 21:02:27
我是那种情况下,你需要使用第一种语法。 – jessehouwing 2015-02-24 21:04:28
对不起,错过了第一个语法。完美的作品! – ginalster 2015-02-24 21:06:28
'帮助about_Remote_Variables' – PetSerAl 2015-02-24 20:47:41
https://technet.microsoft。 com/en-us/library/jj149005.aspx – jessehouwing 2015-02-24 20:50:51