【问题标题】:Copy files from UNC path to C:\ and Program files (x86) folder within a remotly generated New-PSSession将文件从 UNC 路径复制到远程生成的 New-PSSession 中的 C:\ 和 Program files (x86) 文件夹
【发布时间】:2017-08-24 09:43:43
【问题描述】:

我正在编写一个脚本,它创建一个新 VM,通过 New-PSSession 连接到它并运行多个命令来更改一些设置并将文件夹从 UNC 路径复制到本地 C:C:\Program files (x86)

除了复制部分外一切正常 - 我收到一条错误消息

权限被拒绝。

我以域管理员身份运行脚本本身,并且我传递的凭据也具有域管理员权限。

例如:

$source = '\\server\share'
$cred = Get-Credential
$pss = New-PSSession -ComputerName "$computername" -Credential $cred
Invoke-Command -Session $pss -ScriptBlock {
    Copy-Item "$source\FOLDER" -Destination 'C:\FOLDER' -Recurse -Force -
Credential 
$cred

即使我通过了证书,它也失败了。 “使用 robocopy” 中的快速搜索结果,但我认为必须可以使用 PowerShell 将文件从 UNC 路径复制到本地目录,即使该目录基本上受 Microsoft 保护。 p>

【问题讨论】:

标签: powershell unc copy-item


【解决方案1】:

我认为您需要将变量传递给远程会话:

invoke-Command -Session $pss -Args $source -ScriptBlock{Copy-Item "$args[0]\FOLDER" ...}

否则 $source 被认为是远程会话中的一个变量,因为它不存在那里 ps 将尝试从 \Folder 复制

【讨论】:

  • 啊,是的,我的错。实际上,我是在当前脚本中这样做的,并且该变量在远程会话中是已知且可解析的。
  • 我在使用 enter-pssession 的交互式会话中遇到了同样的错误,并使用 new-psdrive -name Y -PSProvider FileSystem -Root $source -Credential $cred set-location Y: 这样做我解决了它能够复制文件。我把它放到我的 new-pssession 中并让我的脚本运行。现在需要一些时间,我会在得到结果后立即写信
【解决方案2】:

你可以尝试使用-ToSession参数而不进入PSSession。

$ses = New-PSSession -ComputerName WorkStation


Copy-Item -ToSession $ses -Destination C:\users\TempUser\Documents\ -Path '\\10.0.0.1\share\' -Recurse

【讨论】:

  • 欢迎来到 SO。还请花一些时间为您的解决方案添加简短说明。
【解决方案3】:

我对 Copy-Item 的解决方案是:

-Destination $($(dir "env:programfiles(x86)").value + "\Avest\AvPCM_nces\")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多