尝试压缩服务器上的文件夹并将其保存在另一个共享文件夹中,
invoke-command -Credential domain\Oleg -computername 'server1' {
add-type -AssemblyName "system.io.compression.filesystem"
[io.compression.zipfile]::createfromdirectory("D:\Folder1\","\\server2\D$\ZIP.zip")}
但我遇到了一个错误
"Exception calling "CreateFromDirectory" with "2" argument(s): "Access to the path
'\\server2\D$\ZIP.zip' is denied."
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : UnauthorizedAccessException
+ PSComputerName : Server1"
我做错了什么?或者我需要在哪里添加权限?
答案1
这是老旧的“双跳”问题。有几种方法(最后统计有 7 种)可以解决这个问题。您需要在脚本块中传递一组新的凭据,以便将它们传递到 Server2 以供使用。在 Server1 上压缩文件,然后使用调用语句和传递的凭据从 Server2 运行复制命令。
$cred = Get-Credential
Invoke-Command -Credential $cred -ComputerName 'server1' {
add-type -AssemblyName "system.io.compression.filesystem"
[io.compression.zipfile]::createfromdirectory("D:\Folder1\","D:\ZIP.zip")
Invoke-Command -Credential $Using:cred -ComputerName 'server2' {
Copy-Item "\\server1\d$\ZIP.zip" "D:\"
}
}